Bot framework get the ServiceUrl of embedded chat control page

I think a way to achieve this would be by using BackChannel, which adds the ability for a bot to communicate with a page that embeds the bot through WebChat. It will a allow you to:

  • Send events to a page that hosts an instance of a WebChat
  • Listen for events from the page that hosts an instance of a WebChat

The first piece is, of course, the HTML page, which will contain what you put together, plus the logic to send/listen to events. The sample page with the basic logic can be found here and below is the image with the events related code.

BackChannel events

Now, you need to prepare your bot to listen and send events. There is a sample in Node.js, that shows how to do that.

Porting that in C#, is as easy as listen and sending to activities of type Event. A sample code for that (using the events of the HTML page mentioned before):

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Event &&
        string.Equals(activity.Name, "buttonClicked", StringComparison.InvariantCultureIgnoreCase))
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        // return our reply to the user
        Activity reply = activity.CreateReply("I see that you just pushed that button");
        await connector.Conversations.ReplyToActivityAsync(reply);
    }

    if (activity.Type == ActivityTypes.Message)
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        // return our reply to the user
        var reply = activity.CreateReply();
        reply.Type = ActivityTypes.Event;
        reply.Name = "changeBackground";
        reply.Value = activity.Text;
        await connector.Conversations.ReplyToActivityAsync(reply);
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

Bottom line, in your HTML page you will have to send an event to the bot, with the page URL and the bot will have to listen to that event to get the value

Leave a Comment