Skip to main content

Python Yandex messenger bot api

Project description

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

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

@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.

bot.send_message("Hello, I'm bot", login=message.user.login)
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.

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

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
@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

@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

data = bot.send_file("files/test.txt", login="login@login.ru")

send_image() example. Returns the message id with the sent image.

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).

@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.

data = bot.change_chat_users("3424234", admins=[User(login="login2")], 
                            remove=[User(login="login")])

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

yandex_bot_py-1.0.6.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yandex_bot_py-1.0.6-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file yandex_bot_py-1.0.6.tar.gz.

File metadata

  • Download URL: yandex_bot_py-1.0.6.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for yandex_bot_py-1.0.6.tar.gz
Algorithm Hash digest
SHA256 eb17bbf22bf0886ba4e9e0e10b5be60a9d67759dc28bff69ca0e1cf1861f1dc0
MD5 adf865fcde8a016dc3337f35b41bb240
BLAKE2b-256 90912e31651b8c6b45e0f3b5689c4c1c99d0497b595dbd4a40217eac07428d10

See more details on using hashes here.

File details

Details for the file yandex_bot_py-1.0.6-py2.py3-none-any.whl.

File metadata

  • Download URL: yandex_bot_py-1.0.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for yandex_bot_py-1.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6063baf9f07668853d4606774e37f5d0514f4480853b074b685fff8dd5b0c7cd
MD5 a89d92ea595f9556adf1fdb6202d4d12
BLAKE2b-256 5b89688226b3ffefa33fa373101b1a3f961c0cf1ca6e6c03dba811611d0bfc62

See more details on using hashes here.

Supported by

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