Search code examples
botframework

Handling Webchat with existing NodeJS Bot


I was building this bot using NodeJS. I was testing it with Skype and Embedded on a web page using an iFrame, everything was working fine. I was trying to achieve something, and here advised me that I should do it using DirectLine in order to use postActivity once the user is connected to the chat to send them a message when they connect.

Now when am reading the docs about the DirectLine API, it seems like it's a totally different thing from what am doing, and the docs aren't showing the differences or the "When to use" DirectLine.

For example, in my NodeJS application, am just following the NodeJS SDK with the dialogs, session.send(), session.sendTyping(), etc..

My question is: does it mean when I want to use DirectLine that I need to replace this whole NodeJS bot code with Activities? Am confused about how I would use DirectLine along with my an existing application. What will happen with the Skype channel? Do I replace everything with activities? Would I be using session.sendTyping or activities for that?


Solution

  • An activity is just the term used for any event that has been processed by one of the bot framework channels. Your bot only ever speaks in activities, so no changes are needed to the serverside code in order to enable directline to communicate with your bot properly.

    What you might need to change is the utilization of the iframe. Webchat is powered by a directline channel by default, but since the iframe is an all in one embed and it's not really possible to access the directline.

    That user is asking you to use this method which looks something like this:

    botConnection = new BotChat.DirectLine({secret: "<secret>"});
    
    BotChat.App({
         botConnection:  botConnection,
         user: { id: 'User', name: 'user'},
         bot: { id: 'botid' },
         resize: 'detect'
       }, document.getElementById("bot"));
    

    to initialize your bot in your website, in order to enable the following backchannel:

    botConnection
      .postActivity({
        from: { id: '<user>' },
        value: "text",
        type: 'event',
        name: "ConversationUpdate"
      })
      .subscribe(id=> console.log("stuff" + id))
    

    However if you are having trouble enabling a welcome message from the frontend, consider following this blogpost on the topic, which explains utilizing listeners on serverside for user connection events. Regardless, you should be OK without needing to rewrite anything in terms of trying to enable support for activities, as they are already being utilized.