node.js - 向机器人执行 POST 时出现 502(错误网关)

标签 node.js botframework direct-line-botframework

我正在构建一个非常简单的交互式 BOT,仅使用 HTTP-GET 和纯 JS。 有时我让 BOT 进行时间密集型处理,并需要 40 秒才能回复。在这种情况下,我会收到以下 POST 响应。

  1. 那么,这个返回是预期的吗?

  2. 我需要进行哪些更改才能收到有意义的响应,而不会将这种情况视为真正的错误?

  3. 对于处理这种情况还有其他建议吗?

谢谢!

502 (Bad Gateway)
{
  "error": {
    "code": "ServiceError",
    "message": "Failed to send activity: bot returned an error"
  }
}

发布请求

//send token and message
function sendMessage() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {


    if (this.readyState == 4 && this.status == 200) {

      var responseObj = JSON.parse(this.responseText);
      convMsgID =responseObj.id;
      latestUserMessage = document.getElementById('textToSend').value;
      showUserMessage();
      document.getElementById('textToSend').value = "";
      getReply();
    }
    else{
      console.log("error :"+ this.responseText);
    }

  };
  var postUrl = "https://directline.botframework.com/v3/directline/conversations/" + convID + "/activities";
  xhttp.open("POST", postUrl, true);

  var authToken="Bearer " + convToken;
  xhttp.setRequestHeader("Authorization", authToken);
  xhttp.setRequestHeader("Content-Type", "application/json");



  var messageBody = '{"type": "message","from": {"id": "user1"},"text": "';
      messageBody =  messageBody + document.getElementById("textToSend").value;
      messageBody = messageBody + '"}';

  console.log("messageBody"+ messageBody);
  xhttp.send(messageBody);
  document.getElementById("send-icon").src="./send.png";

}

获取请求

    function getReply() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {



    if (this.readyState == 4 && this.status == 200) {

      var responseObj = JSON.parse(this.responseText);
      console.log("length" + (responseObj.activities.length -1));
      console.log("response :"+ responseObj.activities[responseObj.activities.length -1].text);
      latestBotMessage = responseObj.activities[responseObj.activities.length - 1].text


      showBotMessage();
    }
    else{
        console.log("response"+ this.responseText);
    }

  };
  var postUrl = "https://directline.botframework.com/v3/directline/conversations/" + convID + "/activities";

  xhttp.open("GET", postUrl, true);

  var authToken="Bearer " + convToken;
  xhttp.setRequestHeader("Authorization", authToken);
 xhttp.send();

}

最佳答案

1) 是的,如果机器人回复时间超过 15 秒,则预计会出现 502

2 & 3) 在开始长时间运行的进程之前响应用户。让某些辅助作业执行长时间操作(Azure Function?),并在完成后将结果作为主动消息发送给用户:https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-proactive-messages

关于node.js - 向机器人执行 POST 时出现 502(错误网关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818392/

相关文章:

salesforce - 有没有办法将 Microsoft Bot Framework 切换集成到 Salesforce Live 代理

c# - Direct Line API - 机器人框架/英雄卡和附件

node.js - 如何让机器人启动1 :1 conversation in a group with the user in Slack using Microsoft bot-framework?

javascript - Botframework-WebChat - 为什么它不能与 Internet Explorer 一起使用?

c# - Azure Bot Framework 自定义状态 cosmosdb 数据为空

node.js - 如何在 NodeJS 中使用生成的随机数并在不调用数据库的情况下进行验证?

node.js - Node js : I to get the output of an (child) exec and set a variable to be retuned?

node.js - 获取用户 ID 后,Firebase 函数 onCreate 方法无法在 Firestore 上运行

node.js - WebSocket 在本地、AWS 上工作,但在阿里巴巴云上不工作

botframework - Direct Line API - 机器人框架