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(message.user.login, "Select an action", inline_keyboard=[btn])


@bot.on_message(phrase="/name")
def command_start(message):
    bot.send_message(message.user.login, "Type your name")
    bot.register_next_step_handler(message.user.login, type_your_name)


def type_your_name(message):
    bot.send_message(message.user.login, f"Your name is {message.text}")


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(message.user.login, f"Hello, {message.user.login}")

To send a message use bot.send_message. You can provide chat_id or login there.

bot.send_message(message.user.login, "Hello, I'm bot")

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(message.user.login, "Select an action", inline_keyboard=[btn])

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(message.user.login, "Type your name")
    bot.register_next_step_handler(message.user.login, type_your_name)

def type_your_name(message):
    bot.send_message(message.user.login, f"Your name is {message.text}")

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.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

yandex_bot_py-1.0.1-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: yandex_bot_py-1.0.1.tar.gz
  • Upload date:
  • Size: 8.9 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.1.tar.gz
Algorithm Hash digest
SHA256 e20b8ebdd902c6af3e6c326ffb499793a46d85827becf87990577744b31b5f41
MD5 6de9c651ce36c8798bbfb2e139551f01
BLAKE2b-256 d78112e1bd2628e552c9957eeb4462c0fb0d8ea502d8e315765eed1c621e13b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for yandex_bot_py-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1715a2d255ad443bbd5a629b68a523ef3302e78fbbf801b66fbe3f764dd95a5c
MD5 d0337c0f5e5fe9b83573e689cdf01bfd
BLAKE2b-256 332cd6746869850e545bd43bb02e1d9ff10d28f6129dbc48eb002b8f3d00eb55

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