I am trying to implement simple slack bot. So I have configured
hubot which will take inputs from
slack and passing it to my webapp (
django app) and it will take whatever the response from
django-app and will reply to slack.
In this process I am trying to store session in
request.session but that is not reflected in
slack. If I am accessing the
django-url in browser it is able to store sessions and getting proper response with session.
So does the problem lie with
slack or my approach and is there a way to store sessions in hubot when requesting to
I can not speak to the specific technologies you use (hubot, django), but I am using server sessions with my Slack apps all the time and can give you a general answer on how it works. Note that my Slack apps are build with PHP, but I think its safe to assume that the principles are the same.
<strong>Slack does not support sessions</strong>
In general Slack does not support sessions or context. Instead everything is request based. So if you want to have sessions to keep a functional context between requests you need to organize that by yourself in your Slack app.
<strong>Challenge for using server sessions with server requests</strong>
One challenge is that most server sessions are designed to work with a client that uses a browser. e.g. a PHP server session will store a cookie in the browser, so the server knows, which requests belong to the same session. This does obviously not work with Slack, since all Slack requests are coming from a server and and there is no browser involved.
<strong>Approach for using server sessions with Slack</strong>
But you can use severs session with Slack with these two tricks:
<em>Manually set the session ID</em>
Usually the ID of a session is chosen automatically by the server, but you can also set it manually. This allows you to tell the server to continue an existing session that was started with a previous request.
<em>Include session ID in Slack control</em>
The functional session of a user is tied together by the Slack controls he uses. (e.g. an interactive button). Its possible to include custom data in those controls (see <a href="https://stackoverflow.com/a/45971885/4379151" rel="nofollow">this answer</a> for details) and that allows you to include the current session ID in it.
You include the ID of your current session in the Slack controls, that you create with your app (e.g. an interactive button). Once the user clicks a button Slack will send a request to your app, which will include the session ID. That allows your app to continue an already started server session.