1
votes

The Bot Framework WebChat control isn't working with the Token protocol, as described in the Embed the Chat Control docs. Here's the code I'm having trouble with:

        string webChatSecret = ConfigurationManager.AppSettings["WebChatSecret"];


        var request = new HttpRequestMessage(HttpMethod.Post, "https://webchat.botframework.com/api/conversations");
        request.Headers.Add("Authorization", "BOTCONNECTOR " + webChatSecret);

        HttpResponseMessage response = await new HttpClient().SendAsync(request);
        string responseJson = await response.Content.ReadAsStringAsync();
        WebChatTokenResponse webChatResponse = JsonConvert.DeserializeObject<WebChatTokenResponse>(responseJson);

        return $"<iframe width='400px' height='400px' src='https://webchat.botframework.com/embed/PigLatinBotJoeMayo?t={webChatResponse.Token}'></iframe>";

the WebChatTokenResponse is

public class WebChatTokenResponse
{
    public string ConversationID { get; set; }

    public string Token { get; set; }
}

When I debug and hit a breakpoint, I do have a ConversationID and a Token. No exceptions are being thrown.

If I just use the secret, like this (instead of the code above, everything works:

        string webChatSecret = ConfigurationManager.AppSettings["WebChatSecret"];
        return $"<iframe width='400px' height='400px' src='https://webchat.botframework.com/embed/PigLatinBotJoeMayo?s={webChatSecret}'></iframe>";

Here are the error messages I'm seeing with F12 tools:

I'm seeing a 500 Internal Server Error on

Request URL: https://webchat.botframework.com/api/conversations

with the error message:

{ "message": "An error has occurred." }

If I enter a 'hi' message to:

Request URL: https://webchat.botframework.com/api/conversations/null/messages

I receive a 403 Forbidden message with the response:

{ "message": "Invalid token or secret" }

Update

I blogged about how I got this working at Using the Bot Framework Chat Control.

1

1 Answers

2
votes

This is a bug we recently introduced and are fixing now.

We also have a doc bug -- there are two ways to retrieve a token and the other way will work better when used with our web chat embed. Instead of POSTing to /api/conversations, which creates a conversation and gets a token, you can just GET /api/tokens (with your secret in the header like before) and you'll get a token but won't start a conversation. The web chat control does the work of starting the conversation on its own, so this prevents us from starting conversations from the bot in the event the control isn't actually loaded. The /api/tokens call will be working likely later today.