Questions about saving bot state - botframework

I read through this https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-state about saving state data. I have some questions regarding the same:-
Lets take a bot exposed through the browser channel as an example here:-
What is the lifetime of the data stored? For instance when the bot saves data using context.ConversationData.SetValue(..) Is the data purged when the session is over(when the user refreshes the page)?
The From object from Activity has Id and Name. Are these generated by the channel each time a chat session begins? For instance, if I was chatting with bot then refresh the web page, now will my Id and Name have changed?
Same question about conversation. If I refresh the page and begin the conversation again, do I get a new conversation ID?
I read in some blog that if you use dialogs, the dialog stack state is automatically saved in whichever storage you have configured. Is this correct? If so, why? Say I refresh the page, will I be able to retrieve the state of the dialog stack and resume the conversation from there?
If you are giving code samples, request you to give C# samples if possible
Thanks very much in advance!

Hi, I hope below answer will find useful to you :
1.
What is the lifetime of the data stored? For instance when the bot saves data using context.ConversationData.SetValue(..) Is the data
purged when the session is over(when the user refreshes the page)?
ANS:->
As per the guidelines by Bot Framework, State API is in the state of depreciation. you will have to use your own state management service to maintain the state of your Bot. Ref: https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-state
So assuming you are using Table Storage or SQL Database for storing your bot in this case the data will persist as long as your storage account and database available.
2.
The From object from Activity has Id and Name. Are these generated
by the channel each time a chat session begins? For instance, if I
was chatting with bot then refresh the web page, now will my Id and
Name have changed?
ANS:->
This depends on how you initialize the chat. for instance, if you are using
Web chat : It will be empty id.
Skype : It will be skype id and user name
DirectLine : you can define your own id and name as per your need.
3.
Same question about the conversation. If I refresh the page and begin the conversation again, do I get a new conversation ID?
ANS:->
Yes. Every time you refresh your webpage you will be assigned new conversationId but in case of DirectLine you can use the previous conversation id to maintain the history of your conversation. you can store the conversation id in local storage or in browser's cookies and read whenever you feel to load the chat history. If you don't need history to be preserved then I suggest let webchannel handle its own ids.
4.
I read in some blog that if you use dialogs, the dialog stack state is automatically saved in whichever storage you have configured. Is this correct?
ANS:->
Yes.
5.
If so, why? Say I refresh the page, will I be able to retrieve the state of the dialog stack and resume the conversation from there?
ANS:->
As hinted earlier, you will need to migrate your bot to use DirectLine API instead of webchat channel. As webchat doesn't support history so DirectLine.
Please refer the guidelines provided by Microsoft and Samples provided on GitHub.
https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-concepts

Quick answers:
As far as I know, there is no purge. And you can check the implementations of Azure DocumentDbBotDataStore or TableBotDataStore here. Based on my implementations, I saw there is a Timestamp column in the stored data so you could do cleanup based in that.
Generation of Id and Name fields (for From, but also for Recipient given the message origin / destination): yes they depend on the channel. I made a detailed answer about on SO: Bot Framework User Identification
Yes in the webchat's case
Yes, the dialog stack state is saved so that you can continue your conversation. "Say I refresh the page, will I be able to retrieve the state of the dialog stack and resume the conversation from there?": if you have the same details (channelId + conversationId, userId) you should yes. The exception is the webchat / directLine where you have to implement the fact that you keep the same IDs. For other channels like Slack, Facebook Messenger etc, these items remains the same and the dialog can continue where it stops on the previous messages exchange

Related

Can I create a configuration page for a Teams bot app?

I'm building my first Teams app which will have two primary functions:
Proactively send a message to the channel (the bot is installed into) when a specific event occurs on my backend.
Members of the channel reacts to the message via actions.
I finally have a pretty good idea of how to set this up (I think) - but one part I'm missing is that in order to identify the specific app installation as belonging to one of my customers, I need to be able to allow the installing user to supply extra information like e.g. an API-key so that I can associate the specific channel with my specific customer.
Is there any way of doing this with a bot app? I've found examples for creating a configuration page, but they all seem to be associated with tab apps?
I could of cource have the bot ask the user for the information - but maybe there's a "cleaner" way?
Any examples or tutorials would be greatly appreciated as I find it rather hard to get stuff working using Microsoft's own examples etc. :)
Thanks a lot!
When you receive any message from the user, either by typing to your bot, or even installing it into a channel, group chat, or personal context (where you get the conversationUpdate event), you are able to get specific details off of the activity object. If the user sends a message, for instance, then the text property on the activity object will have a value. Incidentally, this is the same activity you will use to get the conversation details you need for the Proactive message.
With regards your question, the activity class also includes a tenantId property, hanging off the conversation property. This is the unique Microsoft 365 Id for the tenant, which would be what I'd suggest to uniquely identify them for your API, or licensing, or similar.

Microsoft Teams Redirect URL to a Thread or Group Conversation

I've been using the next URL to redirect any user to their private chat with our MS Teams App:
https://teams.microsoft.com/l/chat/0/0?users=28:b9cc7986-dd56-4b57-ab7d-9c4e5288b775
with b9cc7986-dd56-4b57-ab7d-9c4e5288b775 being the App Id of out bot (sample App Id in this case).
This URL will open the private MS Teams Chat with the user clicking the URL and our App on any client (Mobile, Desktop, Browser) without opening any web pages if not needed. The clients can handle the URL themselves. You can try it with this provided sample URL.
What would be a proper way to be able to redirect to a group conversation or thread?
I've thought about using the Conversation Id (or other Ids available in our MS Teams App) or something but couldn't find any good documentation or examples online.
You can use Deep Links to accomplish that, specifically deep linking to a chat. As shown in that link, you can link to specific users, but also set a conversation topic as well as an initial message. One thing that's often overlooked, just fyi, is this sentence:
New chats are created in draft state until the user sends the first message

How to update an existing activity in WebChat

My scenario is: I send card attachments each with an AcceptButton to users. When 1 user clicks on that button, ideally I want to disable all similar buttons (of the same offer) for all users. I am storing conversation details of each user, and I know the ActivityId of each message-with-said-button (it follows a certain string format).
My issue is similar to this and this but for WebChat not Teams.
I did try those solutions but I got the error "Method not allowed" for both UpdateActivityAsync() and DeleteActivityAsync(). Then I read here that UpdateActivity is not supported in WebChat. (But might be available in the future?)
I would like any visual indication that the offer is no longer available. (Right now, they receive an "Offer was already accepted by {UserX}" which would still be in place on top of changing the card/message.)
Is there a way to do this via backChannel? I can trigger an event and pass the ActivityId (tested by showing a simple alert()) but how do I apply changes to that specific activity?
Per this comment in Web Chat's source code and this open issue in the Web Chat repository for adding support for deleteActivity and updateActivity, Update Activity events are not currently supported in Web Chat. Unfortunately, there is currently no way to really update an activity in Web Chat.
Hope this helps!

How can I add a middleware logger to my bot?

I tried to add telemetry services following this tutorial, but I looked at my logs in azure and I didn't see user messages. How can I add a middleware that can save user's conversations?
What I'm trying to do is to capture the messages that LUIS and QnA could not find an answer for. So we want to log these so we can improve our bot in the future.
I know LUIS has this feature already in luis.ai, but we want to add more information than what LUIS captures.
You have to do a custom implementation for storing user conversations. I have used Azure Table in one of my implementations to store the conversation. The table structure will have the conversation id + user id as the unique key and then storing the conversation in json format in the table. The conversation data is updated by intercepting every message sent to and from the bot. You can refer this MSDN article for a reference implementation.

Sending context information to chatbot

While launching a web chat bot from a portal (through iframe), is it possible to send the details of the logged in user (who has already logged into the portal) to the bot and save it in the bot state so it can retrieved inside the bot code to do some customization or apply custom logic based on the user details. User details can include their name, date of birth, gender etc.
Your web app and bot can exchange information "behind the scenes" (i.e., invisible to the end-user) by exchanging activities of type event. An activity with type event will not be displayed by clients such as Web Chat, so the end user won't see any evidence of the communication. This type of communication between client and bot is sometimes referred to as the "backchannel mechanism."
For an example of how this is done, check out Ryan Volum's backchannel sample bot.

Resources