How can I let users share their location in Bot Framework webchat channel?

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I read that you can share location with the backchannel.

I was thinking to use this code to talk directly to the bot:

function postButtonMessage() {         botConnection.postActivity({             entities:{type: "ClientCapabilities", requiresBotState: true, supportsTts: true, supportsListening: true},             from: { id: 'userid', name: 'username' },             name: 'botname',             type: 'message',             value: 'Hi',             textFormat: 'plain'           })           .subscribe(function (id) {             console.log('"buttonClicked" sent');           });       };

But I get an error saying "bad gateway 502", but when I talk through the web channel windows it works perfectly so I know that the direct line key is configured correctly. And when I use the type: event instead of message it works fine and I don't have problems, so I am confused about that.

(Question originally asked at https://github.com/Microsoft/BotFramework-WebChat/issues/778#)

回答1:

There are two approaches here.

One is to use the backchannel to send an event activity to the bot with whatever data you like. You could do this when a button is clicked, at regular intervals, or whenever the location changes.

var dl = new BotChat.DirectLine({secret});  BotChat.App({     botConnection: dl,     // other Chat props go here });   function postButtonMessage() {     dl.postActivity({         from: { id: 'userid', name: 'username' },         type: 'event',         name: 'location',         value: { /* location goes here */ }       })     .subscribe(id => {         console.log('"buttonClicked" sent');     }); }; 

The other is to use client middleware to send that data with every message, by intercepting and modifying each message as it goes out. The advantage to this approach is that each message is 'tagged' with its location. The disadvantage is that you only get location updates when the user sends a message.

var dl = new BotChat.DirectLine({secret});  BotChat.App({     botConnection: {         ... dl,         postActivity: activity => dl.postActivity({             ... activity,             channelData: { location: /* location goes here */ }         })     },     // other Chat props go here }); 

And of course you could do both!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!