自定义机器人始终回复错误



我正在尝试从 Teams 发送一个网络钩子,这显然是通过自定义机器人完成的。我能够创建机器人,然后我可以执行@botname stuff并且端点会收到有效负载。

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

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

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

您需要返回一个带有键"text"和"type"的 JSON 响应,如此处的示例所示

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


如果您使用的是 NodeJS,您可以尝试此示例代码

我在 C# 中创建了一个 azure 函数作为自定义机器人的回调,最初发回一个 json 字符串,但这不起作用。最后,我必须设置响应对象的ContentContentType才能使其正常工作(如此处所示(。下面是一个简单的机器人的代码,它回显用户在频道中键入的内容,请随时根据你的方案进行调整。

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

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

最新更新