3

我正在使用 DirectLineJS 通过网站从自定义网络聊天中进行交流。我正在使用从微软的 github https://github.com/Microsoft/BotFramework-DirectLineJS发布的格式

我是如何实现的

var directLine;
    directLine= new DirectLine.DirectLine({
        secret: "My_DirectLine_Secret",
    });

    function sendReceiveActivity(msg) {
        document.getElementById("inputtext").value = "";
        conversation.innerHTML = conversation.innerHTML + "ME - " + msg + "<br/><br/>";

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

        directLine.activity$
            .filter(activity => activity.type === 'message' && activity.from.id === 'mybot')
            .subscribe(
            message => console.log(message)"
            );
    }

每当我开始阅读消息时,每条消息的副本数都会来回增加一,因此我的网站将经历这个循环:

我 - 向机器人发送消息

BotReply - 消息 1

我 - 向机器人发送一些消息

BotReply - 消息 2

BotReply - 消息 2

我 - 一些信息

BotReply - 消息 3

BotReply - 消息 3

BotReply - 消息 3

等等

对于重复的消息,我从机器人收到的响应 ID 也不会增加,所以说 msg 3 的 ID = 00005,每个 BotReply - msg 3 的 ID = 00005,但 msg 4 的 ID = 00007

在我的实际机器人中,我通过使用发送消息await context.PostAsync("Some mesage");,仅此而已

我能做些什么来减少消息回复只收到一个?

文档指出“Direct Line 将有助于向您的客户发送每个已发送活动的副本,因此一个常见的模式是过滤来自以下的传入消息:”即使我正在过滤来自“mybot”的消息

4

1 回答 1

2

如果不查看其余代码,很难准确确定发生了什么。但是,每次发送消息时,您似乎都在订阅接收消息。请尝试更改您的代码,以便您只订阅一次:

var directLine = new DirectLine.DirectLine({
        secret: "My_DirectLine_Secret",
    });

directLine.activity$
          .filter(activity => activity.type === 'message' && activity.from.id === 'mybot')
          .subscribe(
            message => console.log(message)
            );

    function sendReceiveActivity(msg) {
        document.getElementById("inputtext").value = "";
        conversation.innerHTML = conversation.innerHTML + "ME - " + msg + "<br/><br/>";

        directLine.postActivity({
            from: { id: 'myUserId', name: 'myUserName' }, // required (from.name is optional)
            type: 'message',
            text: msg
        }).subscribe(
            id => console.log("Posted activity, assigned ID ", id),
            error => console.log("Error posting activity", error)
            );
    }
于 2017-05-03T21:24:35.107 回答