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

Support of "telegram sessions" wanted #533

Closed
Amegatron opened this issue Mar 5, 2018 · 7 comments
Closed

Support of "telegram sessions" wanted #533

Amegatron opened this issue Mar 5, 2018 · 7 comments

Comments

@Amegatron
Copy link

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.

@welcome
Copy link

welcome bot commented Mar 5, 2018

👋 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.

@Amegatron
Copy link
Author

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.
In this case I have full functionality of sessions, not forgetting to call session()->save() at the end of the command.

@Amegatron
Copy link
Author

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();
        }
    }
}

@irazasyed
Copy link
Owner

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.

@DimaSmile
Copy link

Thanks for solution!!! @Amegatron

@mfissehaye
Copy link

This is very important. Are there any updates on this?

@ziZily
Copy link

ziZily commented Jul 25, 2021

@Amegatron Do you think it is possible to use the session to track the language selected in the robot?

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

6 participants
@irazasyed @mfissehaye @Amegatron @DimaSmile @ziZily and others