eflorespalma
eflorespalma

Reputation: 345

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

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#)

Upvotes: 2

Views: 582

Answers (1)

Bill Barnes
Bill Barnes

Reputation: 342

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!

Upvotes: 1

Related Questions