奇怪的报文机器人问题 [英] Strange Telegram Bot probem

查看:36
本文介绍了奇怪的报文机器人问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管电报机器人的python Heroku应用程序。它使用的是蟒蛇-电报-机器人。它工作得很好,除了一个命令.. 该命令为/reputation。

请阅读结尾处的注释。

在我们的项目目录中:

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("

")
        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 | Репутация Группы:🏆
" + rd[rd.keys[0]] + ":" + rd[rd.values[0]] + "
" + rd[rd.keys[1]] + ":" + rd[rd.values[1]] + "
" + rd[rd.keys[2]] + ":" + rd[rd.values[2]] + "
" + rd[rd.keys[3]] + ":" + rd[rd.values[3]] + "
" + rd[rd.keys[4]] + ":" + rd[rd.values[4]]
    return textxtxtxttxtx
        """
        update.message.reply_text(ReputationMessage)
    print(context.args)
    update.message.reply_text(context.args)

备注:

我没有忘记创建词典:

# 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

推荐答案

吊销您通过日志公开的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的设计使得运行时的异常不会关闭你的机器人。我猜您注册了一个错误处理程序,它只记录异常消息,隐藏了实际的回溯。

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


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

这篇关于奇怪的报文机器人问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆