Python šŸ˜ Telegram Games (aiogram)

Daniil Ekzarian
Reflash Programming Adventures
4 min readSep 5, 2018

Good evening, everyone! I think a lot of us like games both to play and to code. Did you know that messengers, so as Facebook Messenger and Telegram, are providing APIs for games?

ā€œwhite and black Nintendo Game Boy Color on yellow surfaceā€ by Mike Meyers on Unsplash

Probably you wonā€™t code something AAA level for messengers, but casual games fit here quite nicely. Today I want to share with you some of my code snippets for Telegram Games šŸŽ². To make it simple I will use python with a Telegram api library aiogram.

Whatā€™s so good about making a game in Telegram?

Firstly, itā€™s an app, thatā€™s highly likely installed on your userā€™s phone already. So no need to download your game.

Secondly, it will be a good complement in case you have a Telegram chatbot. Depending on your chatbotā€™s business goals, you can engage your customer in some promos with a game.

Letā€™s see how to do it by example šŸ˜ƒ

Assuming, that you already have an existing chatbot in Telegram, I will provide further steps to create a game. ChatBot is an integral part of your game, so you must register your Telegram bot first. If you donā€™t have a chatbot, please, follow this guide.

The inline mode of your chatbot should be enabled. To do that ā€” send /setinline command to @BotFather and choose your chatbotā€™s name.

Now you have to send a /newgame command to initiate the ā€œregistrationā€ process.

BotFather will send you the rules, that are applied to your game. In particular, at this moment your game couldnā€™t be monetised, so you canā€™t have ads or other ways of monetizing in your game. That is because Telegram Games are simple HTML5 apps, so Telegram has no control over what you do, but they can punish you post factum.

All the further steps are quite obvious to handle (any trouble? Ask in comments), setting the name, picture/gif for you game.

If youā€™ve finished these steps successfully, you will be able now to address your chatbot in inline mode (typing @<chatbot_name> in message āœ‰ļø area). Yet you canā€™t see your game in the pop-up, that requires a working server.

For the purpose of this article I will use aiogram framework, that seems an adequate choice in this case. Unfortunately, Iā€™ve found no framework specifically for telegram games and even aiogram provides limited functionality.

To start working with aiogram you have to define a dispatcher.

Dispatcher is responsible for connection of messages from Telegram service with handlers.

Next step is to define inline handler which will show our user a game description in inline menu.

Telegram inline output

Check that itā€™s working by sending an inline message to your chatbot. game_short_name is a variable containing your game name.

Next step is to handle the request to send users a link to your game.

url variable contains a url path to your HTML5 page with your game. This handler will be called when you choose your game from the inline menu list.

The button below the message will lead to your game page. The next step is to develop the actual game. In order to test that everything works you could try to change url variable to contain one of the links to already existing games (@gamee).

Next step is to make our own game. I will show you some tricks that you can use to make your life easy šŸ˜ƒ

Telegram can only link to https urls, so your api should return a link with https. But what if you have just your laptop and donā€™t want to spend time configuring server with https endpoints. There is a solution! šŸŽ‰

Use Ngrok!

Ngrok is a service which will allow you to create a tunnel with an externally accessible https link to your localhost server.

After installing ngrok on your computer, simply run this command.

./ngrok http <PORT>

It will open a tunnel to your localhost with port <PORT> and create both http and https links.

Because by default ngrok generates random link names I use the following function to get the actual url.

Now to load an html page I set up simple Flask app which will return static html page.

Now add ā€˜/gameā€™ extension to url variable and you can happily open your game page from Telegram chat.

The full code of this example, you can find via link. Thank you for reading!

--

--

Daniil Ekzarian
Reflash Programming Adventures

Software engineer šŸ‘Øā€šŸ’» Functional programming disciple