有没有办法等待在Microsoft botframework中更新对话?

时间:2019-05-05 18:35:22

标签: angular botframework

我已经创建了一个聊天机器人,并且在Messenger中可以正常工作,现在我正在创建自己的界面,但是我在回复时遇到了一些麻烦。我正在使用Microsoft BotFramework,可以发送和接收消息,但无法通过订阅工作。因此,现在我总是必须等待几秒钟,否则不能保证将漫游器的回复添加到对话中。 订阅的结果只是已发送消息的ID,而不是聊天机器人的响应消息/答案。

我尝试了订阅,但超时解决了它。有没有人可以帮助我解决这个问题?

我将消息发送到对话,并在订阅中得到响应,我期望它的工作方式如下:

this.http.post(this.conversationUrl, body, {headers: this.headers}).subscribe(
        res =>{
        this.getMessage()
        }
    )

获得对话(getMessage()):

this.http.get(this.conversationUrl, {headers: this.headers}).subscribe()

要“解决”该问题,我使用了超时,删除了订阅中的getMessage()调用,并在发送消息以进行对话之后等待了4秒:

this.chatbotService.sendMessage(newMessage);

setTimeout(() => {
    var result = this.chatbotService.getMessage()
    this.messages.push(result);
    window.scrollTo(0, document.body.scrollHeight);
   }, 4000);

我希望已回复的机器人/将其添加到对话后会立即收到机器人的答复。如果只需要半秒钟,我不想等待4秒钟。

1 个答案:

答案 0 :(得分:0)

我建议您看一下BotFramework-DirectlineJs npm软件包。您可以使用该程序包初始化与机器人的对话,发送活动以及订阅来自机器人的传入消息。

初始化DirectLine实例

import { DirectLine } from 'botframework-directlinejs';
// For Node.js:
// const { DirectLine } = require('botframework-directlinejs');

var directLine = new DirectLine({
    secret: /* put your Direct Line secret here */,
    token: /* or put your Direct Line token here (supply secret OR token, not both) */,
    domain: /* optional: if you are not using the default Direct Line endpoint, e.g. if you are using a region-specific endpoint, put its full URL here */
    webSocket: /* optional: false if you want to use polling GET to receive messages. Defaults to true (use WebSocket). */,
    pollingInterval: /* optional: set polling interval in milliseconds. Default to 1000 */,
});

发布活动

directLine.postActivity({
    from: { id: 'myUserId', name: 'myUserName' }, // required (from.name is optional)
    type: 'message',
    text: 'a message for you, Rudy'
}).subscribe(
    id => console.log("Posted activity, assigned ID ", id),
    error => console.log("Error posting activity", error)
);

订阅活动流

directLine.activity$
.filter(activity => activity.type === 'message')
.subscribe(
    message => console.log("received message ", message)
);

希望这会有所帮助!