If I call $bot->getUser() inside a method of the middleware I receive this error for telegram:
BotMan\Drivers\Telegram\Exceptions\TelegramException: Error retrieving user info: Bad Request: wrong user_id specified in file /Applications/MAMP/htdocs/rank_bot/vendor/botman/driver-telegram/src/TelegramDriver.php on line 57
the same for messenger:
BotMan\Drivers\Facebook\Exceptions\FacebookException: Error sending payload: Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api in file /Applications/MAMP/htdocs/rank_bot/vendor/botman/driver-facebook/src/FacebookDriver.php on line 406
botman.php
$authMiddleware = new Authorization();
$botman->middleware->received($authMiddleware);
$botman->fallback(BotManController::class.'@startConversation');
Authorization.php
class Authorization implements MiddlewareInterface
{
public function captured(IncomingMessage $message, $next, BotMan $bot)
{
return $next($message);
}
public function heard(IncomingMessage $message, $next, BotMan $bot)
{
return $next($message);
}
public function matching(IncomingMessage $message, $pattern, $regexMatched)
{
return true;
}
public function received(IncomingMessage $message, $next, BotMan $bot)
{
$user=$bot->getUser();
return $next($message);
}
public function sending($payload, $next, BotMan $bot)
{
return $next($payload);
}
}
Retrieving users works by using the matched message. In your case, BotMan doesn't know which message might match, because it is inside a received middleware.
To get the user anyway, just pass the message to the driver getUser method.
$user = $bot->getDriver()->getUser($message);
And how to get user info in Conversation class.
Im using $username = $bot->getUser()->getUsername(); in ExampleConversation but have an error Undefined variable: bot
$bot is not defined, you need to use $this->bot
Most helpful comment
Retrieving users works by using the matched message. In your case, BotMan doesn't know which message might match, because it is inside a received middleware.
To get the user anyway, just pass the message to the driver
getUsermethod.