python - TELEPOT 尝试回调查询以发送消息时未找到聊天时不断收到错误 400
问题描述
这是我的代码,我的 conf.py 文件只是我仔细检查过的 API 令牌和时区。我试图让我的机器人通过发送消息或照片来响应按钮单击。我知道我需要使用 sendPhoto 函数,但我不知道如何在回调中激活它
import sys
import time
import os
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')]
]
))
bot.sendMessage(chat_id, 'Quanti anni hai?', reply_markup=keyboard)
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.sendMessage(query_id, 'ddd')
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)
解决方案
您收到 400 错误,因为聊天 ID 不是quesry_id
代表查询,而是from_id
代表聊天,所以:
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.sendMessage(from_id, 'ddd')
推荐阅读
- c# - 使用 Microsoft Graph 从 Office 365 邮箱中获取退回的电子邮件
- javascript - 如何仅从一个脚本运行所有脚本?(带有 React.js 的 HTML)
- javascript - 如何使用快递服务目录?
- javascript - 如何使用 HTML 画布裁剪图像并调整其大小而不使其模糊?
- java - 为什么浮点除法会返回整数结果(Java/Android)
- spring-boot - Springfox:如何手动将 api 路径添加到 swagger-ui?
- sql-server - 解释 SQL Server 表统计信息
- r - 如何有条件地检查和替换 xts 对象中的数据?
- haskell - 类子类结构的设计模式
- sql-server - SQL Server 存储过程中的连续 While 循环