未加星标

How to create a game bot and publish your HTML5 games on Telegram

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

At Ludei we love html5 games, that’s what our motto says. We have developed a lot of HTML5 games, from showcases to test our cocoon platform to fully featured games. At October 3rd the Telegram team announced his gaming platform a ready to play, easy to build, social and lightweight solution to provide digital entertaininment to Telegram’s users (like us, the Ludei team). As the announcement post tells us:“the underlying technology is HTML5…” , we may have an idea here; grass is green.


How to create a game bot and publish your HTML5 games on Telegram

After we checked the examples provided and the documentation, we decided that our bot must be capable of serving our games, setting the user’s score, answering to inline queries. All this while being a social bot, that is, our bot and our games can and are going to be easily shareable in Telegram itself. Let’s start.

For the bot creation you must contact @BotFather , answer wisely all the questions and you will have your bot available to the world.


How to create a game bot and publish your HTML5 games on Telegram

Botfather logo

When your bot is correctly created you must register your games in Telegram. A name, a short name, a description and a correct size photo is enough for Telegram’s “/newgame” command.

First of all, how are we going to access the Telegram API? A fast google search throws lots of good and reliable (frequently updated) libraries written in most modern programming languages so we can choose what to use; there is no problem here. We choosed TelegramsBots Java library and the Java programming language.

There are two kinds of telegram bots depending on how do they receive Telegram’s updates. The long polling and webhook bots, for the first prototype, lets keep it simple (KIS(S)). The webhook server requires a server accessible by Telegram while the long-polling bot doesn’t, so we chose this method for the prototype.


How to create a game bot and publish your HTML5 games on Telegram

The first command a bot receives from any user is the “/start” command, is there a better way to start learning the API? From there we move to our own defined commands, “/list”, “/play” and “/random”.

The list command shows our list of games, so we need to store them before answering the command request, but, what is the information needed to send a game in Telegram? The only properties needed to send a game are where to send it and what game are we sending, the “chatId” and the game “short name” (the same we registered creating our game).

With that information stored in the server, we send a list of games using a custom Telegram keyboard to let the user graphically select the desired game.


How to create a game bot and publish your HTML5 games on Telegram

Telegram’s inline keyboard.

When a user taps on one of the buttons we defined, the text written in that button is sent back to the server. We do process that message and if it follows a concrete arbitrary pattern (“:arrow_forward: gameShortName” in our case) we answer the request executing the play command of our bot.

The “/play” command does the proper game sending process. To do so, we just call Telegram’s API indicating the chatId and the game shortname. We do also send a custom inline keyboard in this response with two buttons. The first button must, as the documentation indicates, always be the “play” button.

We had some problems sending this button because we were a little confused by the documentation stating that the “CallbackGame” value is just “a placeholder, currently holds no information”. When trying to set the callback game with this empty placeholder the button (as our logic expected) doesn’t do anything at all.

After reading our API source code we discovered that a proper game callback does contain some values and after setting a JSON with a unique field “game_short_name” the button works, please, check the following code snippet:

new InlineKeyboardButton().setText("Play solo.") .setCallbackGame( new CallbackGame( new JSONObject("{game_short_name: \"" + game.getShortName() + "\"}")) );

The play with friends button just shares a URL the way the API shares any other URL:

new InlineKeyboardButton() .setText("Play with friends.") .setUrl("http://telegram.me/ludeiBot?game=" + game.getShortName());

The last command is “/random” that just selects a random game and sends it back to the channel it was requested from. Internally, it calls again the “/play” command with filled game shortname parameter (yay, code reuse!).

There is also another topic, the inline queries. Just name our bot @ludeiBot and after writing this, hit space and start writing anything. If one of our games contains the string you have introduced then it will appear immediately so you can tap and start playing it.

This can be done thanks to Telegram’s inline mode . Each time a user enters a new character, Telegram send us a new inline query. We get the query text and look for a game containing that text, we set all the matching games and we send them back before the user can even notice what is happening.

Wait, a request each time a user changes a character, How many requests are we going to process? Can we answer all those requests fast enough for a nice user experience? We use Amazon’s lambda for other internal services and we have some experience with it, could our bot be hosted in this extremely useful service?

Again, after a fast search we found lots of tutorials helping us with this concrete need , we just need to set Telegram’s webhook location to an Amazon’s API Gateway endpoint that launches a Lambda function calling our bot with the received update from Telegram.

Just one Amazon’s Lambda function is enough to receive all the messages from Telegram, process them and answer all the requests. And all this is done in less than 70 ms without spending too much time in the code optimization (this could surely be faster) and without losing time and resources dealing with lateral scalation.


How to create a game bot and publish your HTML5 games on Telegram

Another message that must be processed is the callbackQuery that is being produced when the user taps the “play solo” button of our game message. When we the user taps the button, we receive a callbackQuery from the Telegram API containing the tapped game shortName parameter indicating us which game the user has selected among other parameters.

In order to let the user play, we must then answer this callback with the game’s URL and then Telegram will redirect the user to the game’s site and display it in a webview-like environment.

The last requirement is the score sharing functionality. In order to send a score to Telegram’s API, we have to receive the userId of the player who is scoring, and the chatId and the messageId of the chat the game is being played from or the inlineMessageId from which the game has been launched from.

All this parameters are received in the callbackQuery call a

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: HTMLHTML5Java
分页:12
转载请注明
本文标题:How to create a game bot and publish your HTML5 games on Telegram
本站链接:http://www.codesec.net/view/483826.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(29)