utils package

Description

Modules that provide various util

info_util

Common info needed in both command and callback handlers

modules.utils.info_util.check_message_type(message: telegram.message.Message) → bool

Check that the type of the message is one of the ones supported

Parameters

message (Message) – message to check

Returns

whether its type is supported or not

Return type

bool

modules.utils.info_util.get_callback_info(update: telegram.update.Update, context: telegram.ext.callbackcontext.CallbackContext) → dict

Get the classic info from the update and context parameters for callbacks

Parameters
  • update (Update) – update event

  • context (CallbackContext) – context passed by the handler

Returns

{bot, chat_id, text, query_id, data, message_id, sender_first_name, sender_id}

Return type

dict

modules.utils.info_util.get_message_info(update: telegram.update.Update, context: telegram.ext.callbackcontext.CallbackContext) → dict

Get the classic info from the update and context parameters for commands and messages

Parameters
  • update (Update) – update event

  • context (CallbackContext) – context passed by the handler

Returns

{bot, chat_id, text, message_id, sender_first_name, sender_id}

Return type

dict

keyboard_util

Creates the inlinekeyboard sent by the bot in its messages

modules.utils.keyboard_util.get_approve_kb() → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Generates the InlineKeyboard for the pending post

Returns

new inline keyboard

Return type

InlineKeyboardMarkup

modules.utils.keyboard_util.get_vote_kb() → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Generates the InlineKeyboard for the published post

Returns

new inline keyboard

Return type

InlineKeyboardMarkup

modules.utils.keyboard_util.update_approve_kb(keyboard: List[List[telegram.inline.inlinekeyboardbutton.InlineKeyboardButton]], g_message_id: int, group_id: int, approve: int = - 1, reject: int = - 1) → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Updates the InlineKeyboard when the valutation of a pending post changes

Parameters
  • keyboard (List[List[InlineKeyboardButton]]) – previous keyboard

  • g_message_id (int) – id of the pending post in question ni the admin group

  • group_id (int) – id of the admin group

  • approve (int, optional) – number of approve votes, if known. Defaults to -1.

  • reject (int, optional) – number of reject votes, if known. Defaults to -1.

Returns

updated inline keyboard

Return type

InlineKeyboardMarkup

modules.utils.keyboard_util.update_vote_kb(keyboard: List[List[telegram.inline.inlinekeyboardbutton.InlineKeyboardButton]], c_message_id: int, channel_id: int, upvote: int = - 1, downvote: int = - 1) → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Updates the InlineKeyboard when the valutation of a published post changes

Parameters
  • keyboard (List[List[InlineKeyboardButton]]) – previous keyboard

  • c_message_id (int) – id of the published post in question

  • channel_id (int) – id of the channel

  • upvote (int, optional) – number of upvotes, if known. Defaults to -1.

  • downvote (int, optional) – number of downvotes, if known. Defaults to -1.

Returns

updated inline keyboard

Return type

InlineKeyboardMarkup

post_util

Modules that handles how the post shall be sent according to the chat (adming group or channel)

modules.utils.post_util.anonym_name() → str

Generates a name for an anonym user

Returns

a name among the ones proposed

Return type

str

modules.utils.post_util.send_helper_message(user_id: int, chat_id: int, reply_message_id: int, bot: telegram.bot.Bot, reply_markup: telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup = None) → telegram.message.Message

Sends an helper message to show the author of the post, and to vote on the post if comments are enabled

Parameters
  • user_id (int) – id of the user that originated the post

  • chat_id (int) – id of the chat to which send the helper message

  • reply_message_id (int) – id of the message the helper message will reply to

  • bot (Bot) – bot

  • reply_markup (InlineKeyboardMarkup, optional) – voting Inline Keyboard. Defaults to None.

Returns

helper message

Return type

Message

modules.utils.post_util.send_post_to(message: telegram.message.Message, bot: telegram.bot.Bot, destination: str, user_id: int = None) → telegram.message.Message

Sends the post to the specified destination:

admin -> to the admin group, so it can be approved

channel -> to the channel, so it can be ejoyed by the users (and voted, if comments are disabled)

channel_group -> to the group associated to the channel, so that users can vote the post (if comments are enabled)

Parameters
  • message (Message) – message that contains the post to send

  • bot (Bot) – bot

  • destination (str) – destination of the message (admin | channel | channel_group)

  • user_id (int, optional) – id of the user that originated the post. Defaults to None.

Returns

message used to send a post to a specific destination

Return type

Message

modules.utils.post_util.show_admins_votes(chat_id: int, message_id: int, bot: telegram.bot.Bot, approve: bool)

After a post is been approved or rejected, shows the admins that aproved or rejected it

Parameters
  • chat_id (int) – id of the admin group

  • message_id (int) – id of the post in question in the group

  • bot (Bot) – bot

  • approve (bool) – whether the vote is approve or reject