Request demo
Free Trial
  • Apr 29, 2020
  • 10 Min

Connect WhatsApp to Cognigy.AI

written by:Alexander Teusz
Connect Cognigy.AI To WhatsApp with Tyntec
 

I would bet that you also use WhatsApp for your daily conversations, don’t you? It wouldn’t be surprising… There are 1.6 million daily users worldwide, which places the chat application in the third place of most used social media platforms on this planet (Mehner, 2020). 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;
}
});

As can be seen in the Tyntec Chat API, WhatsApp requires the following input information in order to send a simple text message:The root function  contains three more functions that are provided to manipulate incoming and outgoing data content: ,  and , in which the last one is the one we will implement in the next step.

 

"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.

Set the Cognigy Endpoint in Tyntec

Before the AI can be tested, the configured Cognigy endpoint needs to be connected to the Tyntec WhatsApp for Business configuration. In order to so, the following API request needs to be done, at the moment:

Type: PATCH
Url: https://api.tyntec.com/chat-api/v2/applications/default
Headers:
- apikey (Tyntec API Key)
Body:
{
"webhooks": [
{
"events": [
"MoMessage"
],
"callbackUrl": "YOUR ENDPOINT URL"
},
{
"events": [
"MessageStatus::deleted",
"MessageStatus::accepted"
],
"callbackUrl": "YOUR ENDPOINT URL"
}
]
}

You can find your Tyntec API key under https://my.tyntec.com/api-settings. The request will not return any kind of data information but the 204 No Content success message. Now your AI is ready to be tested.

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 Cogngiy and Tyntec. Have fun and enjoy your new connection! 😎🥳

Take a look at the complete transformer function here.

 

Related Articles

  • Jun 15, 2020
  • 10 Min
  • Apr 27, 2020
  • 10 Min
  • Apr 22, 2020
  • 10 Min