How do 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 Python:
Code:
@bot.message_handler(commands=['ajuda'])
def command_help(m):
cid = m.chat.id
help_text = "Comandos disponíveis: \n"
for key in commands:
help_text += "/" + key + ": "
help_text += commands[key] + "\n"
bot.send_message(cid, help_text)
# AQUI QUE QUERO FAZER A CONDIÇÃO if e else:
@bot.message_handler(commands=['exec'])
def command_exec(m):
cid = m.chat.id
if m.text is not None: # PROBLEMAS AQUI!!
bot.send_message(cid, "Ejecutando: " + 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, "Resultado: " + result)
else: # E PROBLEMAS AQUI TAMBÉM!!
bot.send_message(cid, " COMANDO INVÁLIDO!!")
print(color.RED + " COMANDO INVÁLIDO!! " + color.ENDC)
@bot.message_handler()
def info_opt(m):
cid = m.chat.id
txt = m.text
if txt == "TEMP": # TEMP
bot.send_message(cid, "[+] TEMPERATURAS")
print(color.BLUE + "[+] TEMPERATURAS" + 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.