Rasa in Docker via Telegram

Hi all, I’m trying to deploy an already trained Rasa chatbot using docker-compose with Telegram integration. Previously, I’ve tested it using Ngrok to ensure that all Telegram-related parameters are good and I can send and receive messages through Telegram bot. I also have a subdomain and a server. If I just install my rasa chatbot and run it on the server as is, it won’t work. I believe, this is due to the fact that server listens to 443 port, but Rasa listens on 5005.

This led me to going with Docker, because I can forward ports within compose file. This is how it looks like through docker ps -a:

CONTAINER ID   IMAGE                  COMMAND      CREATED             STATUS          PORTS                                     NAMES
491d675545fc   rasa/rasa:2.7.0-full   "rasa run"   About an hour ago   Up 19 minutes   0.0.0.0:443->5005/tcp, :::443->5005/tcp   ada_bot_rasa_1

Yet my bot is unreachable through Telegram. What can be the issue?

I’ve also run this: curl https://api.telegram.org/bot$BOT_TOKEN/getWebhookInfo

And got this: {"ok":false,"error_code":401,"description":"Unauthorized"}

Please help me figure out how to deploy my bot either with Docker, or without - I don’t really care at this stage, I just want it working on my server.

Are you still using Ngrok?

No, I do not. Because now I have a server with static ip and subdomain attached to it, so there is no need for ngrok type of emulation. I just mentioned ngrok to show that my bot worked with telegram on my local machine through ngrok, but it doesn’t work on my server.

Do you have a certificate? Is it self signed?

No, I don’t have any certificates. Tutorial did not specify that I need one. Should certificate be somehow embedded in Rasa itself? I did a quick glance on Let’sEncrypt tutorials, and it generally requires something like a web-server to serve the certificate. I don’t have that. This server is only for Rasa and rasa-related stuff. So how do I create a certificate and teach Rasa to use it?

Rasa by default works with HTTP, Telegram requires HTTPS. Ngrok makes this transparent.

Download openssl and follow these instructions.

You will need to copy the Telegram channel to your chatbot folder in order to customize it. Replace: channel.setWebhook(self.webhook_url)

with: channel.setWebhook(self.webhook_url, open('my.pem', 'rb'))

And finally follow the instructions of the command line, it will look something like this: rasa run --ssl-certificate my.pem --ssl-keyfile my.key --port 443

The --port 443 is an alternative to the forwarding port.

I did all of that:

  1. created certificate
  2. copy-pasted telegram.py and modified the line you mentioned
  3. added keys to rasa run command to supplement the certificates.

After few hit-and-misses I managed to make it work, but Telegram is still not working for some reason.

Also, I’ve learnt how to change telegram.py and add it in credentials through some of your earlier posts on this forum, cheers!

You are using port 443, one of the four Telegram supports for the webhook (the others are 80, 88 and 8443).

I searched the openssl command that I use:

openssl req -newkey rsa:2048 -sha256 -nodes -keyout my.key -x509 -days 3650 -out my.pem -subj "/C=<Your country>/ST=<Your state>/L=<Your city>/O=<Your company>/CN=<Your IP without the port>"

I don’t have my bot in docker, so I cannot tell you if that’s the cause of the problem.

So I’ve managed to fix it without having to modify Telegram files. Issue is that self-signed certificates are simply not good enough, so you should use Let’sEncrypt and have those certificates as flag when starting Rasa.

Do not use openssl, as it won’t work.