• Home
  • Pyrogram
  • Формирование и обработка текстовых кнопок в Pyrogram

Формирование и обработка текстовых кнопок в Pyrogram

Image

В видеоуроке по Pyrogram мы изучим, как создавать и отправлять текстовые кнопки пользователям, а также обрабатывать их нажатия. Узнаем, какие возможности предоставляет Pyrogram для формирования кнопок, как добавлять текст и значки на кнопки, и как применять их в ботах. Получите практические навыки и знания, которые помогут вам создавать более удобные и интерактивные боты. Смотрите видео и начинайте использовать кнопки в своих проектах Pyrogram уже сегодня!

from pyrogram import Client, idle, filters
from pyrogram.enums import ParseMode
from pyrogram.handlers import MessageHandler
from pyrogram.types import Message, BotCommand, ReplyKeyboardMarkup, KeyboardButton

api_id = 12345678
api_hash = 'abcdefghijklmnopqrstuvwxyz'

bot_token = '9876543210:abcdefghijklm'

client = Client(name='me_client_bot', api_id=api_id, api_hash=api_hash, parse_mode=ParseMode.HTML)

reply_keyboard = ReplyKeyboardMarkup(keyboard=[
    [
        KeyboardButton(
            text='Basic button'
        ),
        KeyboardButton(
            text='Send contact',
            request_contact=True
        )
    ],
    [
        KeyboardButton(
            text='Send location',
            request_location=True
        )
    ]
], resize_keyboard=True, one_time_keyboard=True, placeholder='Press any button')


def command_start(client: Client, message: Message):
    message.reply('Hi! You entered start command.', reply_markup=reply_keyboard)


def all_reply_button(_, message: Message):
    message.reply(f'You press button or sent message {message.text}')


def location(_, message: Message):
    message.reply(f'You sent location {message.location}')


def contact(_, message: Message):
    message.reply(f'You sent contact {message.contact}')


def photo(client: Client, message: Message):
    message.reply(f'You sent picture')
    client.download_media(message)
    message.download()


client.add_handler(MessageHandler(photo, filters.photo))
client.add_handler(MessageHandler(command_start, filters.command(commands='start')))
client.add_handler(MessageHandler(location, filters.location))
client.add_handler(MessageHandler(contact, filters.contact))
client.add_handler(MessageHandler(all_reply_button))

bot_commands = [
    BotCommand(
        command='start',
        description='Get started'
    ),
    BotCommand(
        command='run',
        description='Launch'
    ),
    BotCommand(
        command='go',
        description='Go to'
    )
]

client.start()
client.set_bot_commands(bot_commands)
idle()
client.stop()
Image