在Node.js中的Azure Bot Rest API中调用后,无法在Get调用中收到正确的响应

时间:2018-07-04 06:20:32

标签: node.js api azure botframework azure-bot-service

启动信息

  • SDK平台:Node.js
  • SDK版本:3.0
  • 有效渠道:直线等
  • 部署环境:Azure Bot服务,使用其余API的Azure App Service。

问题说明

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

代码示例

呼叫1:GET-获取对话ID。
呼叫2:POST-从机器人发送活动。
Sleep(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调用获得正确答案。

1 个答案:

答案 0 :(得分:1)

在收到conversation_id时,在步骤1中,您尚未构造正确的URL格式来发送活动。

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

正确的网址应类似于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