奇怪的电报机器人问题

2024-10-02 12:33:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python Heroku应用程序,它承载一个电报机器人。它使用python电报机器人。除了一个命令外,它工作得很好。。 这是一个命令/声誉

请阅读最后的注释

在项目目录中,我们有:

bot.py
Procfile
requirements.txt
reptation.json

命令处理程序的功能是:

# code from bot.py, from line 75 to line 105, Additional comments added
    def reputation(update, context):
    sendername = update.message.from_user.username
    print(sendername)
    if context.args != [] and sendername == "[censored for privacy]": # +sendername check
        # Add reputation to someone, 3 args: target (@username), operation (+, -, =), amount (1)
        target = context.args[0]
        operation = context.args[1]
        amount = context.args[2]
        f = open("reputation.json", "r")
        reputationDict = json.load(f)
        f.close()
        amountTarget = reputationDict[target] 
        if operation == "+":
            amountTarget += amount
        elif operation == "-":
            amountTarget -= amount
        elif operation == "=":
            amountTarget = amount
        out_file = open("reputation.json", "w")
        json.dump(reputationDict, out_file, indent = 6)
        out_file.close()
    elif context.args == []:
        # send reputation
        reputationDict = json.load(f)
        f.close()
        print("\n\n")
        print(reputationDict)
        ReputationMessage = parseReputationMessage(reputationDict)
        # parseReputationMessage is a function that I created behind this piece of code. It takes the dict and returns a string with the reputation message. Here is it:
        """
        def parseReputationMessage(rd):
    textxtxtxttxtx = "🏅Group Reputation | Репутация Группы:🏆\n" + rd[rd.keys[0]] + ":" + rd[rd.values[0]] + "\n" + rd[rd.keys[1]] + ":" + rd[rd.values[1]] + "\n" + rd[rd.keys[2]] + ":" + rd[rd.values[2]] + "\n" + rd[rd.keys[3]] + ":" + rd[rd.values[3]] + "\n" + rd[rd.keys[4]] + ":" + rd[rd.values[4]]
    return textxtxtxttxtx
        """
        update.message.reply_text(ReputationMessage)
    print(context.args)
    update.message.reply_text(context.args)

注意事项:

我没有忘记创建dict:

# bot.py, line 10    
reputationDict = {}

别以为我忘了处理人

# code from bot.py, line 126
dp.add_handler(CommandHandler("reputation", reputation, pass_args=True))

机器人的其余部分还可以。请告诉我怎么了

编辑:以下是Heroku日志(在我调用不带参数的命令之后):

    2021-08-22T12:29:52.711168+00:00 app[web.1]: 2021-08-22 12:29:52,711 - telegram.ext.updater - INFO - Received signal 15 (SIGTERM), stopping...
2021-08-22T12:29:52.906466+00:00 heroku[web.1]: Process exited with status 0
2021-08-22T12:29:54.933584+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2021-08-22T12:29:58.545154+00:00 heroku[web.1]: State changed from starting to up
2021-08-22T12:30:50.310838+00:00 heroku[web.1]: Restarting
2021-08-22T12:30:50.316358+00:00 heroku[web.1]: State changed from up to starting
2021-08-22T12:30:50.953205+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-08-22T12:30:51.008821+00:00 app[web.1]: 2021-08-22 12:30:51,008 - telegram.ext.updater - INFO - Received signal 15 (SIGTERM), stopping...
2021-08-22T12:30:51.742152+00:00 heroku[web.1]: Process exited with status 0
2021-08-22T12:30:53.316367+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2021-08-22T12:30:57.810884+00:00 heroku[web.1]: State changed from starting to up
2021-08-22T12:31:06.708324+00:00 heroku[router]: at=info method=POST path="/1925434978:AAFenjLOuM_dV76IMo_vhrvTenzD5JaBMvk" host=python-is-1090101110100-bot.herokuapp.com request_id=45266fe8-d8ed-45f6-ad88-92fb93a1705d fwd="91.108.6.158" dyno=web.1 connect=0ms service=2ms status=200 bytes=170 protocol=https
2021-08-22T12:31:06.705601+00:00 app[web.1]: VadymShk
2021-08-22T12:31:06.705938+00:00 app[web.1]: 2021-08-22 12:31:06,705 - __main__ - WARNING - Update "{'update_id': 728926592, 'message': {'message_id': 165, 'date': 1629635466, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}, '_effective_user': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}, '_effective_chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, '_effective_message': {'message_id': 165, 'date': 1629635466, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}}" caused error "local variable 'f' referenced before assignment"

Heroku在我使用args调用命令后记录:

2021-08-22T12:29:52.711168+00:00 app[web.1]: 2021-08-22 12:29:52,711 - telegram.ext.updater - INFO - Received signal 15 (SIGTERM), stopping...
2021-08-22T12:29:52.906466+00:00 heroku[web.1]: Process exited with status 0
2021-08-22T12:29:54.933584+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2021-08-22T12:29:58.545154+00:00 heroku[web.1]: State changed from starting to up
2021-08-22T12:30:50.310838+00:00 heroku[web.1]: Restarting
2021-08-22T12:30:50.316358+00:00 heroku[web.1]: State changed from up to starting
2021-08-22T12:30:50.953205+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-08-22T12:30:51.008821+00:00 app[web.1]: 2021-08-22 12:30:51,008 - telegram.ext.updater - INFO - Received signal 15 (SIGTERM), stopping...
2021-08-22T12:30:51.742152+00:00 heroku[web.1]: Process exited with status 0
2021-08-22T12:30:53.316367+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2021-08-22T12:30:57.810884+00:00 heroku[web.1]: State changed from starting to up
2021-08-22T12:31:06.708324+00:00 heroku[router]: at=info method=POST path="/1925434978:AAFenjLOuM_dV76IMo_vhrvTenzD5JaBMvk" host=python-is-1090101110100-bot.herokuapp.com request_id=45266fe8-d8ed-45f6-ad88-92fb93a1705d fwd="91.108.6.158" dyno=web.1 connect=0ms service=2ms status=200 bytes=170 protocol=https
2021-08-22T12:31:06.705601+00:00 app[web.1]: VadymShk
2021-08-22T12:31:06.705938+00:00 app[web.1]: 2021-08-22 12:31:06,705 - __main__ - WARNING - Update "{'update_id': 728926592, 'message': {'message_id': 165, 'date': 1629635466, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}, '_effective_user': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}, '_effective_chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, '_effective_message': {'message_id': 165, 'date': 1629635466, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}}" caused error "local variable 'f' referenced before assignment"
2021-08-22T12:32:37.465600+00:00 app[web.1]: VadymShk
2021-08-22T12:32:37.466462+00:00 app[web.1]: 2021-08-22 12:32:37,466 - __main__ - WARNING - Update "{'update_id': 728926593, 'message': {'message_id': 166, 'date': 1629635556, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation @newUser = 1', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}, {'type': 'mention', 'offset': 12, 'length': 8}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}, '_effective_user': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}, '_effective_chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, '_effective_message': {'message_id': 166, 'date': 1629635556, 'chat': {'id': 1070443704, 'type': 'private', 'username': 'VadymShk', 'first_name': 'Vadym', 'last_name': 'Shkvarchuk'}, 'text': '/reputation @newUser = 1', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 11}, {'type': 'mention', 'offset': 12, 'length': 8}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 1070443704, 'first_name': 'Vadym', 'is_bot': False, 'last_name': 'Shkvarchuk', 'username': 'VadymShk', 'language_code': 'ru'}}}" caused error "Expecting ',' delimiter: line 3 column 2 (char 21)"
2021-08-22T12:32:37.467048+00:00 heroku[router]: at=info method=POST path="/1925434978:AAFenjLOuM_dV76IMo_vhrvTenzD5JaBMvk" host=python-is-1090101110100-bot.herokuapp.com request_id=9d9b3a96-c08f-48b0-b000-5b72bdc43746 fwd="91.108.6.158" dyno=web.1 connect=0ms service=1ms status=200 bytes=170 protocol=https

Tags: nameidfalseherokubotchatusernamerd
1条回答
网友
1楼 · 发布于 2024-10-02 12:33:28

撤消通过日志公开的bot令牌,否则任何人都可以使用您的bot

日志还显示:

2021-08-22T12:31:06.705938+00:00 app[web.1]: 2021-08-22 12:31:06,705 - __main__ - WARNING - Update "{'update_id': ...}" caused error "local variable 'f' referenced before assignment"
...
2021-08-22T12:32:37.466462+00:00 app[web.1]: 2021-08-22 12:32:37,466 - __main__ - WARNING - Update "{'update_id': ...}" caused error "Expecting ',' delimiter: line 3 column 2 (char 21)"

这说明代码中存在导致异常的问题python-telegram-bot的设计使运行时的异常不会关闭bot。我猜您注册了一个错误处理程序,它只记录异常消息,这隐藏了实际的回溯

要了解有关PTB中异常处理的更多信息,请查看此wiki page 和此example


免责声明:我目前是python-telegram-bot的维护者

相关问题 更多 >

    热门问题