zanzara
zanzara copied to clipboard
In the callback function of the onCommand method, calling getMe() does not work.
$bot->onCommand('hello' , function(Context $ctx){
file_put_contents('./input.json' , 'onCommand hello----' . PHP_EOL , 8);
$firstName = $ctx->getEffectiveUser()->getFirstName();
$ctx->sendMessage("Hi $firstName, I'm replying to /hello command");
});
$bot->onCommand('getMe' , function(Context $ctx) use ($bot){
file_put_contents('./input.json' , 'onCommand getMe----' . PHP_EOL , 8);
echo 111111111111;
echo PHP_EOL;
$ctx->getMe()->then(function(User $user) use ($ctx){
echo "Hi " . $user->jsonSerialize();
echo PHP_EOL;
$ctx->sendMessage("Hi " . $user->jsonSerialize());
} , function(TelegramException $error){
echo $error->getDescription() . ' failed......';
echo PHP_EOL;
});
echo 222222222222;
echo PHP_EOL;
});
When I enter /hello to the bot, it can return normally. However, when I enter /getMe, the following two lines can work normally:
echo 111111111111; echo 222222222222; However, the $ctx->getMe() method in the middle does not work properly. No information is received in the bot. What could be the problem?
Simply use the returned $user for that:
$ctx->getMe()->then(function(User $user) use ($ctx){
echo "Hi \n" . $user;
echo PHP_EOL;
$ctx->sendMessage("Hi \n" . $user);
});
jsonSerialize method is meant to provide an array to be used by json_decode, User object has __toString() magic method for you.