Created
November 24, 2019 20:40
-
-
Save Kylmakalle/5208426c02fdc50094117e9fcea2e2c8 to your computer and use it in GitHub Desktop.
Raspberry Pi Telegram status bot based on pyTelegramBotAPI lib.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import telebot | |
from credentials import token | |
# credentials.py | |
# -------------- | |
# token = "435038115:AAGcT63s9aOHcXT3h6SQH36lceVvh..." | |
# | |
bot = telebot.TeleBot(token) | |
def subprocess_cmd(command): | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) | |
proc_stdout = process.communicate()[0].strip() | |
return proc_stdout.decode('utf-8') | |
ip = subprocess_cmd('curl ipecho.net/plain; echo') | |
localip = subprocess_cmd('hostname -I') | |
me = 94026383 | |
def send(): | |
#wifi = round(eval( | |
# subprocess_cmd('iwconfig wlan0 | grep "Link Quality="').split('Link Quality=', 1)[1].split(' ', 1)[0]) * 100) | |
ram = subprocess_cmd('free -m').splitlines()[1][9:].split(' ', 6)[5].lstrip() | |
rom = subprocess_cmd('df -h').splitlines()[1][16:].split(' ', 4)[2].lstrip() | |
temp = subprocess_cmd('vcgencmd measure_temp').split('=', 1)[1] | |
cpu = eval(subprocess_cmd("echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')]")[2:-1]) | |
bot.send_message(me, | |
#'*Wifi:* `{}%`\n' | |
'*CPU:* `{}%`\n' | |
'*RAM:* `{} mb`\n' | |
'*ROM:* `{}`\n' | |
'*IP:* `{}`\n' | |
'*Temp: *{}\n'.format(cpu, ram, rom, localip, temp), | |
parse_mode='Markdown') | |
@bot.message_handler(commands=['start']) | |
def getstats(message): | |
bot.send_chat_action(me, 'typing') | |
send() | |
@bot.message_handler(content_types=['text']) | |
def bash(message): | |
if message.text != '/start' and message.from_user.id == me: | |
bot.send_chat_action(me, 'typing') | |
try: | |
bash_output = subprocess_cmd(message.text) | |
print(bash_output[0]) | |
bot.send_message(me, '<pre>' + bash_output + '</pre>', parse_mode='HTML') | |
except: | |
bot.reply_to(message, '<pre>Command not found</pre>', parse_mode='HTML') | |
while True: | |
try: | |
bot.polling(none_stop=True) | |
except: | |
sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment