Skip to content

Commit 201731e

Browse files
committed
Readme update and polling daemon fix
1 parent 63f42d5 commit 201731e

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,72 @@ final class OnMessageController
9696
{
9797
return new Method\SendMessage(
9898
chatId: $message->from->id,
99-
text: 'You write: ' . $message->text,
99+
text: 'You wrote: ' . $message->text,
100100
);
101101
}
102102
}
103+
```
104+
105+
### Access control controller
106+
```php
107+
use Luzrain\TelegramBotApi\Event;
108+
use Luzrain\TelegramBotApi\EventCallbackReturn;
109+
use Luzrain\TelegramBotApi\Type;
110+
use Luzrain\TelegramBotBundle\Attribute\OnEvent;
111+
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
112+
113+
final class AccessControlController extends TelegramCommand
114+
{
115+
// Set the highest priority to ensure that this method is executed before any others.
116+
#[OnEvent(Event\Update::class, priority: 10)]
117+
public function __invoke(Type\Update $update): EventCallbackReturn
118+
{
119+
// Stop executing other controllers if the sender doesn't meet some conditions
120+
if ($update->message?->from->id !== 123456789) {
121+
return $this->stop();
122+
}
123+
124+
return $this->continue();
125+
}
126+
}
127+
```
128+
129+
### Publish list of command as bot button
130+
It's possible to publish all your commands that will be shown as list of available commands in the bot's menu button.
131+
To do this fill in the _description_ field and the _publish_ flag in the OnCommand attribute, and run the command.
132+
``` bash
133+
$ bin/console telegram:button:setcommands
134+
```
135+
136+
For button delete.
137+
``` bash
138+
$ bin/console telegram:button:delete
139+
```
140+
141+
```php
142+
use Luzrain\TelegramBotApi\Method;
143+
use Luzrain\TelegramBotApi\Type;
144+
use Luzrain\TelegramBotBundle\Attribute\OnCommand;
145+
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
146+
147+
final class CommandsController extends TelegramCommand
148+
{
149+
#[OnCommand(command: '/command1', description: 'Test command 1', publish: true)]
150+
public function __invoke(Type\Message $message): Method
151+
{
152+
return $this->reply('Command 1 response');
153+
}
154+
155+
#[OnCommand(command: '/command2', description: 'Test command 2', publish: true)]
156+
public function __invoke(Type\Message $message): Method
157+
{
158+
return $this->reply('Command 2 response');
159+
}
160+
161+
#[OnCommand(command: '/command3', description: 'This command will not be published', publish: false)]
162+
public function __invoke(Type\Message $message): Method
163+
{
164+
return $this->reply('Command 3 response');
165+
}
166+
}
103167
```

src/TelegramBot/Command/PolllingStartCommand.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function getDefaultDescription(): string
3636

3737
protected function configure(): void
3838
{
39-
$this->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'Timeout for long polling connection in seconds', 15);
39+
$this->addOption('timeout', 't', InputOption::VALUE_REQUIRED, 'Timeout for long polling connection in seconds', 15);
4040
}
4141

4242
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -64,6 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6464

6565
$callbackResponse = $this->updateHandler->handle($update);
6666

67+
if ($callbackResponse === null) {
68+
continue;
69+
}
70+
6771
try {
6872
$this->botApi->call($callbackResponse);
6973
} catch (TelegramApiException $e) {

0 commit comments

Comments
 (0)