Search code examples
angularbotframework

Is there a way to wait on the conversation to be updated in the microsoft botframework?


I've made a chatbot and this works fine in Messenger, now I'm making my own interface, but I'm having some trouble with the replies. I'm using the Microsoft BotFramework and sending and receiving the messages is possible, but I couldn't get it working with a subscribe. So now I always have to wait for a couple of seconds or it is not guaranteed that the reply of the bot is added to the conversation. The result of the subscribe is just the id of the message that was send and not the responsemessage/answer of the chatbot.

I've tried a subscribe and I 'solved' it with a timeout. Is there anyone that can help me with this problem?

I send the message to the conversation and in the subscribe I get the response, the way I expected it to work is like this:

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

To get the conversation (getMessage()):

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

To 'solve' the problem I used a timeout, removed the call of getMessage() in the subscribe and waited 4seconds after the message was send to get the conversation:

this.chatbotService.sendMessage(newMessage);

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

I expect to receive the answer of the bot as soon as it has replied/ as soon as it is added to the conversation. I don't want to wait for 4 seconds if it only takes half a second.


Solution

  • I would recommend taking a look at the BotFramework-DirectlineJs npm package. You can use the package to initialize a conversation with a bot, send activities, and subscribe to incoming messages from the bot.

    Initializing a DirectLine instance

    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 */,
    });
    

    Posting an Activity

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

    Subscribing to Activity Stream

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

    Hope this helps!