pyrobale.client

Classes

Client

A client for interacting with the Bale messenger API.

Package Contents

class pyrobale.client.Client(token, base_url='https://tapi.bale.ai/bot')[source]

A client for interacting with the Bale messenger API.

Parameters:
  • token (str) – The bot token.

  • base_url (str, optional) – The base URL for the API. Defaults to “https://tapi.bale.ai/bot”.

Returns:

The client instance.

Return type:

Client

token[source]
base_url = 'https://tapi.bale.ai/bot'[source]
requests_base[source]
handlers = [][source]
running = False[source]
last_update_id = 0[source]
state_machine[source]
check_defined_message = True[source]
defined_messages[source]
async get_updates(offset=None, limit=None, timeout=None)[source]

Get updates from the Bale API.

Parameters:
  • offset (int, optional) – The offset to start with. Defaults to None.

  • limit (int, optional) – The maximum number of updates to return. Defaults to None.

  • timeout (int, optional) – The maximum number of seconds to wait. Defaults to None.

Returns:

The updates.

Return type:

List[Dict]

async set_webhook(url)[source]

Set the webhook for the bot.

Parameters:

url (str) – The webhook URL.

Returns:

True if the webhook was set.

Return type:

bool

async get_webhook_info()[source]

Get the webhook information for the bot.

Returns:

The webhook information.

Return type:

Dict

async get_me()[source]

Get information about the bot.

Returns:

The bot.

Return type:

User

async logout()[source]

Log out the bot.

Returns:

True if the bot was logged out.

Return type:

bool

async close()[source]

Close the bot.

Returns:

True if the bot was closed.

Return type:

bool

async send_message(chat_id, text, reply_to_message_id=None, reply_markup=None)[source]

Send a message to a chat.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • text (str) – The text to send.

  • reply_to_message_id (int, optional) – The message ID to reply to. Defaults to None.

  • reply_markup (Union[InlineKeyboardMarkup, ReplyKeyboardMarkup], optional) – The reply keyboard markup (buttons). Defaults to None.

Returns:

The message.

Return type:

Message

async delete_message(chat_id, message_id)[source]

Deletes a message from a chat.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • message_id (int) – The message ID to delete.

Returns:

True if the message was deleted.

Return type:

bool

async forward_message(chat_id, from_chat_id, message_id)[source]

Forward a message to a chat.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • from_chat_id (int) – The chat to send the message to.

  • message_id (int) – The message ID to forward.

Returns:

The message.

Return type:

Message

async copy_message(chat_id, from_chat_id, message_id)[source]

Copy a message to a chat without forwarding.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • from_chat_id (int) – The chat to send the message to.

  • message_id (int) – The message ID to forward.

Returns:

The message.

Return type:

Message

async send_photo(chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send a photo to a chat.

Parameters:
  • chat_id (Union[int, str]) – The chat to send the message to.

  • photo (Union[InputFile, str]) – The photo to send.

  • caption (Optional[str], optional) – The caption of the photo. Defaults to None.

  • reply_to_message_id (Optional[int], optional) – The message ID to reply to. Defaults to None.

  • reply_markup (Optional[InlineKeyboardMarkup], optional) – The reply keyboard markup (buttons). Defaults to None.

Return type:

pyrobale.objects.message.Message

async send_audio(chat_id, audio, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send an audio to a chat.

Parameters:
Returns:

The message.

Return type:

Message

async send_document(chat_id, document, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send a document to a chat.

Parameters:
Returns:

The message.

Return type:

Message

async send_video(chat_id, video, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send a video to a chat.

Parameters:
Returns:

The message.

Return type:

Message

async send_animation(chat_id, animation, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send an animation (GIF) to a chat.

Parameters:
Returns:

The message.

Return type:

Message

async send_voice(chat_id, voice, caption=None, reply_to_message_id=None, reply_markup=None)[source]

Send a voice message to a chat.

Parameters:
Returns:

The message.

Return type:

Message

async send_media_group(chat_id, media, reply_to_message_id=None, reply_markup=None)[source]

Send a media group to a chat.

Parameters:
Returns:

The list of messages.

Return type:

List[Message]

async send_location(chat_id, latitude, longitude, horizontal_accuracy=None, reply_to_message_id=None, reply_markup=None)[source]

Send a location to a chat.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • latitude (float) – The latitude of the location.

  • longitude (float) – The longitude of the location.

  • horizontal_accuracy (Optional[float], optional) – The horizontal accuracy of the location.

  • reply_to_message_id (Optional[int], optional) – The message ID to reply to. Defaults to None.

  • Optional[InlineKeyboardMarkup] (reply_markup) – The reply keyboard markup (buttons).

  • optional) – The reply keyboard markup (buttons).

  • reply_markup (Optional[Union[pyrobale.objects.inlinekeyboardmarkup.InlineKeyboardMarkup, pyrobale.objects.replykeyboardmarkup.ReplyKeyboardMarkup]])

Returns:

The message.

Return type:

Message

async send_contact(chat_id, phone_number, first_name, last_name=None, reply_to_message_id=None, reply_markup=None)[source]

Send a contact to a chat.

Parameters:
  • chat_id (int) – The chat to send the message to.

  • phone_number (str) – The phone number of the contact.

  • first_name (str) – The first name of the contact.

  • last_name (Optional[str], optional) – The last name of the contact.

  • reply_to_message_id (Optional[int], optional) – The message ID to reply to. Defaults to None.

  • Optional[InlineKeyboardMarkup] (reply_markup) – The reply keyboard markup (buttons).

  • optional) – The reply keyboard markup (buttons).

  • reply_markup (Optional[Union[pyrobale.objects.inlinekeyboardmarkup.InlineKeyboardMarkup, pyrobale.objects.replykeyboardmarkup.ReplyKeyboardMarkup]])

Returns:

The message.

Return type:

Message

async send_invoice(chat_id, title, description, payload, provider_token, prices, photo_url=None, reply_to_message_id=None)[source]

Sends a message including a invoice for user to pay.

Parameters:
  • chat_id (Union[str, int]) – The chat to send the message to.

  • title (str) – The title of the invoice.

  • description (str) – The description of the invoice.

  • payload (str) – The payload of the invoice.

  • provider_token (str) – The provider token.

  • prices (List[LabeledPrice]) – The prices of the invoice.

  • photo_url (Optional[str], optional) – The photo url of the invoice.

  • reply_to_message_id (Optional[int], optional) – The message ID to reply to. Defaults to None.

Returns:

The message.

Return type:

Message

async get_file(file_id)[source]

Get a file from the Bale servers.

Parameters:

file_id (str) – The file ID.

Returns:

The file.

Return type:

File

async answer_callback_query(callback_query_id, text=None, show_alert=None)[source]

Answer a callback query.

Parameters:
  • callback_query_id (str) – The callback query ID.

  • text (Optional[str], optional) – The text of the answer.

  • show_alert (Optional[bool], optional) – Whether the answer should be shown.

Returns:

Whether the answer was shown.

Return type:

bool

async ban_chat_member(chat_id, user_id)[source]

Ban a user from a chat.

Parameters:
  • chat_id (int) – The chat to ban.

  • user_id (int) – The user to ban.

Returns:

Whether the ban was successful.

Return type:

bool

async unban_chat_member(chat_id, user_id)[source]

Unban a user from a chat.

Parameters:
  • chat_id (int) – The chat to unban.

  • user_id (int) – The user to unban.

Returns:

Whether the unban was successful.

Return type:

bool

async kick_chat_member(chat_id, user_id)[source]

kick a user from a specified chat

Parameters:
  • chat_id (int) – The chat to kick.

  • user_id (int) – The user to kick.

Returns:

Whether the kick was successful.

Return type:

bool

async get_chat_member(chat_id, user_id)[source]

Get a chat member.

Parameters:
  • chat_id (int) – The chat to get.

  • user_id (int) – The user to get.

Returns:

The chat member.

Return type:

ChatMember

async is_user_admin(chat_id, user_id)[source]

Checks if a user is admin in a chat

Parameters:
  • chat_id (int) – The chat to get.

  • user_id (int) – The user to get.

Returns:

Whether the user is admin in a chat.

Return type:

bool

async user_has_permissions(chat_id, user_id, permissions)[source]

checks if a user has a specified permission

Parameters:
  • chat_id (int) – The chat to get.

  • user_id (int) – The user to get.

  • permissions (ChatPermissions) – The permissions to check.

Returns:

Whether the user has a specified permission.

Return type:

bool

async promote_chat_member(chat_id, user_id, can_change_info=None, can_post_messages=None, can_edit_messages=None, can_delete_messages=None, can_invite_users=None, can_restrict_members=None, can_pin_messages=None, can_promote_members=None)[source]

Promote a user in a chat.

Parameters:
  • chat_id (int) – The chat to get.

  • user_id (int) – The user to get.

  • can_change_info (Optional[bool], optional) – Whether the user can change the information.

  • can_post_messages (Optional[bool], optional) – Whether the user can post messages.

  • can_edit_messages (Optional[bool], optional) – Whether the user can edit messages.

  • can_delete_messages (Optional[bool], optional) – Whether the user can delete messages.

  • can_invite_users (Optional[bool], optional) – Whether the user can invite users.

  • can_restrict_members (Optional[bool], optional) – Whether the user can restrict members.

  • can_pin_messages (Optional[bool], optional) – Whether the user can pin messages.

  • can_promote_members (Optional[bool], optional) – Whether the user can promote members.

Returns:

Whether the user has a specified permission.

Return type:

bool

async set_chat_photo(chat_id, photo)[source]

Set a new profile photo for the chat.

Parameters:
  • chat_id (int) – The chat to get.

  • photo (InputFile) – The photo to set.

Returns:

Whether the photo was set.

Return type:

bool

async leave_chat(chat_id)[source]

Leave a group, supergroup or channel.

Parameters:

chat_id (int) – The chat to get.

Returns:

Whether the chat was leaved.

Return type:

bool

async is_joined(user_id, chat_id)[source]

Check if a user is joined to a chat.

Parameters:
  • user_id (int) – The user to get.

  • chat_id (int) – The chat to get.

Returns:

Whether the user is joined to a chat.

Return type:

bool

async get_chat(chat_id)[source]

Get up to date information about the chat.

Parameters:

chat_id (int) – The chat to get.

Returns:

The chat.

Return type:

Chat

static get_ble_ir_page(username_or_phone_number)[source]
Async:

Parameters:

username_or_phone_number (str)

Return type:

pyrobale.objects.peerdata.PeerData

Get BleIR user/group information.

Parameters:

username_or_phone_number (str) – The username or phone number.

Returns:

The BleIR user/group information.

Return type:

PeerData

async get_chat_members_count(chat_id)[source]

Get the number of members in a chat.

Parameters:

chat_id (int) – The chat to get.

Returns:

The number of members in a chat.

Return type:

int

async pin_chat_message(chat_id, message_id)[source]

Pin a message in a chat.

Parameters:
  • chat_id (int) – The chat to get.

  • message_id (int) – The message to pin.

Returns:

Whether the message was pinned.

Return type:

bool

async unpin_chat_message(chat_id, message_id)[source]

Unpin a message in a chat.

Parameters:
  • chat_id (int) – The chat to get.

  • message_id (int) – The message to unpin.

Returns:

Whether the message was unpinned.

Return type:

bool

async unpin_all_chat_messages(chat_id)[source]

Unpin all messages in a chat.

Parameters:

chat_id (int) – The chat to get.

Returns:

Whether the message was unpinned.

Return type:

bool

async set_chat_title(chat_id, title)[source]

Change the title of a chat.

Parameters:
  • chat_id (int) – The chat to get.

  • title (str) – The new title.

Returns:

Whether the title was changed.

Return type:

bool

async set_chat_description(chat_id, description)[source]

Change the description of a chat.

Parameters:
  • chat_id (int) – The chat to get.

  • description (str) – The new description.

Returns:

Whether the description was changed.

Return type:

bool

async delete_chat_photo(chat_id)[source]

Delete a chat photo.

Parameters:

chat_id (int) – The chat to get.

Returns:

Whether the photo was deleted.

Return type:

bool

async edit_message(chat_id, message_id, text, reply_markup=None)[source]

Edits a message in a specified chat

Parameters:
  • chat_id (int) – The chat to get.

  • message_id (int) – The message to get.

  • text (str) – The text to edit.

  • reply_markup (InlineKeyboardMarkup, optional) – the reply markup to use. Defaults to None.

Returns:

The edited message.

Return type:

Message

Create an additional invite link for a chat.

Parameters:

chat_id (int) – The chat to get.

Returns:

The invite link.

Return type:

str

Revoke an invite link created by the bot.

Parameters:
  • chat_id (int) – The chat to get.

  • invite_link (str) – The invite link to revoke.

Returns:

The revoked invite link.

Return type:

str

Generate a new primary invite link for a chat.

Parameters:

chat_id (int) – The chat to get.

Returns:

The invite link.

Return type:

str

async send_chat_action(chat_id, action)[source]

Tell the user that something is happening on the bot’s side.

Parameters:
  • chat_id (int) – The chat to get.

  • action (ChatAction) – The action to send.

Returns:

Whether the action was sent.

Return type:

bool

async wait_for(update_type, check=None)[source]

Wait until a specified update

Parameters:
  • update_type (UpdatesTypes) – The update to wait for.

  • check (Callable, optional) – The check method to check.

async process_update(update)[source]

Process a single update and call registered handlers.

Parameters:

update (Dict[str, Any])

Return type:

None

base_handler_decorator(update_type)[source]

Base decorator for handling different types of updates.

Parameters:

update_type (UpdatesTypes) – The update to process.

Returns:

The decorated handler.

Return type:

Callable

on_command(command, filter=None)[source]

Decorator for handling command updates.

Parameters:
  • command (str)

  • filter (Optional[pyrobale.filters.Filters])

on_message(filter=None)[source]

Decorator for handling new message updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_edited_message(filter=None)[source]

Decorator for handling edited message updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_callback_query(filter=None)[source]

Decorator for handling callback query updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_new_members(filter=None)[source]

Decorator for handling new chat members updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_members_left(filter=None)[source]

Decorator for handling members left updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_pre_checkout_query(filter=None)[source]

Decorator for handling pre-checkout query updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_photo(filter=None)[source]

Decorator for handling photo updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

on_successful_payment(filter=None)[source]

Decorator for handling successful payment updates.

Parameters:

filter (Optional[pyrobale.filters.Filters])

add_handler(update_type, callback, filter=None, **kwargs)[source]

Register a handler for specific update type.

Parameters:
  • update_type (UpdatesTypes) – The update to process.

  • callback (Callable) – The callback to handle.

  • filter (Optional[Filters]) – The filter to handle.

  • **kwargs – The kwargs to pass to the callback.

Returns:

The decorated handler.

Return type:

Callable

remove_handler(callback)[source]

Remove a handler from the list of handlers.

Parameters:

callback (Callable) – The callback to remove.

Return type:

None

remove_all_handlers()[source]

Remove all handlers from the list of handlers.

Return type:

None

async start_polling(timeout=30, limit=100)[source]

Start polling updates from the server.

Parameters:
  • timeout (int) – Time to wait for updates.

  • limit (int) – Number of updates to poll.

Return type:

None

async stop_polling()[source]

Stop polling updates.

Return type:

None

run(timeout=30, limit=100)[source]

Run the client.

Parameters:
  • timeout (int) – Time to wait for updates.

  • limit (int) – Number of updates to poll.

Return type:

None

async stop()[source]

Stop the client.

Return type:

None

async handle_webhook_update(update_data)[source]

Process an update received via webhook.

Parameters:

update_data (Dict[str, Any])

Return type:

None