首页 > 解决方案 > 在 nodejs 中的 azure bot rest API 中的 post call 后无法在 Get 调用中接收正确的响应

问题描述

机器人信息

问题描述

我正在使用 node.js 在我的应用程序中使用 Azure bot rest API。当我在 POST 调用(从机器人发送活动)之后调用 GET 方法(从机器人接收活动)时,我没有收到当前活动的响应。它总是显示旧的响应。但是,如果我在 POST 和 GET 之间放置延迟功能,那么它工作正常。有什么我想念的吗?以下是我在代码中的其余 API 调用的结构。

代码示例

调用 1:GET - 获取对话 ID。
调用 2:POST - 从机器人发送活动。
睡眠(1000)函数。
调用 3:GET - 从机器人接收活动

代码

callStep1();
function callStep1(){
    console.log("Step 1 Start ...");
    Request.post({
        "headers": { "content-type": "application/json" , "Authorization":authBearer },
    "url"  : conversationURL,
}, (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    if(body !=null && body.length > 0){
    var conversationJSON = JSON.parse(body);
    var conversation_Id  = conversationJSON.conversationId;

    console.log("Conversation ID created and received from MS BOT: " + conversation_Id);

    var activityURL = directline + conversation_Id +"/activities/";
    console.log("Activity URL: " + activityURL);
    console.log("Step 1 completed ...");  
    callStep2(conversation_Id,activityURL);
    }else{
        console.log("Step 2 No body response recieved from the boat ..."); 
    }
});
}

Step 2 get the conversation ID

function callStep2(conversation_Id,activityURL){
console.log("Step 2 Start ...");
Request.post({
"headers": { "content-type": "application/json" , "Authorization":authBearer 
},
"url"  : activityURL,
"body" : JSON.stringify({
    "type": "message",
    "text": textMessage,
    "from": {
      "id": "default-user",
      "name": "Ashish"
    },
    "locale": "en-US",
    "textFormat": "plain",
    "timestamp": new Date().toISOString(),
    "channelData": {
      "clientActivityId": "1"
    },
    "id": "lc9ikcikllnj",
    "recipient": {
      "id": "default-bot",
      "name": "Bot"
    },
    "conversation": {
      "id": conversation_Id
    },
    "serviceUrl": "URL"
  }),
}, (error, response, body) => {
if(error) {
    return console.dir(error);
}
var activityLength = Object.keys(JSON.parse(body)).length;
var jsonObj = JSON.parse(body);

console.log("step-2: 1: " + body);
console.log("step-2: 2: " + activityLength);
var id = JSON.parse(body).id;
console.log("step-2: 3: " + id);
//sleep(5000);
console.log("Step 2 completed ...");

callStep3(id,activityURL)
console.log("Step 2 completed ..." + callStep3(id));
});
}

Calling sleep to make some delay while calling step 3

function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
  if ((new Date().getTime() - start) > milliseconds){
    break;
}
}
}

Step 3 get the conversation Answer from BOT

function callStep3(id,activityURL){
sleep(1000);
console.log("Step 3 start ...");
var botMessage = "";
Request.get({
"headers": { "content-type": "application/json" , "Authorization":authBearer },
"url": activityURL
}, (error, response, body) => {
if(error) {
    return console.dir(error);
}
if(body !=null && body.length > 0){
var activityLength = Object.keys(JSON.parse(body).activities).length;
var jsonObj = JSON.parse(body);

console.log("step-3: body: " + body);
console.log("step-3: activityLength: " + activityLength);

for (i = 0; i < activityLength; i++) {
    if(jsonObj.activities[i].replyToId !=null){
         if(jsonObj.activities[i].replyToId == id){
            botMessage = jsonObj.activities[i].text;
             console.log("step-3: bot text message: " + botMessage);
             break;
         } 
     }
 }
}
else{
    console.log("Step 3: No body received from bot");    
}
 console.log("Step 3 completed ...");
 return botMessage;
},
)

预期行为

一旦我发布了活动,我应该会通过立即 GET 电话收到正确的答案。

标签: node.jsapiazurebotframeworkazure-bot-service

解决方案


在步骤 1 中,当您收到conversation_id.

请尝试在您的步骤 1中修改var activityURL = directline + conversation_Id +"/activities/";为 。var activityURL = directline + 'conversations/' + conversation_Id + "/activities/";

正确的 url 应该是这样https://directline.botframework.com/v3/directline/conversations/{conversationId}/activities的,更多信息请参考https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0 -send-activity?view=azure-bot-service-3.0


推荐阅读