I recently created a game on Telegram. This was not my first Telegram bot, but it would be my first game. Hence, I first checked the official Telegram documentation for their gaming platform.
While reading it, I felt it was rather skimpy on the details. I also wished they would link to the bot API documentation directly, so that I could see the exact objects, methods and fields involved.
This guide is meant to provide slightly more details (and useful links) than the existing documentation, but is not meant to be a step-by-step guide. It is language-agnostic so that you can apply these concepts regardless of which language you choose to write your bot and game in.
- Create a Telegram Bot using
- Enable inline mode for the bot using
- Create a new game using
After this process, you will have your bot token and your
game_short_name, which will be used later.
Here's what the documentation says:
Once the game is created, your bot can send it to chats as regular messages, or offer them via inline mode.
Let's break that down.
For example, you could have the bot respond to the
/start command (or any other command/phrase of your choice) by sending the game.
Here's a sequence diagram for what goes on behind the scenes.
- The user initiates the conversation with your chosen phrase
- Your bot will receive a Message object containing the message that was sent
- You may respond using the
sendGameAPI, which is fairly straightforward as the only required fields are
game_short_name(from the set up process)
- The bot sends a message with the game and a "Play Game Name" callback button
Next, the callback interactions occur.
- The user clicks on "Play Game Name"
- Telegram sends a
game_short_nameto your bot
- Your bot calls
answerCallbackQuerywith a url to the game
- Telegram opens the url to the game
This was the part that confused me the most.
Here is the equivalent sequence diagram for the interactions between the user, Telegram and your bot.
- The user initiates the interaction by typing "@your_bot_username" in any chat
- Telegram sends an
InlineQueryto your bot
- Your bot calls
answerInlineQuerywith an array of
- Telegram displays the results to the user
- The user selects a game from the list
- The user sends a message (via your bot) the game and a "Play Game Name" callback button.
The remaining flow for launching the game via callback interactions is the same as before.
This guide provides more elaboration on the interactions between the user, Telegram and your bot when launching a game.
An interesting difference between these two methods is that in the first, the bot is the one who sends the message containing the game. In the second, the user who initiated the inline query is the one who sends the game. This is because the bot may not be a part of the group chat (as it is an inline interaction).
Hope this guide helped you, and happy coding!