Botframework-webchat: Cannot access the bot user data on backchannel post activity

Created on 27 Jun 2017  Â·  5Comments  Â·  Source: microsoft/BotFramework-WebChat

Hello,
We want to post a ConversationUpdate activity using the backchannel when the WebChat load.
The ConversationUpdate is triggered correctly using the code below, but the from.id received by the bot is different to what is sent and therefore the user bot data store is different.

I tried setting the user and from properties on both the bot connection and post method, but neither is working.

How can we access the same bot user data store on the ConversationUpdate event and BotChat App?
Note: The bot user data store available on ConversationUpdate and messages is the same for all other channels we have tried, so this seems like a WebChat issue.

var user = {
    id: userId,
    name: "You"
};

var bot = {
    id: environment.botId,
    name: "Bot"
};

var botConnection = new BotChat.DirectLine({
    secret: environment.directLineSecret,
    user: user
});

console.log("Trigger conversation update");
botConnection
    .postActivity({ type: "ConversationUpdate", user: user, from: user })
    .subscribe(id => console.log("Conversation updated"));

console.log("Intialize BotChat App");
BotChat.App({
    botConnection: botConnection,
    bot: bot,
    user: user,
    resize: 'detect'
}, document.getElementById("BotChatGoesHere"));

Thank you,

Douw

Most helpful comment

ConversationUpdate is a synthetic activity sent by the bot framework connector. You shouldn't send it from the client. I would suggest sending an Event activity instead. That's how Backchannel works.


From: Douw Loots notifications@github.com
Sent: Monday, June 26, 2017 10:05:10 PM
To: Microsoft/BotFramework-WebChat
Cc: Subscribed
Subject: [Microsoft/BotFramework-WebChat] Cannot access the bot user data on backchannel post activity (#536)

Hello,
We want to post a ConversationUpdate activity using the backchannel when the WebChat load.
The ConversationUpdate is triggered correctly using the code below, but the from.id received by the bot is different to what is sent and therefore the user bot data store is different.

I tried setting the user and from properties on both the bot connection and post method, but neither is working.

How can we access the same bot user data store on the ConversationUpdate event and BotChat App?
Note: The bot user data store available on ConversationUpdate and messages is the same for all other channels we have tried, so this seems like a WebChat issue.

`var user = {
id: userId,
name: "You"
};

var bot = {
id: environment.botId,
name: "Bot"
};

var botConnection = new BotChat.DirectLine({
secret: environment.directLineSecret,
user: user
});

console.log("Trigger conversation update");
botConnection
.postActivity({ type: "ConversationUpdate", user: user, from: user })
.subscribe(id => console.log("Conversation updated"));

console.log("Intialize BotChat App");
BotChat.App({
botConnection: botConnection,
bot: bot,
user: user,
resize: 'detect'
}, document.getElementById("BotChatGoesHere"));`

Thank you,

Douw

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMicrosoft%2FBotFramework-WebChat%2Fissues%2F536&data=02%7C01%7CBill.Barnes%40microsoft.com%7C73be23d1c72f4859325108d4bd1a1848%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636341367174834476&sdata=Ch0InFOGNTmhl86dZnfFdvCXDajV58a6J871cxvP9Ko%3D&reserved=0, or mute the threadhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAKiet--nOVGLI5wlEdHC4Uq9jnhx2NScks5sII2GgaJpZM4OGJhn&data=02%7C01%7CBill.Barnes%40microsoft.com%7C73be23d1c72f4859325108d4bd1a1848%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636341367174834476&sdata=rwez1XGZ%2F4FReZ%2BO7%2FMysDAMslwUX2dZ6WcUYuoW4%2Fk%3D&reserved=0.

All 5 comments

ConversationUpdate is a synthetic activity sent by the bot framework connector. You shouldn't send it from the client. I would suggest sending an Event activity instead. That's how Backchannel works.


From: Douw Loots notifications@github.com
Sent: Monday, June 26, 2017 10:05:10 PM
To: Microsoft/BotFramework-WebChat
Cc: Subscribed
Subject: [Microsoft/BotFramework-WebChat] Cannot access the bot user data on backchannel post activity (#536)

Hello,
We want to post a ConversationUpdate activity using the backchannel when the WebChat load.
The ConversationUpdate is triggered correctly using the code below, but the from.id received by the bot is different to what is sent and therefore the user bot data store is different.

I tried setting the user and from properties on both the bot connection and post method, but neither is working.

How can we access the same bot user data store on the ConversationUpdate event and BotChat App?
Note: The bot user data store available on ConversationUpdate and messages is the same for all other channels we have tried, so this seems like a WebChat issue.

`var user = {
id: userId,
name: "You"
};

var bot = {
id: environment.botId,
name: "Bot"
};

var botConnection = new BotChat.DirectLine({
secret: environment.directLineSecret,
user: user
});

console.log("Trigger conversation update");
botConnection
.postActivity({ type: "ConversationUpdate", user: user, from: user })
.subscribe(id => console.log("Conversation updated"));

console.log("Intialize BotChat App");
BotChat.App({
botConnection: botConnection,
bot: bot,
user: user,
resize: 'detect'
}, document.getElementById("BotChatGoesHere"));`

Thank you,

Douw

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMicrosoft%2FBotFramework-WebChat%2Fissues%2F536&data=02%7C01%7CBill.Barnes%40microsoft.com%7C73be23d1c72f4859325108d4bd1a1848%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636341367174834476&sdata=Ch0InFOGNTmhl86dZnfFdvCXDajV58a6J871cxvP9Ko%3D&reserved=0, or mute the threadhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAKiet--nOVGLI5wlEdHC4Uq9jnhx2NScks5sII2GgaJpZM4OGJhn&data=02%7C01%7CBill.Barnes%40microsoft.com%7C73be23d1c72f4859325108d4bd1a1848%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636341367174834476&sdata=rwez1XGZ%2F4FReZ%2BO7%2FMysDAMslwUX2dZ6WcUYuoW4%2Fk%3D&reserved=0.

The event activity does have access to the user bot data and I was able to trigger a "fake" conversation update using below:

WebChat:
botConnection .postActivity({ type: "event", from: user, name: "ConversationUpdate", value: "" }) .subscribe(id => console.log("Conversation updated"));

Bot
if (context.Activity.Type == ActivityTypes.Event) { IEventActivity eventActivity = context.Activity.AsEventActivity(); if (eventActivity.Name == "ConversationUpdate") ...

Yup, that's the way we designed it. Glad it's working for you.

Hello @billba, I wanted to check with you how I implemented this to make sure I'm doing it correctly, in my case, the webchat is receiving a query string param which contains a UUID for a user, then I need the bot to greet the user by name (Bot initiates the greeting), so I need to load the user profile from a certain repository to get firstname/lastname based on the UUID received.
I ended up doing the below, my concern if is it ok to send connector.Conversations.ReplyToActivityAsync(reply) when the activity type is an Event?

Here is the code I used in the Web Chat JavaScript:

function initBotChat(token) {

            var userId = getParameterByName('userid');

            if (!userId) {
                userId = generateId();
            }
            var userName = getParameterByName('username');

            if (!userName) {
                userName = 'You';
            }
            var user = {
                id: userId,
                name: userName
            };

            var bot = {
                id: "botid"
            };

            var botConnection = new BotChat.DirectLine({
                token: token,
                webSocket: false
            });

            postUserIdEventMessage(botConnection,user);

            BotChat.App({
                botConnection: botConnection,
                user: user,
                bot: bot,
                resize: 'detect',
                locale: 'de-DE',
                speechOptions: speechOptions,
            }, document.getElementById("bot"));

        }

        function postUserIdEventMessage(botConnection,user) {
            botConnection
                .postActivity({
                    from: user,
                    name: 'setUserIdEvent',
                    type: 'event',
                    value: ''
                })
                .subscribe(function (id) {
                    console.log('"triggerPostUserIdEvent" sent');
                });
        };

And here is the code I used in the MessagesController to send the Welcome message from the bot to user upon loading the Web Chat page:

private async Task HandleSystemMessage(Activity message)
        {
            await TelemetryLogger.TrackActivity(message);

            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {

            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }
            else if (message.Type == ActivityTypes.Event)
            {
                var eventActivity = message.AsEventActivity();

                if (eventActivity.Name == "setUserIdEvent")
                {
                    ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));

                    var userId = message.From.Id;

                    TelemetryLogger.LogTraceTelemetry($"TriggerCustomEvent::From Controller::message.From.Id: {userId} message.Recipient.Id: {message.Recipient.Id}", SeverityLevel.Warning);

                    var userProfile = await GetUserProfileAsync(userId);

                    await UserProfileHelper.StoreUserProfileAsync(message, userProfile);

                    var welcomeMessages = GreetingHelper.FormatWelcomeMessages(userProfile);

                    foreach (var welcomeMessage in welcomeMessages)
                    {
                        var reply = message.CreateReply(welcomeMessage);

                        await connector.Conversations.ReplyToActivityAsync(reply);

                    }

                }
            }
        }

Let me know if you have any comments on this approach, please?

First rule of programming: if it works, then it works.

I'm not very familiar with the C# SDK but from what I can tell this is the pattern I was espousing. My main feedback is that I'd use a switch statement instead of all those if/elses :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corinagum picture corinagum  Â·  3Comments

compulim picture compulim  Â·  3Comments

corinagum picture corinagum  Â·  3Comments

filipjakov picture filipjakov  Â·  4Comments

adriantan08 picture adriantan08  Â·  3Comments