如何让用户在Bot Framework网络聊天频道中分享他们的位置?

时间:2017-11-10 16:30:11

标签: botframework

我读到你可以与反向频道共享位置。

我正在考虑使用此代码直接与机器人交谈:



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');
          });
      };




但是我得到一个错误说"糟糕的网关502",但是当我通过网络频道窗口进行通话时,它工作得很好,所以我知道直接线路密钥配置正确。当我使用类型:event而不是message时,它工作正常而且我没有问题,所以我对此感到困惑。

(最初在https://github.com/Microsoft/BotFramework-WebChat/issues/778#询问的问题)

1 个答案:

答案 0 :(得分:1)

这里有两种方法。

一种方法是使用 backchannel event活动发送到机器人,并提供您喜欢的任何数据。单击按钮,定期或按位置更改时,可以执行此操作。

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');
    });
};

另一种方法是使用客户端中间件通过在每个消息发出时拦截和修改每个消息来发送该数据。这种方法的优点是每条消息都被标记为'与它的位置。缺点是您只能在用户发送消息时获得位置更新。

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
});

当然,你可以做到这两点!