php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 953 forks source link

/whoami command fails if you don't have a profile picture #60

Closed PeterNotenboom closed 8 years ago

PeterNotenboom commented 8 years ago

The /whoami build-in command fails if you don't have a profile picture. It would show "xxxxxbot is typing..." And that disappears.

The following Stack-trace is then thrown:

2015-12-10 21:39:57 exception 'Longman\TelegramBot\Exception\TelegramException' with message 'total_count is empty!' in /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Entities/UserProfilePhotos.php:26
Stack trace:
#0 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Entities/ServerResponse.php(38): Longman\TelegramBot\Entities\UserProfilePhotos->__construct(Array)
#1 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Request.php(249): Longman\TelegramBot\Entities\ServerResponse->__construct(Array, 'xxxxxxxbot')
#2 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Request.php(392): Longman\TelegramBot\Request::send('getUserProfileP...', Array)
#3 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Commands/WhoamiCommand.php(55): Longman\TelegramBot\Request::getUserProfilePhotos(Array)
#4 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Command.php(53): Longman\TelegramBot\Commands\WhoamiCommand->execute()
#5 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Telegram.php(477): Longman\TelegramBot\Command->preExecute()
#6 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Telegram.php(432): Longman\TelegramBot\Telegram->executeCommand('whoami', Object(Longman\TelegramBot\Entities\Update))
#7 /var/www/vhosts/test/httpdocs/hooks/vendor/longman/telegram-bot/src/Telegram.php(390): Longman\TelegramBot\Telegram->processUpdate(Object(Longman\TelegramBot\Entities\Update))
#8 /var/www/vhosts/test/httpdocs/hooks/index.php(34): Longman\TelegramBot\Telegram->handle()
#9 {main}
MBoretto commented 8 years ago

Thanks for reporting, On the documentation the "count" field seems mandatory: https://core.telegram.org/bots/api#userprofilephotos Can you provide the json log of the update?

PeterNotenboom commented 8 years ago

I found the issue:

src/Entities/UserProfilePhotos.php:

$this->total_count = isset($data['total_count']) ? $data['total_count'] : null;
if (empty($this->total_count)) {

print_r($data) will give:

Array
(
   [total_count] => 0
   [photos] => Array
       (
       )
)

And a var_dump($this->total_count) will give (a correct): int(0) So the problem is:

if (empty($this->total_count)) {

int(0) is TRUE on empty(): http://stackoverflow.com/a/2220529

Photos (2 lines further) has the same problem. I can make a pull request.

PeterNotenboom commented 8 years ago

Pull request created. Edit: And see it's merged now. Closing issue!