BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

Greeting Message Web Chat v4 - Sending events from bot

Open DanielTizon opened this issue 8 years ago • 10 comments

Hi, I am migrating from Webchat v3 to Webchat v4, and I am having problems when I post an event activity for welcome trigger. I was using the workable solution given in official botframework blog:

https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/

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

But when I try to do it in Webchat v4 React version, using the next code:

let directLine = window.WebChat.createDirectLine({ secret: '' });

directLine.postActivity({
                from: user,
                name: 'requestWelcomeDialog',
                type: 'event',
                value: ''
            })
            .subscribe(function (id) {
                console.log('"trigger requestWelcomeDialog" sent');
            });

the event arrives to the bot correctly, but I get a "conversation ended" error in the web browser console, and the messages don't arrive to the client.

Anybody can gimme an example about how to do it correctly in v4?

Thanks in advance!

DanielTizon avatar Nov 26 '18 19:11 DanielTizon

The code looks good to me. Web Chat v4 is still using the very same version of DirectLineJS.

Will investigate the code. But since I am locked down on something else more urgent (#1336), my response may be slow.

p.s. we are drafting PR #1286 to make the "send welcome event" story more standard. You can peek into it for details.

compulim avatar Nov 27 '18 08:11 compulim

We got same error on sending greeting message, with botchatDebug enabled and override DirectLine.end(), we found that seems the DirectLine conversation ended on creating WebSocket connection.

We reverted to v3 since we did not find solution for this problem.

xinfli avatar Nov 27 '18 15:11 xinfli

@xinfli if there are any JavaScript errors inside the business logic of Web Chat, we will close the Direct Line connection. This is a new behavior.

I believe the code that call directLine.postActivity() is causing JavaScript errors in Web Chat. We need to dig into that error.

compulim avatar Nov 28 '18 00:11 compulim

@compulim Thanks, I guess it's caused we transfer customized value in activity and caused this problem, but: 1. same code works fine with v3; 2. in v4 it failed random, sometime it works.

And, how can we catch the error caused the DirectLine connection closed?

Below is our code:

	// ...
	var dl = window.WebChat.createDirectLine({ secret: token });
	var botContainer = document.getElementById('grantBot');

	const styleOptions = {
		// Color options
	};

	window.WebChat.renderWebChat({ directLine: dl, styleOptions }, botContainer);

	var activity = {
		from: {
			id: userId,
			name: userName
		},
		name: 'startConversation',
		type: 'event',
		value: '',
		channelData: {
			"personId": userId,
			"environment": window.location.host
		}
	};

	dl.postActivity(activity).subscribe(function(id) {
		if (console) {
			console.log('"trigger requestWelcomeDialog" sent');
		}
	});
	// ...

xinfli avatar Nov 28 '18 11:11 xinfli

Fix is coming in, #1405. More details over there.

(@xinfli, your code is very useful)

compulim avatar Nov 28 '18 18:11 compulim

Great! waiting for the new release :-)

xinfli avatar Nov 28 '18 20:11 xinfli

It's coming, in Travis CI now.

compulim avatar Nov 28 '18 21:11 compulim

how to send the post activity for any event type from webchatV4? please provide me the exact syntax and dummy code.

Bradrajkumar avatar Aug 12 '19 10:08 Bradrajkumar

I am also looking for. Can u help me with the syntax

selvaduraiece avatar Dec 03 '20 20:12 selvaduraiece

Please see https://github.com/microsoft/BotFramework-WebChat/blob/master/docs/WELCOME_MESSAGE.md

corinagum avatar Dec 03 '20 21:12 corinagum