我已经用 Telepot 12.7 实现了很多电报机器人,从来没有遇到过一个问题。现在,突然,在将我的机器人添加到一个组后,它开始向这个错误消息发送垃圾邮件:
raise KeyError('No suggested keys %s in %s' % (str(keys), str(d)))
KeyError: "No suggested keys ['message', 'edited_message', 'channel_post', 'edited_channel_post', 'callback_query', 'inline_query', 'chosen_inline_result', 'shipping_query', 'pre_checkout_query'] in {'update_id': 676740400, 'my_chat_member': {'chat': {'id': -500413139, 'title': 'NazTestGroup', 'type': 'group', 'all_members_are_administrators': True}, 'from': {'id': 172579176, 'is_bot': False, 'first_name': 'Nazareno Descanso', 'username': 'Descanso7', 'language_code': 'en'}, 'date': 1616661428, 'old_chat_member': {'user': {'id': 1684936782, 'is_bot': True, 'first_name': 'telegram-reputation', 'username': 'reputationhmci_bot'}, 'status': 'member'}, 'new_chat_member': {'user': {'id': 1684936782, 'is_bot': True, 'first_name': 'telegram-reputation', 'username': 'reputationhmci_bot'}, 'status': 'left'}}}"
问题似乎是这样的:https ://github.com/nickoala/telepot/issues/184 但这是 2016 年解决的一个老问题。我尝试将“new_chat_member”添加到键盘映射中,通过此修复,机器人启动但它没有响应,没有任何东西传递给“handle(msg)”方法。如果您在私人聊天中而不是在群组中写信,它会响应。
这是我初始化机器人的代码:
def handle(msg):
global users_manager
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id, msg)
try:
userid = get_userid(chat_id, msg)
users_manager.add_user(userid)
if content_type == "text" and msg["text"] == "/start":
bot.sendMessage(chat_id, f"Ciao! Sono reputation_bot, vi darò punti per ogni messaggio scritto "
f"potendo guadagnare così livelli esclusivi!")
else:
users_manager.get_user(userid).add_points(1)
check_if_lucky_message(userid, chat_id, msg)
check_current_level(userid, chat_id, msg)
print(users_manager.get_user(userid))
pickle.dump(users_manager, open("score_dict.pkl", mode="wb"))
except Exception as e:
print(f"Error:: {e}")
def main(argv):
global bot
global users_manager
global levels_manager
print(f"Bot started, token {TOKEN}")
bot = telepot.Bot(TOKEN)
levels_manager = LevelManager()
users_manager = pickle.load(open("score_dict.pkl", mode="rb")) \
if os.path.exists("score_dict.pkl") else UsersManager()
print(levels_manager)
print(users_manager)
bot.message_loop(handle, run_forever=True)
if __name__ == "__main__":
main(sys.argv[1:])