【问题标题】:Is there a way to wait on the conversation to be updated in the microsoft botframework?有没有办法等待微软机器人框架中更新的对话?
【发布时间】:2019-06-02 16:17:48
【问题描述】:

我已经制作了一个聊天机器人,它在 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 秒。

【问题讨论】:

  • 我希望你能成功!如果您觉得我的回答足够,请“接受”它,以便我可以从我的支持跟踪器中清除这张票。如果没有,请告诉我我还能提供哪些帮助!

标签: angular botframework


【解决方案1】:

我建议您查看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)
);

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多