3

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

但是,机器人立即回复“抱歉,您的请求遇到问题”。如果我将“回调 URL”指向 requestb.in url 或者将其指向我的端点,我会收到此错误。这让我怀疑机器人正在期待来自端点的一些特定响应,但这没有记录在案。我的端点以 202 和一些 json 响应。Requestb.in 以 200 和“ok”响应。

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

上面的那个链接提到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.但是没有指示如何满足这个请求,除非自定义机器人需要同步回复。

4

1 回答 1

3

您需要返回带有键“文本”和“类型”的 JSON 响应,如此处的示例所示

{
"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;
}
于 2017-08-23T18:57:32.253 回答