0
How do I check whether a command has been typed 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 don’t know where I’m going wrong, remembering that I’m inciting in 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 give me back this corrected code I really appreciate it.
It was unclear exactly what the problem with the structures is
if/else
– Woss
For example, the above code, when the user enters the command: /exec temp, will return information, but I wanted when the user type: /exec asdf, to display a warning message that the command does not exist. In the code I put the conditions IF and ESLE, but it does not work.
– HWarlley Massafera