How can I check if a command was spelled correctly or not? For example, if the user enters a command that does not exist, it will display a warning message, otherwise it will execute the command. I built the code partially and tried with if and else but I do not know where I'm going wrong, remembering that I'm starting in Python.
For example, the code below, when the user enters the command: /exec temp, will return an information, but I wanted it when the user typed: /exec asdf, to display a warning message that the command does not exist. In the code, I put the IF/ESLE conditions, but it does not work:
The code:
@bot.message_handler(commands=['help'])
def command_help(m):
cid = m.chat.id
help_text = "Available Commands: \n"
for key in commands:
help_text += "/" + key + ": "
help_text += commands[key] + "\n"
bot.send_message(cid, help_text)
# HERE I WANT TO MAKE CONDITION if and else:
@bot.message_handler(commands=['exec'])
def command_exec(m):
cid = m.chat.id
if m.text is not None: # PROBLEMS HERE!!
bot.send_message(cid, "Running: " + m.text[len("/exec"):])
bot.send_chat_action(cid, 'typing')
time.sleep(2)
f = os.popen(m.text[len("/exec"):])
result = f.read()
bot.send_message(cid, "Result: " + result)
else: # AND PROBLEMS HERE ALSO!!
bot.send_message(cid, " INVALID COMMAND!!")
print(color.RED + " INVALID COMMAND!! " + color.ENDC)
@bot.message_handler()
def info_opt(m):
cid = m.chat.id
txt = m.text
if txt == "TEMP": # TEMP
bot.send_message(cid, "[+] TEMPERATURES")
print(color.BLUE + "[+] TEMPERATURES" + color.ENDC)
# cpu temp
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
cpu_temp = tempFile.read()
tempFile.close()
cpu_temp = round(float(cpu_temp)/1000)
bot.send_message(cid, " [i] CPU: %s" % cpu_temp)
print(color.GREEN + " [i] CPU: %s" % cpu_temp + color.ENDC)
# gpu temp
gpu_temp = os.popen('/opt/vc/bin/vcgencmd measure_temp').read().split("=")[1][:-3]
bot.send_message(cid, " [i] GPU: %s" % gpu_temp)
print(color.GREEN + " [i] GPU: %s" % gpu_temp + color.ENDC)
bot.polling(none_stop=True)
If you can return this corrected code I thank you very much.
Aucun commentaire:
Enregistrer un commentaire