Python Telegram Game Bot 功能错误 0x7fcfa257f790

Python Telegram Game Bot function error at 0x7fcfa257f790

我是 Python 的新手,最近碰碰运气设置了一个由 Mark Powers 制作的名为 Telegram Arcade

很明显,它是用 python-telegram-bot 框架构建的。尽管设置它看起来很简单(附带说明),但我无法让它工作。

即使在我更新了一些代码以符合对框架的更改后,现在我仍然收到与机器人交互的用户一起显示的错误:函数错误在 0x7fcfa257f790 .

目前代码如下:

import configparser, threading, requests, json, re, time, sys

from uuid import uuid4

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram import InlineQueryResultGame, ParseMode, InputTextMessageContent
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, InlineQueryHandler, CommandHandler
from telegram.ext import CallbackContext

from http.server import HTTPServer, BaseHTTPRequestHandler

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


class Global:
    def __init__(self):
        return

class GameHTTPRequestHandler(BaseHTTPRequestHandler):
    def __init__(self, *args):
        BaseHTTPRequestHandler.__init__(self, *args)

    def do_GET(self):
        if "#" in self.path:
            self.path = self.path.split("#")[0]
        if "?" in self.path:
            (route, params) = self.path.split("?")
        else:
            route = self.path
            params = ""
        route = route[1:]
        params = params.split("&")
        if route in Global.games:
            self.send_response(200)
            self.end_headers()
            self.wfile.write(open(route+'.html', 'rb').read())
        elif route == "setScore":
            params = {}
            for item in self.path.split("?")[1].split("&"):
                if "=" in item:
                    pair = item.split("=")
                    params[pair[0]] = pair[1]
            print(params)
            if "imid" in params:
                Global.bot.set_game_score(params["uid"], params["score"], inline_message_id=params["imid"]) 
            else:
                Global.bot.set_game_score(params["uid"], params["score"], message_id=params["mid"], chat_id=params["cid"])
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Set score')
        else:
            self.send_response(404)
            self.end_headers()
            self.wfile.write(b'Invalid game!')

def start(update, context):
    bot.send_game(update.message.chat_id, Global.featured)

def error(update, context):
    print(update, error)

def button(update, context):
    print(update)
    query = update.callback_query
    game = query.game_short_name
    uid = str(query.from_user.id)
    if query.message:
        mid = str(query.message.message_id)
        cid = str(query.message.chat.id)
        url = "http://" + Global.host + ":"+Global.port + "/" + game + "?uid="+uid+"&mid="+mid+"&cid="+cid
    else:
        imid = update.callback_query.inline_message_id
        url = "http://" + Global.host + ":"+Global.port + "/" + game + "?uid="+uid+"&imid="+imid
    print(url)
    bot.answer_callback_query(query.id, text=game, url=url)

def inlinequery(update, context):
    query = context.inline_query.query
    results = []
    for game in Global.games:
        if query.lower() in game.lower():
            results.append(InlineQueryResultGame(id=str(uuid4()),game_short_name=game))
    context.inline_query.answer(results)
def main():
    config = configparser.ConfigParser()
    config.read('config.ini')
    token = config['DEFAULT']['API_KEY']
    Global.games = config['DEFAULT']['GAMES'].split(',')
    Global.host = config['DEFAULT']['HOST']
    Global.port = config['DEFAULT']['PORT']
    Global.featured = config['DEFAULT']['FEATURED']
    updater = Updater(token=token, use_context=True)

    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(InlineQueryHandler(inlinequery))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))
    updater.dispatcher.add_error_handler(error)
    Global.bot = updater.bot

    print("Polling telegram")
    updater.start_polling()

    print("Starting http server")   
    http = HTTPServer((Global.host, int(Global.port)), GameHTTPRequestHandler)
    http.serve_forever()


if __name__ == '__main__':
    main()

我一直在阅读文档并研究示例,但似乎找不到解决方案。甚至 inlineCommands 也无法显示类似的错误。如果有任何建议,我将不胜感激,很抱歉我不是专家来解释我的情况。谢谢!

python-telegram-bot 改变了回调在两年前发布的 v12 中的工作方式。您正在使用的回购协议似乎仍然适用于旧的回调。我建议首先尝试并让它与 ptb 11.1 版一起使用。或 12.0 without passing use_context=True(在 v12 中,这仍然默认为 False 以实现向后兼容性)。如果一切正常并且您想升级到更新的 python-telegram-bot 版本,我强烈建议您阅读 v12 and v13.

的转换指南

免责声明:我目前是 python-telegram-bot.

的维护者