代码之家  ›  专栏  ›  技术社区  ›  nomadic_squirrel

自定义Bot总是回复错误

  •  3
  • nomadic_squirrel  · 技术社区  · 9 年前

    Custom Bot @botname stuff 端点接收有效载荷。

    然而,机器人立即回复“对不起,您的请求遇到了问题”。如果我将“回调URL”指向requestb,就会出现这个错误。或者如果我将其指向我的端点。这让我怀疑bot正在期待来自端点的特定响应,但这没有记录在案。我的端点用202和一些json进行响应。请求B。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. 但是没有指示如何满足此请求,除非自定义bot需要同步回复。

    1 回复  |  直到 8 年前
        1
  •  5
  •   S Raghav Diego Torres Milano    9 年前

    您需要返回一个带有键“text”和“type”的JSON响应,如示例所示 here

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


    如果您正在使用NodeJS,您可以尝试 this sample code

    Content ContentType 使其工作(如图所示 here ). 这是一个简单的机器人程序的代码,它可以回应用户在频道中键入的内容,请根据您的场景随意调整。

    使用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;
    }