How to connect Chatwork with Rasa, and 5 minutes to chant latency in the sky.

Tram Ho

TL; DR : Here is the code. https://github.com/ngoctnq-1957/rasa-chatwork-echo

Opening

If you are a chatbot like me, you probably used Rasa. With superior advantages such as completely local without fear of losing information, a good dialog handler and connectors (even if catching the entity is a bit stupid), Rasa is the number 1 choice of projects that need security / or Need everything in 1 package. Also, if you work at a company that uses Chatwork like you, you will need to find a way for Chatwork to contact Rasa so that it can replace you to reply to the boss’s message. ? So I will guide your people to do this. Bonus way to make a chatbot to parody what you say.

Note: this post uses Rasa <1.8.0 because it is compatible with TensorFlow 1.x, due to experience that the TF2.0 version is still crumbling.

How to get Rasa to parody you

I have to build a basic bot to demo for you, anyway, so I recommend: the parody action you will define in the actions.py file:

And you leave the default is to run that action in config.yml :

Over ?

How to get Rasa connected to Chatwork

The first is that you define the webhook to receive incoming messages for Rasa.

The structure of extend InputChannel classes requires the following methods: starting with name , will determine your webhook URL.

For my example to return chatwork , the URL would be /webhooks/chatwork/webhook .

Next is the method to get the token settings in the credentials.yml file. This section will also be discussed later in this section.

Next is an optional method, but I put in to remove the To / Reply types from the input message to clean it.

The most important piece of code is the blueprint for Rasa’s sanic server. In particular, Rasa requires you to implement 2 routes that are / with the method name health , and webhook with the method name receive .

To avoid someone sneaking your request into your webhook (not from Chatwork), you need to check that the message is from Chatwork.

Basically, the request’s header sent to the webhook will include the hash of the message content, signed with your secret token. Compare ok is ok ?

And the focus of this article is webhook. The code has some pretty basic callbacks.

And finally the optional method, used to create OutputChannel so you can return messages to the user.

Then you define what the return message will look like.

Basically, this class only fires one POST request on the Chatwork server in the right syntax into the right room. If you want to add a cool original sender reply, correct it as follows:

Remember to change the code to create the ChatworkOutput object in the ChatworkInput class.

Next, you need to install the necessary APIs.

Both can be accessed from the API Setting section of the Chatwork:

With the API key, enter the password in the following field and you will get the flag:

For webhook secret token, click Webhook and create a new entry:

Where the secret token is the Token entry, as I highlighted. In addition, you need to fill in the Webhook URL according to the structure you have put in the image. For example, if you run Rasa and don’t have any proxy port pass (as with nginx ), that link would be

Finally, you need to install the API settings.

Go to the credentials.yml file and add these 3 lines to the bottom

with the token values ​​taken from the previous step.

And the problem of Chatwork Webhook

Not to mention the Chatwork changing theme blinded me, the Chatwork webhook sometimes runs extremely bad. Typically, I tried sending a message and it took 5 minutes for Rasa to arrive ? To prove that this is not a problem for Rasa or the internet, I tried to replay Chatwork’s payload into the webhook, and everything went as normal as never had a breakup:

In the file chatwork_connector.py as above, in the receive function of Sanic blueprint, please edit a bit to get the Chatwork payload (5 ‘after you send):

From there, you already have the payload to replay the attack server ? Don’t worry, you can’t be hacked like this in real life, because Chatwork requires HTTPS so you won’t have a Man-in-the-Middle (MITM) attack like you are doing right now. Turn on Postman and send it to the webhook URL – remember to include the header to validate your message:

It only takes 1.5 seconds, not 5 minutes, you know. I also fired this request from my computer to the EC2 server used to host this bot, so it was not latency localhost.

Conclude

If you can, get your boss to change to Slack. If you can’t, try to bear with it, and use this code to make your life a little easier. ? Thank you for reading this, and good luck.

Share the news now

Source : Viblo