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?
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.
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! 🎉
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!