我正在尝试从 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.但没有指示如何满足此请求,除非自定义机器人需要同步回复。
您需要返回带有键“text”和“type”的 JSON 响应,如此处示例所示
{
"type": "message",
"text": "This is a reply!"
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 NodeJS,您可以尝试以下示例代码:
我在 C# 中创建了一个 azure 函数作为自定义机器人的回调,最初发送回 json 字符串,但这不起作用。最后,我必须设置响应对象Content并ContentType使其正常工作(如此处所示)。以下是一个简单机器人的代码,它会回显用户在频道中输入的内容,请随意调整它以适应您的场景。
使用 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3155 次 |
| 最近记录: |