-
Notifications
You must be signed in to change notification settings - Fork 651
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
Support of "telegram sessions" wanted #533
Comments
👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. To help make it easier for us to investigate your issue, please follow the contributing guidelines. |
Well, while experimenting, I came up to this class: <?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Session\Middleware\StartSession;
use Telegram\Bot\Api;
class StartTelegramSession extends StartSession
{
/**
* Get the session implementation from the manager.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Session\Session
*/
public function getSession(Request $request)
{
/** @var Api $telegram */
$telegram = app('telegram');
$update = $telegram->getWebhookUpdates();
$sessionName = null;
if ($update) {
$sessionName = $update->getMessage()->getFrom()->getId();
return tap($this->manager->driver(), function ($session) use ($sessionName) {
$session->setId(str_pad($sessionName, 40, "0", STR_PAD_LEFT));
});
}
}
} And attach this middleware to the webhook route instead of default StartSession. |
Updated version of StartTelegramSession middleware which I'm now using: <?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Session\Middleware\StartSession;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;
class StartTelegramSession extends StartSession
{
public function handle($request, Closure $next)
{
$result = parent::handle($request, $next);
session()->save();
return $result;
}
/**
* Get the session implementation from the manager.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Session\Session
*/
public function getSession(Request $request)
{
/** @var Api $telegram */
$telegram = app('telegram');
$update = $telegram->getWebhookUpdate();
$sessionName = null;
if ($update instanceof Update) {
if ($update->getMessage()) {
$sessionName = $update->getMessage()->getFrom()->getId();
} else if ($update->getCallbackQuery()) {
$sessionName = $update->getCallbackQuery()->getFrom()->getId();
}
}
if ($sessionName) {
return tap($this->manager->driver(), function ($session) use ($sessionName) {
$session->setId(str_pad($sessionName, 40, "0", STR_PAD_LEFT));
});
} else {
return $this->manager->driver();
}
}
} |
Looks great, although the idea I had was to use Cache to support something like this since we could cache the full conversation and what not. Will consider this. Thanks for sharing. |
Thanks for solution!!! @Amegatron |
This is very important. Are there any updates on this? |
@Amegatron Do you think it is possible to use the session to track the language selected in the robot? |
Hello. I'm pretty new to Telegram botting, but already missing the out-of-the-box possibility of persisting session data within the bot.
I mean, I want to implement some kind of a multi-step process in chat and thus want to keep track of which stage of the process I'm currently on for the user sending a message. It is possible to handle this by my own code, but I suppose some kind of "Session" out-of-the-box, linked to chatter id, would be nice.
Also, I'm writing my bot using Laravel, maybe the idea could be implemented by means of Laravel's Session functionality. While I'm thinking this way I just decided to post it here, maybe you'll be faster and provide a better solution.
Thanks.
The text was updated successfully, but these errors were encountered: