telegram_game package¶
Submodules¶
telegram_game.api module¶
Telegram API implementation.
-
class
telegram_game.api.
BotAPI
(api_token, session=None)[source]¶ Bases:
object
Telegram Bot API Wrapper.
-
API_URL
= 'https://api.telegram.org'¶
-
getMe
()[source]¶ A simple method for testing your bot’s auth token.
Requires no parameters.
Returns: basic information about the bot in form of a User object.
-
getUpdates
(offset=None, limit=None, timeout=None)[source]¶ Use this method to receive incoming updates using long polling (wiki).
It automatically manage offset, so normaly you only have to pass a reasonable timeout value here (30 seconds is a good one, for example).
Parameters: - offset (int) –
Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an _offset_ higher than its update_id.
The negative offset can be specified to retrieve updates starting from -_offset_ update from the end of the updates queue. All previous updates will forgotten.
- limit (int) – Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100.
- timeout (int) – Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling
Returns: An Array of Update objects is returned.
- offset (int) –
-
sendMessage
(chat_id, text, parse_mode=None, disable_web_page_preview=None, disable_notification=None, reply_to_message_id=None, reply_markup=None)[source]¶ Use this method to send text messages.
Parameters: - chat_id (int|str) – Unique identifier for the target chat or username of the target channel (in the format @channelusername)
- text (str) – Text of the message to be sent
- parse_mode (str) – Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot’s message.
- disable_web_page_preview (bool) – Disables link previews for links in this message
- disable_notification (bool) – Sends the message silently. iOS users will not receive a notification, Android users will receive a notification with no sound.
- reply_to_message_id (int) – If the message is a reply, ID of the original message
- reply_markup (str) – Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to hide reply keyboard or to force a reply from the user. InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply.
Returns: On success, the sent Message is returned.
-
-
class
telegram_game.api.
Chat
(api, chat_id)[source]¶ Bases:
object
An API wrapper for chat-related methods
-
sendMessage
(text, parse_mode=None, disable_web_page_preview=None, disable_notification=None, reply_to_message_id=None, reply_markup=None)[source]¶ Use this method to send text messages.
Parameters: - text (str) – Text of the message to be sent
- parse_mode (str) – Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot’s message.
- disable_web_page_preview (bool) – Disables link previews for links in this message
- disable_notification (bool) – Sends the message silently. iOS users will not receive a notification, Android users will receive a notification with no sound.
- reply_to_message_id (int) – If the message is a reply, ID of the original message
- reply_markup (str) – Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to hide reply keyboard or to force a reply from the user. InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply.
Returns: On success, the sent Message is returned.
-
telegram_game.cli module¶
telegram_game.dispatcher module¶
Message dispatching logic
telegram_game.game module¶
telegram_game.redis_game module¶
In some games the game state is not critical, it is ok to drop all and ask the
player to start from scratch. In that case just use
telegram_game.game.BaseGame
, and store state in the start coroutine
local variables or self attributes.
But in some games it is crucial to save the state
between different bot runs. In that case you can use RedisGame
.
-
class
telegram_game.redis_game.
RedisField
(initial)[source]¶ Bases:
object
Define a RedisGame field as a persistent field.
-
class
telegram_game.redis_game.
RedisGame
(*args, **kwargs)[source]¶ Bases:
telegram_game.game.BaseGame
Game with persistent fields stored in Redis.
You can define persistent fields with telegram_game.redis_game.RedisField, and use them as the usual variables in your game logic code:
class Game(RedisGame): score = RedisField(initial=0) level = RedisField(initial=1) async def start(self): self.score += 1 await self.reply(self.level)
Field values are serialised with msgpack. Think of it like about a trivial ORM.
Note
It is recommened to run the redis server on the same host, where the game bot is running (to reduce latency). In this case it should be ok to use a blocking redis client for the sake of simplicity.