Skip to content

Commit 520e564

Browse files
committed
Add constants for Update types and extend readme with new functionality
1 parent 9eacd3a commit 520e564

File tree

3 files changed

+81
-39
lines changed

3 files changed

+81
-39
lines changed

README.md

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A Telegram Bot based on the official [Telegram Bot API]
99

10-
[![API Version](https://img.shields.io/badge/Bot%20API-5.1%20%28March%202021%29-32a2da.svg)](https://core.telegram.org/bots/api#november-4-2020)
10+
[![API Version](https://img.shields.io/badge/Bot%20API-5.1%20%28March%202021%29-32a2da.svg)](https://core.telegram.org/bots/api#march-9-2021)
1111
[![Join the bot support group on Telegram](https://img.shields.io/badge/telegram-@PHP__Telegram__Bot__Support-64659d.svg)](https://telegram.me/PHP_Telegram_Bot_Support)
1212
[![Donate](https://img.shields.io/badge/%F0%9F%92%99-Donate%20%2F%20Support%20Us-blue.svg)](#donate)
1313

@@ -33,6 +33,7 @@ A Telegram Bot based on the official [Telegram Bot API]
3333
- [Unset Webhook](#unset-webhook)
3434
- [getUpdates installation](#getupdates-installation)
3535
- [getUpdates without database](#getupdates-without-database)
36+
- [Filter Update](#filter-update)
3637
- [Support](#support)
3738
- [Types](#types)
3839
- [Inline Query](#inline-query)
@@ -43,7 +44,6 @@ A Telegram Bot based on the official [Telegram Bot API]
4344
- [getUserProfilePhoto](#getuserprofilephoto)
4445
- [getFile and downloadFile](#getfile-and-downloadfile)
4546
- [Send message to all active chats](#send-message-to-all-active-chats)
46-
- [Filter Update](#filter-update)
4747
- [Utils](#utils)
4848
- [MySQL storage (Recommended)](#mysql-storage-recommended)
4949
- [External Database connection](#external-database-connection)
@@ -339,6 +339,45 @@ If you choose to / or are obliged to use the `getUpdates` method without a datab
339339
$telegram->useGetUpdatesWithoutDatabase();
340340
```
341341

342+
## Filter Update
343+
344+
Update processing can be allowed or denied by defining a custom update filter.
345+
346+
Let's say we only want to allow messages from a user with ID `428`, we can do the following before handling the request:
347+
348+
```php
349+
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter') {
350+
$user_id = $update->getMessage()->getFrom()->getId();
351+
if ($user_id === 428) {
352+
return true;
353+
}
354+
355+
$reason = "Invalid user with ID {$user_id}";
356+
return false;
357+
});
358+
```
359+
360+
The reason for denying an update can be defined with the `$reason` parameter. This text gets written to the debug log.
361+
362+
Alternatively, you can define which update types are handled by your bot, by defining them when setting the [webhook](#webhook-installation) or passing an array of allowed types when using [getUpdates](#getupdates-installation).
363+
364+
```php
365+
use Longman\TelegramBot\Entities\Update;
366+
367+
// Define the list of allowed Update types here.
368+
$allowed_updates = [
369+
Update::TYPE_MESSAGE,
370+
Update::TYPE_CHANNEL_POST,
371+
// etc.
372+
];
373+
374+
// When setting the webhook.
375+
$telegram->setWebhook($hook_url, ['allowed_types' => $allowed_updates]);
376+
377+
// When handling the getUpdates method.
378+
$telegram->handleGetUpdates($limit = null, $timeout = null, $allowed_updates);
379+
```
380+
342381
## Support
343382

344383
### Types
@@ -433,26 +472,6 @@ $results = Request::sendToActiveChats(
433472

434473
You can also broadcast a message to users, from the private chat with your bot. Take a look at the [admin commands](#admin-commands) below.
435474

436-
#### Filter Update
437-
438-
Update processing can be allowed or denied by defining a custom update filter.
439-
440-
Let's say we only want to allow messages from a user with ID 428, we can do the following before handling the request:
441-
442-
```php
443-
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter') {
444-
$user_id = $update->getMessage()->getFrom()->getId();
445-
if ($user_id === 428) {
446-
return true;
447-
}
448-
449-
$reason = "Invalid user with ID {$user_id}";
450-
return false;
451-
});
452-
```
453-
454-
The reason for denying an update can be defined with the `$reason` parameter. This text gets written to the debug log.
455-
456475
## Utils
457476

458477
### MySQL storage (Recommended)

src/Entities/Update.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,50 @@
3636
*/
3737
class Update extends Entity
3838
{
39-
public static $update_type_entities = [
40-
'message' => Message::class,
41-
'edited_message' => EditedMessage::class,
42-
'channel_post' => ChannelPost::class,
43-
'edited_channel_post' => EditedChannelPost::class,
44-
'inline_query' => InlineQuery::class,
45-
'chosen_inline_result' => ChosenInlineResult::class,
46-
'callback_query' => CallbackQuery::class,
47-
'shipping_query' => ShippingQuery::class,
48-
'pre_checkout_query' => PreCheckoutQuery::class,
49-
'poll' => Poll::class,
50-
'poll_answer' => PollAnswer::class,
51-
];
39+
public const TYPE_MESSAGE = 'message';
40+
public const TYPE_EDITED_MESSAGE = 'edited_message';
41+
public const TYPE_CHANNEL_POST = 'channel_post';
42+
public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
43+
public const TYPE_INLINE_QUERY = 'inline_query';
44+
public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
45+
public const TYPE_CALLBACK_QUERY = 'callback_query';
46+
public const TYPE_SHIPPING_QUERY = 'shipping_query';
47+
public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
48+
public const TYPE_POLL = 'poll';
49+
public const TYPE_POLL_ANSWER = 'poll_answer';
50+
public const TYPE_MY_CHAT_MEMBER = 'my_chat_member';
51+
public const TYPE_CHAT_MEMBER = 'chat_member';
5252

5353
/**
5454
* {@inheritdoc}
5555
*/
5656
protected function subEntities(): array
5757
{
58-
return self::$update_type_entities;
58+
return [
59+
self::TYPE_MESSAGE => Message::class,
60+
self::TYPE_EDITED_MESSAGE => EditedMessage::class,
61+
self::TYPE_CHANNEL_POST => ChannelPost::class,
62+
self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
63+
self::TYPE_INLINE_QUERY => InlineQuery::class,
64+
self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
65+
self::TYPE_CALLBACK_QUERY => CallbackQuery::class,
66+
self::TYPE_SHIPPING_QUERY => ShippingQuery::class,
67+
self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class,
68+
self::TYPE_POLL => Poll::class,
69+
self::TYPE_POLL_ANSWER => PollAnswer::class,
70+
self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class,
71+
self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class,
72+
];
73+
}
74+
75+
/**
76+
* Get the list of all available update types
77+
*
78+
* @return string[]
79+
*/
80+
public static function getUpdateTypes(): array
81+
{
82+
return array_keys((new self([]))->subEntities());
5983
}
6084

6185
/**
@@ -65,8 +89,7 @@ protected function subEntities(): array
6589
*/
6690
public function getUpdateType(): ?string
6791
{
68-
$types = array_keys($this->subEntities());
69-
foreach ($types as $type) {
92+
foreach (self::getUpdateTypes() as $type) {
7093
if ($this->getProperty($type)) {
7194
return $type;
7295
}

src/Telegram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public function handleGetUpdates(?int $limit = null, ?int $timeout = null, ?arra
416416

417417
// By default, allow ALL known update types.
418418
if ($allowed_updates === null) {
419-
$allowed_updates = array_keys(Update::$update_type_entities);
419+
$allowed_updates = Update::getUpdateTypes();
420420
}
421421
$offset = 0;
422422

0 commit comments

Comments
 (0)