# Yandex messenger bot python
### It is still under development and it has regular updates, do not forget to update it regularly
### Getting started
```
pip install yandex-bot-py
```
> Depends requests >= 2.32.3
#### Example
``` Python
from yandex_bot import Client, Button, Message, User
bot = Client(os.getenv("YANDEX_BOT_KEY"))
@bot.on_message(phrase="/start")
def command_start(message):
btn = Button(text="What is your name", phrase="/name")
bot.send_message("Select an action", login=message.user.login, inline_keyboard=[btn])
@bot.on_message(phrase="/name")
def command_start(message):
bot.send_message("Type your name", login=message.user.login)
bot.register_next_step_handler(message.user.login, type_your_name)
def type_your_name(message):
bot.send_message(f"Your name is {message.text}", login=message.user.login)
bot.run()
```
### Message processing
To process all messages starting with a specific phrase, use decorator `@bot.on_message`.
Specify in the parameters `phrase` to handle messages that begin with the specified phrase.
> `phrase` checks the first word of the text from the user
``` Python
@bot.on_message(phrase="/start")
def command_start(message):
bot.send_message(f"Hello, {message.user.login}", login=message.user.login)
```
To send a message use `bot.send_message`. You can provide `chat_id` or `login` there.
```Python
bot.send_message("Hello, I'm bot", login=message.user.login)
```
```Python
bot.send_message("Hello, I'm bot", chat_id="12512571242")
```
`inline_keyboard` is used to add buttons to a chat with a user. Just create a Button class and provide `text` (The text on the button).
You can provide `phrase` for fast binding to the processing function, or you can provide any `callback_data` to a Button and it will be returned on `Message` class in `callback_data`.
```Python
btn = Button(text="My button", phrase="/data", callback_data={"foo": "bar", "bar": "foo"})
bot.send_message("Select an action", login=message.user.login, inline_keyboard=[btn])
```
You can also `delete_message()` in chats and channels where the bot is located.
Pass the `message_id` and the `login` or `chat_id` to the method. Return `message_id` deleted message
```Python
data = bot.delete_message(12356532, login="test@login.com")
```
### Handling next step
For example, to wait for a response from a user to a question, you can use `bot.register_next_step_handler()`. This method will store the session with the current user. The method includes:
1. `user_login` - the username of the user from whom to wait for the message;
2. `callback` - the handler function
```Python
@bot.on_message(phrase="/name")
def get_user_name(message):
bot.send_message("Type your name", login=message.user.login)
bot.register_next_step_handler(message.user.login, type_your_name)
def type_your_name(message):
bot.send_message(f"Your name is {message.text}", login=message.user.login)
```
### Unhandled messages
To process messages for which no handler is specified, use the decorator `@bot.unhandled_message()`. By default, messages without a handler are not processed in any way
```Python
@bot.unhandled_message()
def unhandled(message: Message):
print(message)
```
### Send file or image
The method allows you to send files to private or group chats.
`send_file()` example. Returns a dictionary containing the `message id` and `file id`
```Python
data = bot.send_file("files/test.txt", login="login@login.ru")
```
`send_image()` example. Returns the `message id` with the sent image.
```Python
data = bot.send_image("files/test.jpg", login="login@login.ru")
```
### Chats
`create_chat()`
The method allows you to create a chat or channel, add its description and icon, assign administrators, add members (for the chat) or subscribers (for the channel).
```Python
@bot.on_message(phrase="/create_chat")
def command_create_chat(message: Message):
chat = Chat(name="Test chat 1", description="Description")
users = [User(login="login1@login.ru"), User(login="login2@login.ru")]
chat.set_admins(users)
chat_id = bot.create_chat(chat=chat)
```
`change_chat_users()`
The method allows you to add and remove participants to the chat, add and remove subscribers to the channel, as well as appoint chat or channel administrators.
```Python
data = bot.change_chat_users("3424234", admins=[User(login="login2")],
remove=[User(login="login")])
```
Raw data
{
"_id": null,
"home_page": null,
"name": "yandex-bot-py",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "api, bot, messenger, tools, yandex",
"author": null,
"author_email": "dimankaboski <dimankab@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/54/05/2cc729ae30f788b6797ad237994c2820654f9fb1ff72d8c955144f3593b5/yandex_bot_py-1.0.4.tar.gz",
"platform": null,
"description": "# Yandex messenger bot python\n\n\n### It is still under development and it has regular updates, do not forget to update it regularly\n\n### Getting started\n```\npip install yandex-bot-py\n```\n> Depends requests >= 2.32.3\n\n#### Example\n\n``` Python\nfrom yandex_bot import Client, Button, Message, User\n\nbot = Client(os.getenv(\"YANDEX_BOT_KEY\"))\n\n@bot.on_message(phrase=\"/start\")\ndef command_start(message):\n btn = Button(text=\"What is your name\", phrase=\"/name\")\n bot.send_message(\"Select an action\", login=message.user.login, inline_keyboard=[btn])\n\n\n@bot.on_message(phrase=\"/name\")\ndef command_start(message):\n bot.send_message(\"Type your name\", login=message.user.login)\n bot.register_next_step_handler(message.user.login, type_your_name)\n\n\ndef type_your_name(message):\n bot.send_message(f\"Your name is {message.text}\", login=message.user.login)\n\n\nbot.run()\n```\n\n### Message processing\nTo process all messages starting with a specific phrase, use decorator `@bot.on_message`. \nSpecify in the parameters `phrase` to handle messages that begin with the specified phrase.\n> `phrase` checks the first word of the text from the user\n\n``` Python\n@bot.on_message(phrase=\"/start\")\ndef command_start(message):\n bot.send_message(f\"Hello, {message.user.login}\", login=message.user.login)\n```\n\nTo send a message use `bot.send_message`. You can provide `chat_id` or `login` there.\n\n```Python\nbot.send_message(\"Hello, I'm bot\", login=message.user.login)\n```\n\n```Python\nbot.send_message(\"Hello, I'm bot\", chat_id=\"12512571242\")\n```\n\n`inline_keyboard` is used to add buttons to a chat with a user. Just create a Button class and provide `text` (The text on the button).\nYou can provide `phrase` for fast binding to the processing function, or you can provide any `callback_data` to a Button and it will be returned on `Message` class in `callback_data`.\n\n```Python\nbtn = Button(text=\"My button\", phrase=\"/data\", callback_data={\"foo\": \"bar\", \"bar\": \"foo\"})\nbot.send_message(\"Select an action\", login=message.user.login, inline_keyboard=[btn])\n```\nYou can also `delete_message()` in chats and channels where the bot is located.\nPass the `message_id` and the `login` or `chat_id` to the method. Return `message_id` deleted message\n```Python\ndata = bot.delete_message(12356532, login=\"test@login.com\")\n```\n### Handling next step\nFor example, to wait for a response from a user to a question, you can use `bot.register_next_step_handler()`. This method will store the session with the current user. The method includes:\n1. `user_login` - the username of the user from whom to wait for the message;\n2. `callback` - the handler function\n\n```Python\n@bot.on_message(phrase=\"/name\")\ndef get_user_name(message):\n bot.send_message(\"Type your name\", login=message.user.login)\n bot.register_next_step_handler(message.user.login, type_your_name)\n\ndef type_your_name(message):\n bot.send_message(f\"Your name is {message.text}\", login=message.user.login)\n```\n### Unhandled messages\n\nTo process messages for which no handler is specified, use the decorator `@bot.unhandled_message()`. By default, messages without a handler are not processed in any way\n\n```Python\n@bot.unhandled_message()\ndef unhandled(message: Message):\n print(message)\n```\n\n\n\n### Send file or image\nThe method allows you to send files to private or group chats. \n\n`send_file()` example. Returns a dictionary containing the `message id` and `file id`\n```Python\ndata = bot.send_file(\"files/test.txt\", login=\"login@login.ru\")\n```\n`send_image()` example. Returns the `message id` with the sent image.\n```Python\ndata = bot.send_image(\"files/test.jpg\", login=\"login@login.ru\")\n```\n\n### Chats\n`create_chat()`\n\nThe method allows you to create a chat or channel, add its description and icon, assign administrators, add members (for the chat) or subscribers (for the channel).\n```Python\n@bot.on_message(phrase=\"/create_chat\")\ndef command_create_chat(message: Message):\n chat = Chat(name=\"Test chat 1\", description=\"Description\")\n users = [User(login=\"login1@login.ru\"), User(login=\"login2@login.ru\")]\n chat.set_admins(users)\n chat_id = bot.create_chat(chat=chat)\n```\n`change_chat_users()`\n\nThe method allows you to add and remove participants to the chat, add and remove subscribers to the channel, as well as appoint chat or channel administrators.\n\n```Python\ndata = bot.change_chat_users(\"3424234\", admins=[User(login=\"login2\")], \n remove=[User(login=\"login\")])\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python Yandex messenger bot api",
"version": "1.0.4",
"project_urls": {
"Homepage": "https://github.com/dimankaboski/yandex-bot-py",
"Issues": "https://github.com/dimankaboski/yandex-bot-py/issues",
"Repository": "https://github.com/dimankaboski/yandex-bot-py"
},
"split_keywords": [
"api",
" bot",
" messenger",
" tools",
" yandex"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "192dd20c2a90df7c29b5d62df6160642315b10452db531b7f979ff50c3488b67",
"md5": "eb619ea2762527daea582a81d506c6ea",
"sha256": "14a9a759e1498926b2e12a5fc9977a9ecb49b40b7b39bef1628b55453bfa9a57"
},
"downloads": -1,
"filename": "yandex_bot_py-1.0.4-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "eb619ea2762527daea582a81d506c6ea",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 9877,
"upload_time": "2024-11-18T09:54:40",
"upload_time_iso_8601": "2024-11-18T09:54:40.558257Z",
"url": "https://files.pythonhosted.org/packages/19/2d/d20c2a90df7c29b5d62df6160642315b10452db531b7f979ff50c3488b67/yandex_bot_py-1.0.4-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "54052cc729ae30f788b6797ad237994c2820654f9fb1ff72d8c955144f3593b5",
"md5": "0c284ad3ad0a3525e7ada962ff391f95",
"sha256": "3322af4513284f6364b64e7c686c25bb061b77ab591051b4ba0b7a0d12cc8245"
},
"downloads": -1,
"filename": "yandex_bot_py-1.0.4.tar.gz",
"has_sig": false,
"md5_digest": "0c284ad3ad0a3525e7ada962ff391f95",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11240,
"upload_time": "2024-11-18T09:54:42",
"upload_time_iso_8601": "2024-11-18T09:54:42.912825Z",
"url": "https://files.pythonhosted.org/packages/54/05/2cc729ae30f788b6797ad237994c2820654f9fb1ff72d8c955144f3593b5/yandex_bot_py-1.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-18 09:54:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dimankaboski",
"github_project": "yandex-bot-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": []
},
{
"name": "requests",
"specs": []
},
{
"name": "python-dotenv",
"specs": []
},
{
"name": "pytest-cov",
"specs": []
}
],
"lcname": "yandex-bot-py"
}