自定义机器人总是回复错误

Custom Bot always replies with an error

我正在尝试从 Teams 发送一个 webhook,这显然是通过 Custom Bot 完成的。我能够创建机器人,然后我可以执行 @botname stuff 并且端点接收有效负载。

但是,机器人会立即回复 "Sorry, there was a problem encountered with your request"。如果我将 "Callback URL" 指向 requestb.in url 或者如果我将它指向我的端点,我会收到此错误。这让我怀疑机器人正在期待来自端点的一些特定响应,但这没有记录在案。我的终端响应 202 和一些 json。 Requestb.in 以 200 和 "ok" 响应。

那么,机器人是否真的需要特定的响应负载?如果是,这个负载是什么?

上面link提到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.但是没有说明如何满足这个请求,除非自定义bot需要同步回复。

您需要 return 使用键 'text' 和 'type' 的 JSON 响应,如示例 here 所示

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


如果您使用的是 NodeJS,您可以尝试

我在 C# 中创建了一个 azure 函数作为自定义机器人的回调,最初发回了一个 json 字符串,但没有成功。最后,我必须设置响应对象的 ContentContentType 才能使其正常工作(如图 所示)。这是一个简单机器人的代码,它可以回显用户在频道中输入的内容,您可以根据自己的情况随意调整它。

使用 Azure 函数的自定义 MS Teams 机器人示例代码

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