首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Telepot机器人嵌入式键盘

Python Telepot机器人嵌入式键盘
EN

Stack Overflow用户
提问于 2021-03-01 19:37:08
回答 1查看 95关注 0票数 1

我正在使用Telepot在Python中构建一个机器人。我能够让它响应命令,但当我实现内联键盘时,我不知道如何调用它们。我知道它必须是callbackquery,但我不知道如何实现它。任何帮助都将不胜感激。

代码语言:javascript
复制
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 Teleshop\n      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')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-01 21:43:06

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66421436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档