I am creating a telegram bot to send the information that I request to the app.
When I run the code below it works fine except for the last part, when it does the subprocess.call(['robocop', '--robocop', '--cpu', '--server {f}']) it doesn’t seem to put the correct data of the variable “f” because it gives me information of the server where the python code is, but not of the server that I request.
The robocop script is an app that I have created in bash
# Check CPU Status
@bot.message_handler(commands=['cpu'])
def command_long_text(m):
cid = m.chat.id
f = (m.text[len("/cpu"):])
bot.send_message(cid, "Collecting CPU information from " + f)
bot.send_chat_action(cid, 'typing')
time.sleep(3)
subprocess.call(['robocop', '--robocop', '--cpu', '--server {f}'])
Could you please help me?
If I change the variable to a string it works correctly.
subprocess.call(['robocop', '--robocop', '--cpu', '--server', 'appdb'])
Thank you!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
the right version:
cid = m.chat.id
f = (m.text[len("/cpu"):])
bot.send_message(cid, "Collecting CPU information from " + f)
bot.send_chat_action(cid, 'typing')
time.sleep(3)
subprocess.call(['robocop', '--robocop', '--cpu', f'--server {f}'])
A formatted string literal or f-string is a string literal that is prefixed with ‘f’ or ‘F’. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.
See also PEP 498.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0