Thumbnail
WhatsApp Business API | Integration for Cognigy.AI

Cognigy is a global leader in the Conversational AI marketplace, with a highly-flexible Conversational AI Platform to build advanced, integrated Enterprise Virtual Agents for Customer and Employee Service on Chat and Voice Channels.

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, \uD83D\uDC4B 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, \uD83D\uDC4B 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, \uD83D\uDC4B 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.