-1

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

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
  • What is wrong, is that you haven't explained to us what the actual problem is. How are we supposed to know what part of the script is not working EXACTLY as you intended? Please read the help-page [mre] and update your question accordingly. – Hampus Larsson Aug 22 '21 at 12:18
  • The bot doesn't respond to the command ¯\_(ツ)_/¯ I will put the Heroku logs – LOLOOLOLOLOLO Aug 22 '21 at 12:28
  • And I don't know how to describe the problem ¯\_(ツ)_/¯ – LOLOOLOLOLOLO Aug 22 '21 at 12:37
  • If you know there is a problem, then you KNOW how to describe the problem. How else would you know that there is a problem to begin with? – Hampus Larsson Aug 22 '21 at 12:42

1 Answers1

0

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.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
  • Ok. I revoked the token and I am going to replace the token and try to fix the errors. – LOLOOLOLOLOLO Aug 24 '21 at 06:10
  • Pls tell me how to fix `caused error "'builtin_function_or_method' object is not subscriptable"` because I have strange errors like this and I don't know from where it comes lol – LOLOOLOLOLOLO Aug 24 '21 at 07:44
  • See e.g. https://stackoverflow.com/questions/8322534 – CallMeStag Aug 24 '21 at 07:54
  • Regarding "where it comes from": that's exactly my point in the answer. if your error handler hides the traceback, you should for starters just remove it ;) – CallMeStag Aug 24 '21 at 07:55