Search code examples
pythonbotframeworkmicrosoft-teams

How do I save/load Bot Framework session information to/from the local disk?


I have a question about a Microsoft Teams Python bot. If the bot has been added to some personal chats and group chats and I restart the bot, sometimes the bot needs to be added to the chats again. So I want to make bot sessions.

Is it possible to make a bot session in Microsoft Teams? I want to store session information on the local disk, and then have the bot load that data when it starts.

My bot code is very similar to this sample.

Thank you for your help.

Updated:

Like I said my bot code is very similar to this sample, but a bit different. Because of this I created an example for this question. First of all I create a bot in Azure and set up it.

Azure bot configuration

After this, in my bot's config.py file I set up the port and Microsoft app ID and password (generated by clicking the "Manage" button).

import os
""" Bot Configuration """
class DefaultConfig:
    """ Bot Configuration """

    PORT = 3978
    APP_ID = os.environ.get("MicrosoftAppId", "sadsadsadasd")
    APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "asdasdasdasdasd")

After this I execute the command ngrok http 3978 and put the generated endpoint in the Azure bot configuration. To register the bot as an application, I use App Studio in Teams. After I do that, I just to need run the bot in CMD so I run a command like python run.py

After I run the bot, I can add it in the channel and run commands and functions that I created in the code.

This is just an example of how I set up the bot. The main bot is on a Linux server.

Here is why I want to make the bot keep session information and load it after the server or bot was restarted. Sometimes after I restart the bot or server it is no longer in the chat or team. In the future I want make some kind of commands and execute them using a cron job or something like that.

If the bot disappears from a chat then I can't use bot commands in that chat. For example, I add two bots in a chat. After I restart one of them I can't get any response from it like in the picture below.

Conversation

And with @ I can't see the bot.

Empty search results

I have an idea. After I add the bot in the chat I get this in the console:

Adding new conversation to the list: {'additional_properties': {}, 'activity_id': '123215513', 'user': <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027C0ED60>, 'bot': <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027Cs2FD0>, 'conversation': <botbuilder.schema._models_py3.ConversationAccount object at 0x0000027C0400>, 'channel_id': 'msteams', 'locale': 'en-US', 'service_url': 'https://smba.trafficmanager.net/emea/'}

Formatted:

{
  "additional_properties": {},
  "activity_id": "123215513",
  "user": <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027C0ED60>,
  "bot": <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027Cs2FD0>,
  "conversation": <botbuilder.schema._models_py3.ConversationAccount object at 0x0000027C0400>,
  "channel_id": "msteams",
  "locale": "en-US",
  "service_url": "https://smba.trafficmanager.net/emea/"
}

So if I store this information and then load it when I start the bot, maybe it will work?


Solution

  • Like Hilton said, it's a bad idea to try and save bot state to the local disk. Also, there's no need for that. Both Hilton and I have linked to documentation that should help you understand how bot state is meant to be saved.

    I cannot reproduce the problem you're encountering where the bot gets removed from Teams chats. The problem sounds impossible anyway, based on how Teams works and how bots work. Teams should have no way of knowing whether your bot is started or stopped. It's possible that your server is set up to manually uninstall the bot from Teams conversations based on when the bot starts and stops, but that would still be very strange. I'm willing to continue troubleshooting this with you, but I thought I'd post an answer now in case you'd like to award your bounty to someone before it expires.