spotted.utils package

Submodules

spotted.utils.constants module

Constants used by the util module

spotted.utils.conversation_util module

Common functions needed in conversation handlers

spotted.utils.conversation_util.conv_cancel(family)[source]

Creates a function used to handle the /cancel command in the conversation. Invoking /cancel will exit the conversation immediately

Parameters:

family (str) – family of the command

Returns:

Callable[[Update, CallbackContext], int] – function used to handle the /cancel command

spotted.utils.conversation_util.conv_fail(family)[source]

Creates a function used to handle any error in the conversation

Parameters:

family (str) – family of the command

Returns:

Callable[[tuple[Update, CallbackContext] | EventInfo, str, int | None], int | None] – function used to handle the error

spotted.utils.info_util module

Common info needed in both command and callback handlers

class spotted.utils.info_util.EventInfo(bot, ctx, update=None, message=None, query=None)[source]

Bases: object

Class that contains all the relevant information related to an event

async answer_callback_query(text=None)[source]

Calls the answer_callback_query method of the bot class, while also handling the exception

Parameters:

text (str, default: None) – Text to show to the user

property args: list[str]

Return the args of the message that caused the update. If the update was caused by a callback, the callback data is splitted by ‘,’ and returned

property bot: Bot

Instance of the telegram bot

property bot_data: dict

Data related to the bot. Is not persistent between restarts

property callback_key: str

Return the args of the message that caused the update. If the update was caused by a callback, the callback data is splitted by ‘,’ and returned

property chat_id: int

Id of the chat where the event happened

property chat_type: str

Type of the chat where the event happened

property context: CallbackContext

Context generated by some event

async edit_inline_keyboard(chat_id=None, message_id=None, new_keyboard=None)[source]

Generic wrapper used to edit the inline keyboard of a message with the telegram bot, while also handling the exception

Parameters:
  • chat_id (int, default: None) – id of the chat the message to edit belongs to or the current chat if None

  • message_id (int, default: None) – id of the message to edit. It is the current message if left None

  • new_keyboard (InlineKeyboardMarkup, default: None) – new inline keyboard to assign to the message

property forward_from_chat_id: int

Id of the original chat the message has been forwarded from

property forward_from_id: int

Id of the original message that has been forwarded

classmethod from_callback(update, ctx)[source]

Instance of EventInfo created by a callback update

Parameters:
  • update (Update) – update event

  • context – context passed by the handler

Returns:

EventInfo – instance of the class

classmethod from_job(ctx)[source]

Instance of EventInfo created by a job update

Parameters:

context – context passed by the handler

Returns:

EventInfo – instance of the class

classmethod from_message(update, ctx)[source]

Instance of EventInfo created by a message update

Parameters:
  • update (Update) – update event

  • context – context passed by the handler

Returns:

EventInfo – instance of the class

property inline_keyboard: InlineKeyboardMarkup

InlineKeyboard attached to the message

property is_forward_from_channel: bool

Whether the message has been forwarded from a channel

property is_forward_from_chat: bool

Whether the message has been forwarded from a chat

property is_forward_from_user: bool

Whether the message has been forwarded from a user

property is_forwarded_post: bool

Whether the message is in fact a forwarded post from the channel to the group

property is_private_chat: bool

Whether the chat is private or not

property is_valid_message_type: bool

Whether or not the type of the message is supported

property message: Message

Message that caused the update

property message_id: int

Id of the message that caused the update

property query_data: str

Data associated with the query that caused the update

property query_id: str

Id of the query that caused the update

property reply_markup: InlineKeyboardMarkup

Reply_markup of the message that caused the update

async send_post_to_admins()[source]

Sends the post to the admin group, so it can be approved

Returns:

bool – whether or not the operation was successful

async send_post_to_channel(user_id)[source]

Sends the post to the channel, so it can be enjoyed by the users (and voted, if comments are disabled)

async send_post_to_channel_group()[source]

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

async show_admins_votes(pending_post, reason=None)[source]

After a post is been approved or rejected, shows the admins that approved or rejected it and edit the message to show the admin’s votes

Parameters:
  • pending_post (PendingPost) – post to show the admin’s votes for

  • reason (str | None, default: None) – reason for the rejection, currently used on autoreply

property text: str

Text of the message that caused the update

property update: Update

Update generated by some event

property user_data: dict

Data related to the user. Is not persistent between restarts

property user_id: int

Id of the user that caused the update

property user_name: str

Name of the user that caused the update

property user_username: str

Username of the user that caused the update

spotted.utils.keyboard_util module

Creates the inlinekeyboard sent by the bot in its messages. Callback_data format: <callback_family>_<callback_name>,[arg]

spotted.utils.keyboard_util.get_approve_kb(pending_post=None, approve=-1, reject=-1)[source]

Generates the InlineKeyboard for the pending post. If the pending post is None, the keyboard will be generated with 0 votes. Otherwise, the keyboard will be generated with the correct number of votes. To avoid unnecessary queries, the number of votes can be passed as an argument and will be assumed to be correct.

Parameters:
  • pending_post (PendingPost, default: None) – existing pending post to which the keyboard is attached

  • approve (int, default: -1) – number of approve votes known in advance

  • reject (int, default: -1) – number of reject votes known in advance

Returns:

InlineKeyboardMarkup – new inline keyboard

spotted.utils.keyboard_util.get_autoreply_kb(page, items_per_page)[source]

Generates the keyboard for the autoreplies

Parameters:
  • page (int) – page of the autoreplies

  • items_per_page (int) – number of items per page

Returns:

list[list[InlineKeyboardButton]] – new part of keyboard

spotted.utils.keyboard_util.get_confirm_kb()[source]

Generates the InlineKeyboard to confirm the creation of the post

Returns:

InlineKeyboardMarkup – new inline keyboard

spotted.utils.keyboard_util.get_paused_kb(page, items_per_page)[source]

Generates the InlineKeyboard for the paused post

Parameters:

page (int) – page of the autoreplies

Returns:

InlineKeyboardMarkup – autoreplies keyboard append with resume button

async spotted.utils.keyboard_util.get_post_outcome_kb(bot, votes, reason=None)[source]

Generates the InlineKeyboard for the outcome of a post

Parameters:
  • bot (Bot) – bot instance

  • votes (list[tuple[int, bool]]) – list of votes

  • reason (str | None, default: None) – reason for the rejection, currently used on autoreplies

Returns:

InlineKeyboardMarkup – new inline keyboard

spotted.utils.keyboard_util.get_preview_kb()[source]

Generates the InlineKeyboard to choose if the post should be previewed or not

Returns:

InlineKeyboardMarkup – new inline keyboard

spotted.utils.keyboard_util.get_published_post_kb()[source]

Generates the InlineKeyboard for the published post adding the report button if needed

Returns:

InlineKeyboardMarkup | None – new inline keyboard

spotted.utils.keyboard_util.get_settings_kb()[source]

Generates the InlineKeyboard to edit the settings

Returns:

InlineKeyboardMarkup – new inline keyboard

Module contents

Modules that provide various util