Bot Info
- SDK Platform: Node.js
- SDK Version: 3.0
- Active Channels: Direct Line, etc.
- Deployment Environment: Azure Bot Service, Azure App Service using rest APIs.
Issue Description
I'm using Azure bot rest APIs in my application using node.js. When I'm calling GET method(to receive an activity from the bot) after POST call(to send an activity from the bot), I'm not receiving the current activity's response. It is displaying the old response always. But if I'm putting delay function between the POST and GET then it's working fine. Is there something I'm missing?? following are my rest API call's structure in the code.
Code Example
Call 1: GET - to get conversation ID.
Call 2: POST - to send an activity from the bot.
Sleep(1000) function.
Call 3: GET - to receive an activity from the bot
Code
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;
},
)
Expected Behavior
Once I have posted the activity I should receive the correct answer for that though immediate GET call.