Connect WhatsApp to Cognigy.AI

Alexander
Teusz

Originally published by Alexander Teusz of Cognigy on Medium.

6 min read

I would bet that you also use WhatsApp for your daily conversations, don’t you? It wouldn’t be surprising… There are up to 2 billion daily users worldwide, which places the chat application in the third place of most used social media platforms on this planet (Manish, 2020).

Furthermore, a user study found out that 9 of 10 participants use WhatsApp regularly (Tyntec, 2019). For this reason, one may think about delivering their Cognigy Conversational AI through this channel as well.

That’s exactly what we will do today! Connecting Cognigy.AI to WhatsApp.

Okay, fine. If you already played around with the Cognigy.AI platform you may have considered that there is no native WhatsApp channel connector (Endpoint) yet. However, with the help of Tyntec this challenge can be completed very easily since they offer simple to use communication APIs for Messaging, Chat Apps, Number Data, and Authentication. In order to finally have a nice chat with our AI, a few steps need to be followed:

1. Create a Tyntec Account

2. Set up a WhatsApp Business Account and connect it to Tyntec

3. Inside Cognigy, define an Endpoint Transformer function to prepare the AI’s output to be displayed in WhatsApp

Create an Endpoint Transformer Function

After steps 1 and 2 are completed, our Cognigy output needs to be converted into a valid WhatsApp message. Therefore, this tutorial uses the following two Cognigy project resources:

Please add a simple SAY node to your flow and insert a nice welcome message:

Hi, 👋 I am your personal WhatsApp assistant. How can I help you?

In the REST endpoint, this message now will be converted. In order to do so, navigate to the Endpoints section and click on your recently created REST endpoint. If you click on the Transformer Functions expansion panel, it opens the detail view to define our transformer:

We need to enable all Transformer Settings to continue with our tutorial. Four clicks later, take a brief look at the code below the Transfomer headline:

createRestTransformer({  handleInput: async ({ endpoint, request, response }) => {    const userId = "";
    const sessionId = "";
    const text = "";
    const data = {}    return {
      userId,
      sessionId,
      text,
      data
    };
  },  handleOutput: async ({ output, endpoint, userId, sessionId }) => {    return output;
  },  handleExecutionFinished: async ({ processedOutput, outputs,     userId, sessionId, endpoint, response }) => {    return processedOutput;
  }
});

The root function createRestTransformer() contains three more functions that are provided to manipulate incoming and outgoing data content: handleInput(), handleOutput() and handleExecutionFinished(), in which the last one is the one we will implement in the next step.

As can be seen in the Tyntec Chat API, WhatsApp requires the following input information in order to send a simple text message:

"whatsapp": {
  "from": "545345345",
  "contentType": "text",
  "text": "Hi, 👋 I am your personal WhatsApp assistant. How can I help you?"
}

Inside our transformer, we have to send this information to the Tyntec API:

return await httpRequest({
  uri: "https://api.tyntec.com/chat-api/v2/messages",
  method: "POST",
  headers : {
    'Content-Type':'application/json',
    'Accept':'application/json',
    'apikey': '098765434567'
  },
  body: {
    "to": sessionId,
    "channels": [
      "whatsapp"
    ],
    "whatsapp": {
      "whatsapp": {
        "from": "545345345",
        "contentType": "text",
        "text": "Hi, 👋 I am your personal WhatsApp assistant. How can I help you?"
      }   
    }
  },
  json: true
});

This HTTP post request needs our valid Tyntec API key, the current Cognigy session information and the message object including the receiving mobile number.

Test the Connection

Nothing more is needed to publish our AI through WhatsApp. Now, write a message to your Tyntec number using WhatsApp and wait for the Cognigy.AI response.

If everything went well, you should see something like this:

It works! We’re finished.

Thank you for reading this little tutorial about Cognigy and Tyntec. Have fun and enjoy your new connection! 😎🥳

Take a look at the complete transformer function here:

Cognigy/TransformersSamples (github.com)

More from tyntec

Customer Service
How Enterprises Can Use Viber Business to Enhance Customer Experience
Manuela
Marques
4 min read
Customer Service
How to Increase Your WhatsApp Presence
Manuela
Marques
5 min read
Conversational Commerce
Sticking Power: 6 Reasons for Leveraging Branded 'Stickers' in Business Messaging
Adam
Phillips
4 min read