Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Bot Builder: how to set session data to proactive message?

I first send a proactive message to the user via sms channel inside OAuthCallback method

 var connector = new ConnectorClient();
 Message message = new Message();
 message.From = new ChannelAccount { Id = Constants.botId, Address = "+12312311", ChannelId = "sms", IsBot = true };
 message.To = new ChannelAccount { Id = newUserId, Address = "+18768763", ChannelId = "sms", IsBot = false };
 message.Text = $"How are you doing? ";
 message.Language = "en";
 connector.Messages.SendMessage(message);

 IBotData myDataBag = new JObjectBotData(message);

 myDataBag.UserData.SetValue("Username", "Bob");
 myDataBag.PerUserInConversationData.SetValue("Newuser", "yes");

Then in my main Dialog.cs I try to access it

public static readonly IDialog<string> dialog = Chain
    .PostToChain()            
    .Switch(new Case<Message, IDialog<string>>((msg) =>
    {
        var regex = new Regex("hello$", RegexOptions.IgnoreCase);
        return regex.IsMatch(msg.Text);
    },
    (ctx, msg) =>
    {
        // Clearing user related data upon logout
        string isnewuser = ctx.PerUserInConversationData.TryGetValue("Newuser");
        string username = ctx.UserData.TryGetValue("Username");
        return Chain.Return($"Welcome {username}");
    }))
    .Unwrap()
    .PostToUser();

I receive the message on my phone. However, I am not able to get back the username and newuser session data saved inside OAuthCallback.

I suspect that this is happening because the proactive message does not have conversationId set. And the conversationId must differ somehow.

so how can I get it to set session data to my proactive message in the future conversation?

like image 357
user299709 Avatar asked Aug 30 '25 15:08

user299709


2 Answers

In proactive's scenarios, the conversation Id for channels change when the user answers your message, it's like a new session, we do this type of features using the channel data, but this solution is only for small data, you also have the option of creating a persistent session using the same table storage that the bot framework is using to save the dialog context, in this solution you can create another table to store your data serialized, and the final one is a persistent session using a distributed cache like Redis, but this type of services are expensive, so you have to analyze which type of solution is the right one for your solution, but as a start, you should try with the Channel Data property and if it works, you can analyze another approach

I hope I have been helpful

like image 139
Walter Avatar answered Sep 10 '25 15:09

Walter


Not sure if this is still relevant after four years, but I think I figured this out in Access UserProfile from NotifyBot. Check it out.

like image 21
Zak Avatar answered Sep 10 '25 15:09

Zak