DirectLineJS收到Bot回复的副本 [英] DirectLineJS receiving copies of Bot replies

查看:79
本文介绍了DirectLineJS收到Bot回复的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DirectLineJS通过网站从自定义网络聊天中删除内容.我正在使用从Microsoft的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不会重复发送,因此说味精3的ID = 00005,每个BotReply-味精3的ID = 00005,但是味精4的ID = 00007

在我真正的机器人中,我使用await context.PostAsync("Some mesage");和其他任何东西

发送消息

我怎样做才能减少仅收到一封邮件的答复?

文档指出"Direct Line会向您的客户端发送所有已发送活动的副本,因此,即使我正在过滤来自"mybot"的消息,一种常见的模式是从以下位置过滤传入消息:

解决方案

很难在不看其余代码的情况下准确确定正在发生的事情.但是,似乎您每次发送消息时都在订阅接收消息.请尝试更改代码,以便只订阅一次:

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)
            );
    }

I'm using DirectLineJS to comminucate from a custom webchat through a website. I'm using the format posted from Microsoft's github https://github.com/Microsoft/BotFramework-DirectLineJS

how I have it implemented is

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)"
            );
    }

whenever I start reading the messages the number of copies increase by one through each message back and forth so the my website will go through this cycle:

Me - send message to bot

BotReply - msg 1

Me - send some message to bot

BotReply - msg 2

BotReply - msg 2

Me - some message

BotReply - msg 3

BotReply - msg 3

BotReply - msg 3

and so on

the response ID I receive from the bot doesn't increase either for repeated messages so say msg 3 had ID = 00005, each of the BotReply - msg 3 has ID = 00005, but msg 4 would be ID = 00007

In my actual bot I send my messages by using await context.PostAsync("Some mesage");and nothing else

what can I do to reduce the message replies to receive just one?

The documentation states "Direct Line will helpfully send your client a copy of every sent activity, so a common pattern is to filter incoming messages on from:" even though I am filtering my messages to be from "mybot"

解决方案

It is difficult to determine exactly what is going on without seeing the rest of the code. However, it appears as if you are subscribing to receive messages every time you send a message. Please try changing your code so that you only subscribe once:

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)
            );
    }

这篇关于DirectLineJS收到Bot回复的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆