Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the callback function of the onCommand method, calling getMe() does not work. #87

Open
wf58585858 opened this issue Jun 14, 2023 · 1 comment

Comments

@wf58585858
Copy link

wf58585858 commented Jun 14, 2023

$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?

@awohsen
Copy link
Contributor

awohsen commented Nov 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants