Skip to content

Commit

Permalink
Fix type issue parsing "entities" (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
lptn committed Jul 7, 2022
1 parent 50b05f7 commit 34acb88
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Telegram\Bot\Answers\Answerable;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\MessageEntity;
use Telegram\Bot\Objects\Update;

/**
Expand Down Expand Up @@ -352,8 +353,8 @@ private function allCommandOffsets()
collect() :
$message
->get('entities', collect())
->filter(function ($entity) {
return $entity['type'] === 'bot_command';
->filter(function (MessageEntity $entity) {
return $entity->type === 'bot_command';
})
->pluck('offset');
}
Expand Down
16 changes: 10 additions & 6 deletions src/Commands/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Telegram\Bot\Answers\AnswerBus;
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Objects\MessageEntity;
use Telegram\Bot\Objects\Update;
use Telegram\Bot\Traits\Singleton;

Expand Down Expand Up @@ -173,8 +174,11 @@ protected function handler(Update $update): Update

if ($message->has('entities')) {
$this->parseCommandsIn($message)
->each(function (array $botCommand) use ($update) {
$this->process($botCommand, $update);
->each(function ($botCommandEntity) use ($update) {
$botCommandAsArray = $botCommandEntity instanceof MessageEntity
? $botCommandEntity->all()
: $botCommandEntity;
$this->process($botCommandAsArray, $update);
});
}

Expand All @@ -186,13 +190,13 @@ protected function handler(Update $update): Update
*
* @param $message
*
* @return Collection
* @return Collection<int, MessageEntity>
*/
protected function parseCommandsIn(Collection $message): Collection
{
return collect($message->get('entities'))
->filter(function ($entity) {
return $entity['type'] === 'bot_command';
return Collection::wrap($message->get('entities'))
->filter(function (MessageEntity $entity) {
return $entity->type === 'bot_command';
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CommandsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function processCommand(Update $update)
*
* @param string $name Command Name
* @param Update $update Update Object
* @param null $entity
* @param array|null $entity
*
* @return mixed
*/
Expand Down

0 comments on commit 34acb88

Please sign in to comment.