node.js - 在 nodejs 中的 azure bot rest API 中的 post call 后无法在 Get 调用中接收正确的响应
问题描述
机器人信息
- SDK平台:Node.js
- SDK版本:3.0
- 活动渠道:直线等
- 部署环境:Azure Bot 服务、使用 REST API 的 Azure 应用服务。
问题描述
我正在使用 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 电话收到正确的答案。
解决方案
在步骤 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
推荐阅读
- mysql - 选择从当前周的周一到下周日的日期
- cassandra - Cassandra 驱动程序中是否有支持 Java 二维字符串数组的编解码器?
- c# - 如何使用 Azure devops CI/CD 部署 .net core windows 服务?
- sql - 有没有办法为 Azure SQL Server 设置默认的 Azure SQL 数据库定价层
- vue.js - 在 v-list-item 中嵌入(子)组件
- android-studio - 将 Flutter 包发布到本地 .m2 或私有 Nexus-Repository
- c# - 为一个表中的嵌套数据创建简化查询 (.NET Core)
- angular - 角 | 如何动态更改 ngTemplateOutlet 以实现响应目的
- javascript - 提交表单后如何保留切换复选框状态?
- ubuntu - 在远程服务器上运行 jupyter notebook 时如何使 julia 内核可用