9

I am trying to integrate Microsoft Bot Framework with WhatsApp.

I already have existing bots (skype and webchat) that talk to the bot service and was trying to create a new channel for communication.

How can I construct this new channel?

I already have APIs in place that can send an receive messages from WhatsApp, I plan to hook my bot service when I receive a message from a user, but I can't figure out how to use my bot service as it accepts an a class Activity.

Guido
  • 46,642
  • 28
  • 120
  • 174
user1393503
  • 275
  • 1
  • 4
  • 10

5 Answers5

12

There are a two possibilities to connect a bot to additional channels, using a custom adapter and using the DirectLine API.

My preference would be to use an adapter, since you will have a direct connection to the WhatsApp service which offers a lot of flexibility. However in some scenarios you could benefit from using a proxy service connected to the Direct Line.

In the end, you could use any provider you like, as long as they offer an API. For example providers like Twilio, RingCentral, InfoBip, LivePerson.

My recommended approach: Custom Adapter

  • Bot has a direct connection to the WhatsApp service, using an extra endpoint in the bot
  • Well written adapters transform all Bot Framework SDK activities to the WhatsApp service, and vice versa. (example).
  • Multiple adapters could be used with the same bot project
  • Adapters could offer specific helper functions for the channel, callable from your bot project. (example)

Currently the following adapters for WhatsApp are available in the BotBuilderCommunity.

Examples of how to build a custom adapter can be found on the BotBuilderCommunity. (C#, Javascript)


Alternative approach: Proxy service connected to DirectLine

  • DirectLine API communicates via a self-hosted proxy service to the WhatsApp service
  • Proxy service could be used for C#/NodeJS/Python bots, not language specific
  • Well written proxy services transform all Bot Framework SDK activities to the WhatsApp service, and vice versa.

Example of how to build a proxy service can be found here: C#, Javascript.

Mick
  • 2,946
  • 14
  • 19
4

You can try using the Direct Line as stated in the documentation!

You can enable your own client application to communicate with your bot by using the Direct Line channel.

Ali Heikal
  • 3,790
  • 3
  • 18
  • 24
2

You can try using the Twilio Channel & then using the Twilio API for WhatsApp

KyleMit
  • 30,350
  • 66
  • 462
  • 664
The Memebot
  • 3,199
  • 1
  • 16
  • 21
  • @KyleMit is there a bennefit in using the Twilio Channel instead of just connecting the twilio API for WhatsApp? Because I've connected the API without the channel – The Memebot Apr 01 '20 at 19:46
  • Ooh, sorry, my edit might have been in error. I was trying to add clarity to the necessary steps to get the Twilio API and presumed that you had to route through the Twilio channel to do it. If you can document more about how you've setup and configured to the Twilio API for What's App to work with MS Bot Framework, I'd gladly bounty you some rep – KyleMit Apr 01 '20 at 21:14
  • @KyleMit, In my experience it is not needed, what I did was to configure an endpoint in our code to connect the whatsapp API and then call directline – The Memebot Apr 02 '20 at 18:51
1

I used direct line channel. A relay application needs to be built to interface between whatsapp and microsoft directline.

Mahesha
  • 29
  • 6
1

You can use Twilio Channel and connect using Azure Functions. Here is the code for Azure Function -

[FunctionName("Function1")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
    {
        var dataResponse = await req.Content.ReadAsStringAsync();

        var values = dataResponse .Split('&')
            .Select(value => value.Split('='))
            .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                          pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

        var whatsAppMsg = values["Body"].ToString();
        var qnaAnswer= await evaluateMessage(whatsAppMsg);
        var response = new MessagingResponse().Message(qnaAnswer);

        var twiml = response.ToString();
        twiml = twiml.Replace("utf-16", "utf-8");

        return new HttpResponseMessage
        {
            Content = new StringContent(twiml, Encoding.UTF8, "application/xml")
        };
    }
  • Where evaluate message is calling method for QnA Answer. It can be LUIS implementation as well.

For more information connecting to QnA maker for WhatsApp Bot - Create WhatsApp Bot. Get the source code here.

  • Borderline-spam link answer Please be careful with linking to your own content on different sites, you don't want to be a spammer. You should be including the majority of the content here, and use the link only as a reference. – David Buck May 25 '20 at 17:13
  • I am new to StackOverflow, will be more careful now on. Thanks. Edited the content with more information. – Jagdish Kumawat May 26 '20 at 03:58