BotMan makes it simple to access your chatbot user information. Which user fields are available purely depend on the messaging service you use. Some services offer more information than others do. Yet you can access them using the same API with BotMan.
BotMan has a simple way to let you retrieve user relevant information. Once you listened to a command and are in either a conversation or a message callback, you can use the getUser
method on the BotMan instance.
You can access the user's first name using:
// Access user
$user = $bot->getUser();
// Access first name
$firstname = $user->getFirstName();
You can access the user's last name using:
// Access user
$user = $bot->getUser();
// Access last name
$lastname = $user->getLastName();
You can access the user ID using:
// Access user
$user = $bot->getUser();
// Access ID
$id = $user->getId();
You can access the user Username using:
// Access user
$user = $bot->getUser();
// Access Username
$id = $user->getUsername();
Be aware that you might not receive any username data if the user hasn't set a username in their messenger settings.
You can access also access the unprocessed raw messaging service user information that BotMan receives using:
// Access user
$user = $bot->getUser();
// Access Information
$info = $user->getInfo();
BotMan will cache user information for a duration of 30 minutes by default. This is especially useful for drivers like
Facebook where it takes an additional request to retrieve this information. In the BotMan config you are able to
change this duration with the user_cache_time
. You can also set it to zero in order to prevent BotMan from caching the user information at all.
'botman' => [
'user_cache_time' => 30
],