DirectLineJS receiving copies of Bot replies

2019-07-10 00:35发布

问题:

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"

回答1:

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