Tyntec: Deploying a WhatsApp Endpoint

tyntec.png

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.

 

With the help of Tyntec a WhatsApp connection can be provided very easily since they offer simple to use communication APIs for Messaging, Chat Apps, Number Data, and Authentication. Therefore, the following steps need to be followed:

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

tyntecWhatsAppDashboard.png

3. Inside Cognigy, one needs to 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, the 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:

RestEndpointTransformer.png

One needs to enable all Transformer Settings to continue with our tutorial. Four clicks later, a brief look at the code below the Transfomer headline can be taken:

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 the transformer, one has 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 a 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"
}
]
}

One can find their 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 the 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:

tyntecWhatsAppMessage.jpeg

Take a look at the complete transformer function here: 

https://github.com/Cognigy/Transformers/tree/master/endpoint/rest/whatsapp


Literature:


Comments

0 comments

Please sign in to leave a comment.

Was this article helpful?
0 out of 0 found this helpful