1

我已经创建了内联键盘,并试图用一条消息来回答回调查询。终端正在接收查询,但我不知道用消息以及最终照片和其他内容回复它的正确语法。

我有时会收到电报错误 400。谷歌搜索后,他们说我应该在 conf.py 中确认我的令牌,我已经做到了

import sys
import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.delegate import (
   per_chat_id, create_open, pave_event_space, include_callback_query_chat_id)

def on_chat_message(msg):
   content_type, chat_type, chat_id = telepot.glance(msg)

   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')]
                               ]
                           ))

def on_callback_query(msg):
   query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
   print('Callback Query:', query_id, from_id, query_data)

   if query_data == 'a':
       bot.answerCallbackQuery(query_id, text='Welcome to @UK_Cali Teleshop')



   

bot = telepot.Bot('1646167995:AAGsOwfjcryYYkoah69QJ6XGA7koUywmuRk')
MessageLoop(bot, {'chat': on_chat_message,
                 'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

while 1:
   time.sleep(10)            

4

1 回答 1

0

要使用 msg、照片、音频、视频文档回复回调查询,您必须执行以下操作:

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  HERE based on the callback query to reply,
    #to recognize the button pressed check query_data,
    #that corresponds to he callback_data setted when creating the button


    #to send text messages:
    bot.sendMessage(chat_id, "your test message")

    #to send photo:
    bot.sendPhoto(chat_id, "C:/path/to/your/photo or https://link.to/your/photo")

    #to send video:
    bot.sendPhoto(chat_id, "C:/path/to/your/video or https://link.to/your/video")

    #to send audio:
    bot.sendPhoto(chat_id, "C:/path/to/your/audio or https://link.to/your/audio")

    #to send document:
    bot.sendPhoto(chat_id, "C:/path/to/your/document or https://link.to/your/document")

    #please note that you can do the exactly above thing 
    #in the on_chat_message to reply to a txt message
    #ans that in any case you can use if...else statement to make the 
    #reply different by the msg/inline button pressed


bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message, 
                  'callback_query': on_callback_query}).run_as_thread()

while True:
    time.sleep(10)
于 2021-03-02T06:51:30.183 回答