
الروبوتات هي إحدى ميزات Telegram التي جعلت برنامج المراسلة مشهورًا للغاية. كما تمنح لوحات المفاتيح المدمجة للمطورين مزيدًا من الحرية في التفاعل مع المستخدمين.
تساعدك Keyboa على إنشاء لوحات مفاتيح مدمجة بأي تعقيد للروبوتات على أساس pyTelegramBotAPI .
في هذه المقالة ، سننظر في القدرات الأساسية للوحدة - إنشاء لوحات مفاتيح من مجموعات بيانات مختلفة ، والتوزيع التلقائي واليدوي للأزرار في صفوف ، والجمع بين عدة لوحات مفاتيح في واحدة. سوف نتعلم كيفية إنشاء عمليات استدعاء معقدة وديناميكية ، وتخزين المعلومات حول اختيار المستخدم فيها.
هذه المقالة مخصصة لأولئك الذين يعرفون أساسيات Telegram Bot API وهم على الأقل على دراية قليلة بإطار عمل pyTelegramBotAPI.
تتطلب الوحدة> Python 3.5 ويتم تثبيتها عبر نقطة:
pip install keyboa
Telegram inline_keyboard , (Array of Array of InlineKeyboardButton). () - , ( ) - . , - .
, :
import os
from telebot import TeleBot
from keyboa import keyboa_maker
token = os.environ["TELEGRAM_BOT_TOKEN"]
uid = os.environ["TELEGRAM_USER_ID"]
bot = TeleBot(token=token), .
fruits = [
"banana", "coconut", "orange",
"peach", "apricot", "apple",
"pineapple", "avocado", "melon"
], :
kb_fruits = keyboa_maker(items=fruits, copy_text_to_callback=True), - items. allback_data ( ) , , . callback_data, copy_text_to_callback.
:
bot.send_message(
chat_id=uid, reply_markup=kb_fruits,
text="Please select one of the fruit:")
.
items_in_row, . . Telegram.
:
kb_fruits = keyboa_maker(items=fruits, copy_text_to_callback=True, items_in_row=3)
bot.send_message(
chat_id=uid, reply_markup=kb_fruits,
text="Please select one of the fruit:")
, , . , auto_alignment. True, Keyboa , , , [2, 3, 7].
reverse_alignment_range , Keyboa , auto_alignment.
Keyboa . . : - , - .
fruitscomplex = [
"banana",
["coconut", "orange"],
["peach", "apricot", "apple"],
"pineapple",
["avocado", "melon"],
]
kb_fruits_complex = keyboa_maker(items=fruits_complex, copy_text_to_callback=True)
bot.send_message(
chat_id=uid, reply_markup=kb_fruits_complex,
text="Please select one of the fruit:")
, .
callback
, callback_data - , Telegram . , .
callback_data , , . callback_data .
fruits_with_ids = [
{"banana": "101"}, {"coconut": "102"}, {"orange": "103"},
{"peach": "104"}, {"apricot": "105"}, {"apple": "106"},
{"pineapple": "107"}, {"avocado": "108"}, {"melon": "109"}, ]
# or [
# ("banana", "101"), ("coconut", "102"), ("orange", "103"),
# ("peach", "104"), ("apricot", "105"), ("apple", "106"),
# ("pineapple", "107"), ("avocado", "108"), ("melon", "109"), ] , callback_data. . , callback_data, copy_text_to_callback .
kb_fruits = keyboa_maker(items=fruits_with_ids, items_in_row=3)
bot.send_message(
chat_id=uid, reply_markup=kb_fruits,
text="Please select one of the fruit:"), , , id , callback_data.
{"text": "banana", "callback_data": "101"}, ..., id. , ? , ?
Keyboa . front_marker callback_data , back_marker, , . :
kb_fruits = keyboa_maker(items=fruits_with_ids, front_marker="&fruit_id=") callback_data front_marker + value, : '&fruit_id=101', '&fruit_id=102' . . , .
, :
# , , "&fruit_id=102"
selected_fruit_id = call.data
available_weight = [1, 2, 5, 10, 100, ]
kb_available_weight = keyboa_maker(
items=available_weight, copy_text_to_callback=True,
front_marker="&weight=", back_marker=selected_fruit_id)callback_data :
'&weight=1&fruit_id=102', '&weight=2&fruit_id=102', '&weight=5&fruit_id=102'…
front_marker back_marker, , callback_data . , Telegram callback_data 64 , , - .
, , . . keyboa_combiner().
- keyboards, . , :
from keyboa import keyboa_maker, keyboa_combiner
controls = [["⏹️", "⏪️", "⏏️", "⏩️", "▶️"], ]
tracks = list(range(1, 13))
keyboard_controls = keyboa_maker(items=controls, copy_text_to_callback=True)
keyboard_tracks = keyboa_maker(items=tracks, items_in_row=4, copy_text_to_callback=True)
keyboard = keyboa_combiner(keyboards=(keyboard_tracks, keyboard_controls))
bot.send_message(
chat_id=uid, reply_markup=keyboard,
text="Please select the track number:")
- , . Telegram.
Telegram Keyboa. Github.
, .