Skip to main content

No project description provided

Project description

BotAPI

Overview

The BotAPI class provides an interface for interacting with the Yandex Bot API. This class allows you to send messages (text, file, image) and manage incoming messages via polling. You can register message handlers using regular expressions and set up custom logic to process incoming messages.

Features

  • Send text messages to users via chat_id or login.
  • Send file messages by specifying a file path.
  • Send image messages with an image file path.
  • Poll for new messages continuously.
  • Decorate message handlers to process specific patterns using regular expressions.

Installation

Ensure you have the necessary dependencies installed:

pip install httpx

Usage

Initialization

To initialize the bot, you need to pass the OAuth token:

from bot_api import BotAPI

bot = BotAPI(token="your-oauth-token")

Sending Messages

You can send different types of messages using the following methods:

  • send_text_message(message: Message, text: str)
  • send_file_message(message: Message, file_path: str)
  • send_image_message(message: Message, image_path: str)

Example to send a text message:

await bot.send_text_message(message, "Hello, World!")

Handling Incoming Messages

You can register handlers for messages using the @message_handler decorator. The handler can process messages matching a specific pattern.

Example:

@bot.message_handler(r'^/start')
async def start_handler(message):
    await bot.send_text_message(message, "Welcome to the bot!")

Polling for Messages

To start polling for new messages, use the start_polling method:

await bot.start_polling()

This will continuously poll for new messages and process them using registered handlers.

Example Bot

Here is a complete example of setting up a simple bot that responds to the /start command:

from bot_api import BotAPI

bot = BotAPI(token="your-oauth-token")

@bot.message_handler(r'^/start')
async def start_handler(message):
    await bot.send_text_message(message, "Welcome to the bot!")

async def main():
    await bot.start_polling()

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Methods

get_new_messages()

Fetches new messages from the Yandex Bot API.

call_api_for_messages()

Makes a request to the Yandex Bot API to retrieve updates and handles the polling offset.

send_text_message(message: Message, text: str)

Sends a text message to the recipient.

send_file_message(message: Message, file_path: str)

Sends a file message to the recipient.

send_image_message(message: Message, image_path: str)

Sends an image message to the recipient.

get_recipient_info(message: Message)

Determines whether to use the login or chat_id to send the message.

message_handler(pattern)

A decorator to register a message handler for a specific pattern (regex or string).

process_message(message: Message)

Processes an incoming message by matching it with a registered handler.

start_polling()

Begins polling for new messages at the defined interval.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yandexbot-0.2.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

yandexbot-0.2.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file yandexbot-0.2.2.tar.gz.

File metadata

  • Download URL: yandexbot-0.2.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.6 Linux/6.10.12-200.fc40.x86_64

File hashes

Hashes for yandexbot-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1686918b99104d7e6f1eceddabad01fdec4e29c6be6f2e24670a2249e9fc388b
MD5 e004582a43b7ac977a89af06dda36d50
BLAKE2b-256 0f90a83cad95cd75431902e648436c65a3f71a81d649d3aea8f0fc7889c778b1

See more details on using hashes here.

File details

Details for the file yandexbot-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: yandexbot-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.6 Linux/6.10.12-200.fc40.x86_64

File hashes

Hashes for yandexbot-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 50cc6e59739242317b1cfd35ccc8a5daa97b84676a06528c790aa4afdbaaa814
MD5 1fd25313cc00012e292622bc59eade7a
BLAKE2b-256 284f54d99f82194255c58a9a93c0d11e34f675f6d75b24435d1183461e3f07ba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page