#python #bots #telepot
#python #боты #telepot
Вопрос:
Я создаю бота на Python с помощью Telepot. Я могу заставить его отвечать на команды, но когда я внедряю встроенную клавиатуру, я не знаю, как их вызывать. Я знаю, что это должен быть обратный вызов, но я не могу найти, как его реализовать. Любая помощь будет оценена.
import sys
import time
import telepot
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print('Chat Message:', content_type, chat_type, chat_id)
if content_type == 'text':
if msg['text'] == '/start':
bot.sendMessage(chat_id, 'Welcome to @UK_Cali Teleshopn Created by JonSnow 2021',reply_markup = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="Feedback",callback_data='a'), InlineKeyboardButton(text="You",callback_data='b'),InlineKeyboardButton(text="PGP",callback_data='c'), InlineKeyboardButton(text="Cunt",callback_data='d')],
[InlineKeyboardButton(text="Products",callback_data='e')]
]
))
bot.answerCallbackQuery(callback_query_id)
bot = telepot.Bot('1646167995:AAGsOwfjcryYYkoah69QJ6XGA7koUywmuRk')
print('Listening ...')
bot.message_loop({'chat': on_chat_message}, run_forever=True)
bot = telepot.Bot('TOKEN')
Комментарии:
1. взгляните сюда
2. Я прочитал это несколько раз
3. в нем есть все ответы на ваш вопрос
4. В этом случае я слишком новичок, чтобы понять это прямо сейчас. Знаете ли вы что-нибудь, что я мог бы прочитать, что является предшественником этого. Я бы никогда не стал утверждать, что у меня есть опыт работы с python, не говоря уже о telepot
Ответ №1:
import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
TOKEN = "super secret bot token"
def on_chat_message(msg):
#here you handel messages and create the iniline keyboard
content_type, chat_type, chat_id = telepot.glance(msg)
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text='button text', callback_data='callback query data for reconizing it')],
])
def on_callback_query(msg):
#here you handels callback querys,
#the event that are fired when user clickan inline keyboard'sbutton
query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
#do something based on the callback query,
#to recognize the button pressed check query_data,
#that corresponds to he callback_data setted when creating the button
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
'callback_query': on_callback_query}).run_as_thread()
while True:
time.sleep(10)
Комментарии:
1. здесь есть упрощенная и прокомментированная версия образца документа telepot, надеюсь, вы поймете, как использовать встроенную клавиатуру