Forum Discussion
storage of bot state
hi team,
i have created bot app using teams toolkit , i want to store user specific data like token , so i got to know that i can implement using CosmosDB but for now i want to store data for testing so
for now i got to know that i can implement using session or var in Memory Storage
but there is no clear data
could you please provide any resource so that i can continue
thank you
- LeonPavesicSilver Contributor
Hi sowmyaseetha,
There are a few ways to store user specific data for a bot app created using Teams Toolkit, including CosmosDB, memory storage, and file storage.
Session and var are two ways to store data in memory storage. Session variables are stored for the duration of a user's session, while var variables are stored for the duration of the bot's execution.
To use session variables, you can use the bot.session.set() and bot.session.get() methods. To use var variables, you can use the bot.var.set() and bot.var.get() methods.
Example:
// Store a user's token in a session variable. bot.session.set('token', 'my_token'); // Retrieve the user's token from the session variable. const token = bot.session.get('token');
To use file storage, you can use the bot.fs module. This module allows you to read and write files to the bot's file system.
Example:
// Write the user's token to a file. fs.writeFileSync('token.txt', token); // Read the user's token from the file. const token = fs.readFileSync('token.txt');
- If you need to store data for a long period of time, then CosmosDB is a good option.
- If you need to store data for the duration of a user's session, then session variables are a good option.
- If you need to store data for the duration of the bot's execution, then var variables are a good option. - If you need to store a small amount of data that is not sensitive, then file storage is a good option.here are some usefull links to resources on storing user specific data for a Teams bot app:
- Microsoft Docs: Manage state in Bot Framework SDK: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-concept-state?view=azure-bot-service-4.0
- Microsoft Docs: Save user and conversation data: https://dev.to/albertbennett/how-to-save-conversation-data-in-an-microsoft-chatbot-22ml
- Microsoft Docs: Use Cosmos DB to store bot state: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0
- Microsoft Docs: Use memory storage to store bot state: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0
- Microsoft Docs: Use file storage to store bot state: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0
- Microsoft blog post: Use Cosmos DB to store state for your Microsoft Teams bot: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0
- Microsoft blog post: Manage state in your Microsoft Teams bot using the Bot Framework SDK: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-concept-state?view=azure-bot-service-4.0
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic