Code execution in Telegram chat

Anthony Murygin
2 min readMar 25, 2020

--

Ruby REPL in Telegram

Yesterday I’ve been struck with a beautiful question: is it possible to make REPL in Telegram? Spoiler: yes.

The architecture will be like this: I will receive webhooks firing on every outgoing message, on to my server, that will execute the code and send the result back.

Receiving the outgoing message text

How to get webhooks about outgoing messages?

I will be using TaaS for that (https://www.t-a-a-s.ru). TaaS is built around TDLib library, developed by Telegram, which is the engine that lets you basically build your own client. It is pretty complicated to build and use the library, but TaaS makes it super easy taking away all the maintenance costs by hosting the TDLib instance in the cloud. I can use it to subscribe to webhooks on outgoing messages.

I log in to TaaS, click New Client button, creating the client for my phone number, then I create a subscription using corresponding request. The webhook is ready.

Creating a webhook subscription in TaaS

Evaluating the code

I quickly set up the Rails server (because I love programming in Ruby) on Heroku, and I make just one controller action for it. Also I make special tdputs method to be able to send additional messages from within the code (e.g. from loops).

Evaluation code

Conclusion

That’s it! Basically, you can create your own Telegram client, that works in parallel with your usual client, but has some very special features.

Result, one more time:

Ruby REPL in Telegram

--

--