1
votes

I am trying to connect to a bot Service using an Angular Client. I am able to connect to the Bot Service, but as soon as I post a message Activity, the WebChat client is sending a conversationUpdate event to the Bot, due to which I receive a Sign In Card from the Bot. (If I send userToken in channelData of firstMessage to Bot Service from client, i will not get the Sign In card).

I am trying to send a activity with userToken inside channelData, but the conversationUpdate event reaches Bot Service before my message activity and I receive a Sign In card.

I need to send custom channelData with the conversationUpdate event being sent from Client.

Using backchannel mechanism, i am able to send custom channelData for posting acctivities, but this conversationUpdate event is being triggered internally from websocket and I need to intercept this event trigger.

Following are the code details:

Index.html

<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<script src="https://unpkg.com/simple-update-in/dist/simple-update-in.production.min.js"></script>

app.component.ts


// after response from https://directline.botframework.com/v3/directline/conversations
createDirectLine(response: any) {
        this.directLine =  window.WebChat.createDirectLine({
            token: response.token,
            webSocket: true,
            streamUrl: response.streamUrl,
            conversationId: response.conversationId
          });
}

this.store = window.WebChat.createStore({},
            ({dispatch}) => next => action => {
                    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
                     // add custom channelData to every postActivity
                     action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'UserToken'], function () { return this.userToken; });
                    }
                    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
                        // Send event to bot with custom data
                        dispatch({
                          type: 'WEB_CHAT/SEND_EVENT',
                          payload: {
                            activity: {
                                type : 'conversationUpdate',
                                channelData : { 'UserToken : this.userToken}
                                }
                            }
                        })
                    }
                  return next(action);
                });

renderWebChat() {
        window.WebChat.renderWebChat(
            {
                directLine: this.directLine,
                store: this.store
            },
            this.botWindowElement.nativeElement
        );
}

P.S. This is'nt the full code, I have only added snippets.

1
It looks like your code is trying to send its own conversationUpdate activity. Is your bot getting both the conversationUpdate activities from Direct Line and the conversationUpdate activity from your client?Kyle Delaney
I was trying to send my own conversationUpdate event with custom data inside channelData, but it did not work out. I removed that code snippet from my application. The bot is getting only one conversationUpdate event from Directline as soon as I send a message activity from client.Murtaaz Khan
What do you mean when you say it did not work out? Do you mean your bot did not receive your custom conversationUpdate, or do you mean your bot didn't respond the way you wanted it to?Kyle Delaney
Hey Kyle, I meant that the bot Service did not receive my custom conversationUpdate event sent from my client application. Currently we have made a fix in Bot Service, to send Sign In card only in response to 'message' event. But am not sure whether this is the correct approach to handle this scenario.Murtaaz Khan
Yes, thanks for the support :)Murtaaz Khan

1 Answers

1
votes

You should not be trying to use Web Chat to directly manipulate the conversation update activities spawned by Direct Line. You can influence those activities by providing a user ID when you create the conversation, but the rest is out of your hands. It's probably not a good idea to try to embed the token in the user ID, but you might be able to host the token somewhere the bot could look it up using the user ID.

I think what you really want to do is to send an event activity to the bot that indicates a successful connection, and then use that instead of a conversation update. You already seem to be on the right track because you're trying to respond to DIRECT_LINE/CONNECT_FULFILLED actions in your middleware. Just try following the sample here: https://github.com/microsoft/BotFramework-WebChat/tree/master/samples/04.api/a.welcome-event