39

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?

CSDev
  • 3,177
  • 6
  • 19
  • 37
user299709
  • 4,922
  • 10
  • 56
  • 88
  • Not a solution, but if I recall correctly, the conversationId does not need to be set when sending the proactive message. The framework will check to see if one exists. If the conversationId exists it sends that message to that conversation, if it doesn't a new conversation is started. [source](http://docs.botframework.com/connector/new-conversations/#navtitle) Also, are you doing this through the emulator or do you have your bot in Azure? I am trying to send proactive messages through the emulator and am getting a 500 status code. – pl0x Jun 21 '16 at 14:27
  • @pl0x it won't work in emulator. I'm more wondering how to set a session data on the convo started by the proactive message, which doesn't seem to work in this case. – user299709 Jun 21 '16 at 19:31
  • once I get my bot up on Azure I will let you know if I find out how to set the session data. – pl0x Jun 21 '16 at 19:47
  • 3
    We now have a [sample](https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-CreateNewConversation) for creating new conversations. – Will Portnoy Sep 30 '16 at 22:12
  • I done this once, see here https://github.com/DanielHWe/NotifyBot – Daniel W. Oct 21 '19 at 12:38

2 Answers2

1

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

Walter
  • 11
  • 3
1

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.

Zak
  • 724
  • 4
  • 18