3

I am trying to send a webhook out from Teams, which is apparently accomplished with a Custom Bot. I am able to get the bot created and then I can do @botname stuff and the endpoint receives a payload.

However, the bot immediately replies with "Sorry, there was a problem encountered with your request". I get this error if I point the "Callback URL" to a requestb.in url or if I point it to my endpoint. This leads me to suspect the bot is expecting some specific response from the endpoint, but this isn't documented. My endpoint responds with a 202 and some json. Requestb.in responds with a 200 and "ok".

So, is it true that the bot requires a specific response payload and if so what is this payload?

That link above mentions Your custom bot will need to reply asynchronously to the HTTP request from Microsoft Teams. It will have 5 seconds to reply to the message before the connection is terminated. But there is no indication of how to satisfy this request, unless the custom bot will need to reply synchronously.

nomadic_squirrel
  • 614
  • 7
  • 21
  • I suspect it doesn't like the `204`. Have you tried pushing back a simple `200`? A `204` implies the sever accepted the request but won't be returning any content. – Marc LaFleur Aug 21 '17 at 20:53
  • Yea, that is my suspicion as well, but I have no control over the return payload. This web service processes the requests asynchronously. The response is a uuid for the request for tracking, but I don't really need to capture this in Teams. – nomadic_squirrel Aug 21 '17 at 22:56
  • @MarcLaFleur, Sorry, it's actually a 20 (post updated). The request is processed asynchronously, processed and then we'll make a call back to Teams with a follow up message. Technically ALL of the 200 response codes are success so Teams should consider them a success. – nomadic_squirrel Jan 05 '18 at 17:24

1 Answers1

5

You need to return a JSON response with the keys 'text' and 'type' as shown in the example here

{
"type": "message",
"text": "This is a reply!"
}


In case you are using NodeJS, you can try this sample code

I created an azure function in C# as the callback for the custom bot and was initially sending back a json string but that didnt work. Finally I had to set the response object's Content and ContentType to get it working (as shown here). Here is the code for a simple bot that echoes back what the user types in the channel, feel free to adapt it to your scenario.

Custom MS Teams bot example code using azure functions

#r "Newtonsoft.Json"
using System.Net;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();
    log.Info(JsonConvert.SerializeObject(data));
    // Set name to query string or body data
    name = name ?? data?.text;
    Response res = new Response();
    res.type = "Message";
    res.text = $"You said:{name}";
    var response = req.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(JsonConvert.SerializeObject(res));
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    return response;
}

public class Response {
    public string type;
    public string text;
}
S Raghav
  • 1,386
  • 1
  • 16
  • 26
  • This is a good answer, but not the answer I wanted to hear :) I can't change the format of the response payload. I think the documentation is a little vague, "Example response" to me is not a "defined" response. The doc implies you *could* respond with that value not that you *must*. – nomadic_squirrel Aug 24 '17 at 22:40
  • @nomadic_squirrel thats what I thought initially. Attempts at sending just a string back failed and hence I fell back on the superstition of just following the example exactly. Maybe the vague documentation is a way of making us use custom bots less and the bot framework more :) ;) – S Raghav Aug 27 '17 at 15:13