Games are simply HTML5(HTML/CSS/JS) pages loaded into a Telegram client's browser. Users also have option to open the game page in a browser. Check Telegram's post about Gaming Platform to learn more.
For example, LumberJack is a Telegram game. Go ahead and play it!
This all happens in your chat with BotFather. Enable Inline Mode for your bot and use /newgame
command to create your game. You can read more about it here.
Each game has a short name that is game's unique id whithin a bot.
In the chat, when user clicks on Play LumberJack, for instance, your bot receives a CallbackQuery update
containing game_short_name
. In response, bot makes a answerCallbackQuery
request passing the url to the
game's page. Telegram client opens the browser and the fun starts!
For example, start LumberJack game on your phone and click on Open in... while in game to open it in a browser. You will see the link to game is something like:
https://tbot.xyz/lumber/#{some-encoded-value}&tgShareScoreUrl=tgb://share_game_score?hash={some-hash-value}
Games can set high scores for users in the chat that they got opened from. This means when game is finished,
a request is usually made to backend (bot's ASP.NET Core app) and bot makes a setGameScore
call to Telegram API.
Similarly, for getting high scores, a request from HTML page(game) could be sent to backend. Then, bot makes a getGameHighScores
call to Bot API and passes back the high scores to the game.
Your Game's HTML page can be anywhere on the internet. It's worth mentioning as a web developer, you should consider many things in your deployment such as:
- Preferring HTTPS over HTTP
- Using a trusted TLS certificate
- Allowing Cross-Origin requests, if necessary
- Guarding against attacks
- Preventing cheaters
- And many more...
Sample games in this project are kept simple and do not follow best practices.