Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

python - Strange Telegram Bot probem

I have a python Heroku app that hosts a telegram bot. It's using python-telegram-bot. It works perfectly except 1 command.. That command is /reputation.

And please read the notes at the end.

In the project directory we have:

bot.py
Procfile
requirements.txt
reptation.json

The command handler function is:

# 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)

NOTES:

I don't forgot to create the dict:

# bot.py, line 10    
reputationDict = {}

And don't think that I forgot the handler

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

The rest of the bot is ok. Please tell me what is wrong.

EDIT: Here's the Heroku logs (after I call the command without 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"

Heroku logs after I call the command with 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Revoke the bot token that you exposed through the logs or anyone can use your bot.

Also the logs show this:

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

This tells you that there are problems in your code that lead to exceptions. python-telegram-bot is designed so that exceptions during runtime don't shut your bot down. My guess is that you have an error handler registered which only logs the exception message, which hides the actual traceback.

To learn more about exception handling in PTB, please have a look at this wiki page and also this example.


Disclaimer: I'm currently the maintainer of python-telegram-bot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...