When coding telegram bots, one of the things you will find is that each HTTP query that the system does to your code is independent and sessionless. The Telegram API does not provide a way to remember things; it is up to you to do that.
Why remember? Useful bots are not those where you answer one question and they give you an answer, and then you ask the same question and they give the same answer. Bots have to evolve and remember your last actions; you may want to have a bot that chats with you, it needs to remember your name and so on.
Fortunately for us, PHP has sessions, an easy way to have a memory of what happens. PHP sessions out of the box won't work with Telegram bot; when a browser interacts with a PHP script that uses sessions, the browser uses a cookie to name the session and PHP automatically gets it. In this case, you are attending a headless client that doesn't support the cookies. Here is what I did.
This article assumes knowledge of the Longman PHP Telegram Bot library. Which in my opinion is the best PHP library to write Telegram bots. Also note that this is a short-term memory solution, it is not meant to be long-term.
I wrote this library https://github.com/daniel-lucio/telegram-sessions. Follow this example as a guide.
This small code will answer hello in the language the Telegram user has configured.
composer.json
...
"autoload": {
"files": [
...
"helpers/sessions.php"
]
},
...
StartCommand.php
declare(strict_types=1);
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Request;
use okayinc;
class StartCommand extends UserCommand {
protected $name = 'start';
protected $description = 'Start command';
protected $version = '0.2.0';
protected $usage = '/start';
protected $private_only = true;
public function execute(): ServerResponse
{
$message = $this->getMessage();
$from_user = $message->getFrom();
$chat = $message->getChat();
$from_user_id = $from_user->getId();
$chat_id = $chat->getId();
okayinc\telegram_session::start(md5($from_user_id.'@'.$chat_id));
okayinc\telegram_session::reset();
okayinc\telegram_session::set('setup_step',1);
$answer = $l->get('hello');
return $this->replyToChat($answer,[]);
}
}
GenericmessageCommand.php
declare(strict_types=1);
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Request;
use okayinc;
class StartCommand extends UserCommand {
protected $name = 'genericmessage';
protected $description = 'Show generic command';
protected $version = '0.2.0';
public function execute(): ServerResponse
{
$message = $this->getMessage();
$from_user = $message->getFrom();
$chat = $message->getChat();
$from_user_id = $from_user->getId();
$chat_id = $chat->getId();
okayinc\telegram_session::start(md5($from_user_id.'@'.$chat_id));
$setup_step = okayinc\telegram_session::get('setup_step');
switch ($setup_step){
case 1:
$answer = 'First step';
okayinc\telegram_session::set('setup_step', 2);
break;
case 2:
$answer = 'Second step';
okayinc\telegram_session::set('setup_step', 3);
break;
default:
$answer = 'Let us restart';
okayinc\telegram_session::set('setup_step', 1);
}
return $this->replyToChat($answer,[]);
} }
This example implements memory on a session variable called setup_step. The key here to make it work is the start method. I have selected to use user_id@chat_id to make unique session cookies. The MD5 is just to obfuscate the name of the session.
Yes, of course, I have had this discussion with some developers:
However, I feel it is too much drama there to implement something as easy as short-term memory capability. Remember PHP session engine can be stored not only on files (by default) but on different storages, including Redis and database.
Yes, long-term memory, such as remembering a purchase must be done through a database.
Good luck!
blog comments powered by DisqusAbout
Read about IT, Migration, Business, Money, Marketing and other subjects.
Some subjects: FusionPBX, FreeSWITCH, Linux, Security, Canada, Cryptocurrency, Trading.