在 nodejs 中的 azure bot rest API 中调用 post 后无法在 Get 调用中收到正确的响应
Unable to receive correct response in Get call after post call in azure bot rest API in nodejs
机器人信息
- SDK 平台:Node.js
- SDK 版本:3.0
- 活跃渠道:直线等
- 部署环境:Azure Bot Service、Azure App Service using rest APIs.
问题描述
我在使用 node.js 的应用程序中使用 Azure bot rest APIs。当我在 POST 调用(从机器人发送 activity 之后调用 GET 方法(从机器人接收 activity)时,我没有收到当前的 activity的回应。它始终显示旧响应。但是,如果我在 POST 和 GET 之间放置延迟函数,那么它就可以正常工作。
有什么我想念的吗?
以下是我在代码中的其余 API 调用结构。
代码示例
调用 1:GET - 获取对话 ID。
呼叫 2:POST - 从机器人发送 activity。
休眠 (1000) 函数。
调用 3:GET - 从 bot
接收 activity
代码
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;
},
)
预期行为
一旦我发布了 activity 我应该立即收到正确的 GET 电话。
您没有构建正确的 URL 格式来在第 1 步收到 conversation_id
时发送 activity。
请尝试修改var activityURL = directline + conversation_Id +"/activities/";
为
var activityURL = directline + 'conversations/' + conversation_Id + "/activities/";
在您的第 1 步中。
正确的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
机器人信息
- SDK 平台:Node.js
- SDK 版本:3.0
- 活跃渠道:直线等
- 部署环境:Azure Bot Service、Azure App Service using rest APIs.
问题描述
我在使用 node.js 的应用程序中使用 Azure bot rest APIs。当我在 POST 调用(从机器人发送 activity 之后调用 GET 方法(从机器人接收 activity)时,我没有收到当前的 activity的回应。它始终显示旧响应。但是,如果我在 POST 和 GET 之间放置延迟函数,那么它就可以正常工作。 有什么我想念的吗? 以下是我在代码中的其余 API 调用结构。
代码示例
调用 1:GET - 获取对话 ID。
呼叫 2:POST - 从机器人发送 activity。
休眠 (1000) 函数。
调用 3:GET - 从 bot
代码
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;
},
)
预期行为
一旦我发布了 activity 我应该立即收到正确的 GET 电话。
您没有构建正确的 URL 格式来在第 1 步收到 conversation_id
时发送 activity。
请尝试修改var activityURL = directline + conversation_Id +"/activities/";
为
var activityURL = directline + 'conversations/' + conversation_Id + "/activities/";
在您的第 1 步中。
正确的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