botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

CreateDirectConversationAsync - how to set BotState?

Open AlexanderKovalev opened this issue 9 years ago • 5 comments

I'm using external service to initiate conversation with user from Bot. In this service I set BotState, but in bot it contains different values.

My code: `
var connector = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials()); var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount); Activity message = (Activity)Activity.CreateMessageActivity(); message.From = botAccount; message.Recipient = userAccount; message.Conversation = new ConversationAccount(id: conversationId.Id); message.ChannelId = channelId;

        message.Text = text;
        message.Locale = "en-Us";

        try
        {
            message.ServiceUrl = serviceUrl;

            var stateClient = message.GetStateClient();
            var botData = new BotData(eTag: "*");
            botData.SetProperty(stateKey, stateValue);
            await stateClient.BotState.SetUserDataAsync(message.ChannelId, message.From.Id, botData);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Trace.TraceError(ex.ToString());
        }

        await connector.Conversations.SendToConversationAsync(message);`

Where stateValue - custom class with simple properties (one enum(translates to int), one bool, one string values).

In Emulator I see that state was set correctly, but when I receiveing this state in bot - it contains different data.

Please advise, what am I doing wrong?

AlexanderKovalev avatar Feb 03 '17 22:02 AlexanderKovalev

Anyone?

AlexanderKovalev avatar Feb 07 '17 07:02 AlexanderKovalev

You set message.From = botAccount but then you use message.From.Id when calling SetUserDataAsync - shouldn't it be the user ID?

willportnoy avatar Feb 17 '17 02:02 willportnoy

hm... I think it should work, will check it later! Thank you!

AlexanderKovalev avatar Feb 17 '17 15:02 AlexanderKovalev

Can't check because of this error: https://github.com/Microsoft/BotBuilder/issues/2294

AlexanderKovalev avatar Feb 17 '17 20:02 AlexanderKovalev

I'm going to close this issue for now (as it looks like the other issue has been solved), but feel free to open a new issue for further discussion.

willportnoy avatar Feb 28 '17 03:02 willportnoy