Name | yandexbot JSON |
Version |
0.2.3
JSON |
| download |
home_page | None |
Summary | None |
upload_time | 2024-10-15 10:35:28 |
maintainer | None |
docs_url | None |
author | Ivan.Utkin |
requires_python | <4.0,>=3.11 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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:
```bash
pip install httpx
```
## Usage
### Initialization
To initialize the bot, you need to pass the OAuth token:
```python
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:
```python
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:
```python
@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:
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "yandexbot",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": null,
"author": "Ivan.Utkin",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/c0/ef/fee863cd17a6fa73cb3349d62287a3941e9898a0c9e40a76625c8e24eeb8/yandexbot-0.2.3.tar.gz",
"platform": null,
"description": "# BotAPI\n\n## Overview\n\nThe `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.\n\n## Features\n\n- Send text messages to users via `chat_id` or `login`.\n- Send file messages by specifying a file path.\n- Send image messages with an image file path.\n- Poll for new messages continuously.\n- Decorate message handlers to process specific patterns using regular expressions.\n\n## Installation\n\nEnsure you have the necessary dependencies installed:\n\n```bash\npip install httpx\n```\n\n## Usage\n\n### Initialization\n\nTo initialize the bot, you need to pass the OAuth token:\n\n```python\nfrom bot_api import BotAPI\n\nbot = BotAPI(token=\"your-oauth-token\")\n```\n\n### Sending Messages\n\nYou can send different types of messages using the following methods:\n\n- `send_text_message(message: Message, text: str)`\n- `send_file_message(message: Message, file_path: str)`\n- `send_image_message(message: Message, image_path: str)`\n\nExample to send a text message:\n\n```python\nawait bot.send_text_message(message, \"Hello, World!\")\n```\n\n### Handling Incoming Messages\n\nYou can register handlers for messages using the `@message_handler` decorator. The handler can process messages matching a specific pattern.\n\nExample:\n\n```python\n@bot.message_handler(r'^/start')\nasync def start_handler(message):\n await bot.send_text_message(message, \"Welcome to the bot!\")\n```\n\n### Polling for Messages\n\nTo start polling for new messages, use the `start_polling` method:\n\n```python\nawait bot.start_polling()\n```\n\nThis will continuously poll for new messages and process them using registered handlers.\n\n### Example Bot\n\nHere is a complete example of setting up a simple bot that responds to the `/start` command:\n\n```python\nfrom bot_api import BotAPI\n\nbot = BotAPI(token=\"your-oauth-token\")\n\n@bot.message_handler(r'^/start')\nasync def start_handler(message):\n await bot.send_text_message(message, \"Welcome to the bot!\")\n\nasync def main():\n await bot.start_polling()\n\nif __name__ == \"__main__\":\n import asyncio\n asyncio.run(main())\n```\n\n## Methods\n\n### `get_new_messages()`\n\nFetches new messages from the Yandex Bot API.\n\n### `call_api_for_messages()`\n\nMakes a request to the Yandex Bot API to retrieve updates and handles the polling offset.\n\n### `send_text_message(message: Message, text: str)`\n\nSends a text message to the recipient.\n\n### `send_file_message(message: Message, file_path: str)`\n\nSends a file message to the recipient.\n\n### `send_image_message(message: Message, image_path: str)`\n\nSends an image message to the recipient.\n\n### `get_recipient_info(message: Message)`\n\nDetermines whether to use the `login` or `chat_id` to send the message.\n\n### `message_handler(pattern)`\n\nA decorator to register a message handler for a specific pattern (regex or string).\n\n### `process_message(message: Message)`\n\nProcesses an incoming message by matching it with a registered handler.\n\n### `start_polling()`\n\nBegins polling for new messages at the defined interval.\n\n## License\n\nThis project is licensed under the MIT License.",
"bugtrack_url": null,
"license": null,
"summary": null,
"version": "0.2.3",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4e91caf67e89e6592b45e536711af043e4f507232738da963d7e948aa7e4b26c",
"md5": "e9c9b1b0d83f68aa7a331fffe45512d7",
"sha256": "8d129fb70285de77fa6b32a41013d47c09ad755213723ee16c51364d49189c8e"
},
"downloads": -1,
"filename": "yandexbot-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e9c9b1b0d83f68aa7a331fffe45512d7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 5348,
"upload_time": "2024-10-15T10:35:27",
"upload_time_iso_8601": "2024-10-15T10:35:27.464388Z",
"url": "https://files.pythonhosted.org/packages/4e/91/caf67e89e6592b45e536711af043e4f507232738da963d7e948aa7e4b26c/yandexbot-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c0effee863cd17a6fa73cb3349d62287a3941e9898a0c9e40a76625c8e24eeb8",
"md5": "1f4756b4a2594f626752f29428b77e34",
"sha256": "83637d5dee1a451d8411ad7bc68177d7134e50031a9ea81d515808384cd4223b"
},
"downloads": -1,
"filename": "yandexbot-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "1f4756b4a2594f626752f29428b77e34",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 4356,
"upload_time": "2024-10-15T10:35:28",
"upload_time_iso_8601": "2024-10-15T10:35:28.988841Z",
"url": "https://files.pythonhosted.org/packages/c0/ef/fee863cd17a6fa73cb3349d62287a3941e9898a0c9e40a76625c8e24eeb8/yandexbot-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-15 10:35:28",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "yandexbot"
}