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.

1 Like

Hi, I have also created certificates for my subdomain. When curl from: https://api.telegram.org/bot$BOT_TOKEN/getWebhookInfo, I get result: 200 back. However, when I send a message in Telegram, the message does not go through. Have you had similar problems? I have an ubuntu vm on a server and rasa running in docker container there.

@nik202 Du you have any experiences how to attach SSL certificates? when I run ‘rasa run --ssl-certificate fullchain.pem --ssl-keyfile priv.key --endpoints endpoints_local.yml --credentials credentials_local.yml --connector addons.custom_channel.MyIO --debug’ I get: rasa/lib/python3.8/site-packages/rasa/server.py", line 437, in create_ssl_context ssl_context.load_cert_chain( FileNotFoundError: [Errno 2] No such file or directory

Although my certificates are in root directory. Thanks in advance!

@PaulKeller94 I guess I have created one but now not remember. You can check the Masteclass on youtube or the documentation for rasa 2.0.

whilst seeing this error is not able to read from the location, try save it at same location and then try.

Please do let me know your workaround.

Thanks for answering. My workaround is now, that I have a NGINX-Reverseproxy, which loads the certificates and forwards the requests to the specific Docker container.