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