简体   繁体   中英

502(Bad gateway) when doing a POST to the bot

I am building a very simple interactive BOT using only HTTP-GET and plain JS. Sometimes I have the BOT do time intensive processing and takes 40s to reply back. In such cases I get the below response for the POST.

  1. So, is this return expected?

  2. What changes do I make so that a meaningful response is received, and not consider this situation as a genuine error?

  3. Any other suggestions to deal with this scenario?

Thank you!

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

Post request

//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";

}

GET request

    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) Yes, a 502 is expected if the bot takes more than 15 seconds to reply

2 & 3) Respond to the user before starting the long running process. Have some worker job perform the long operation (Azure Function?) and after it completes, send the user the result as a proactive message: https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-proactive-messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM