Last active
July 23, 2020 00:18
-
-
Save VycktorStark/5814972dc02fc8946061b246a3594c62 to your computer and use it in GitHub Desktop.
This project aims to execute functions in the terminal using the bot, it was developed in python with one using the telepot framework.
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
__author__ = "Vycktor Stark" | |
""" | |
This is a simple project to execute a command on your terminal | |
using a telegram bot that will work with the telepot framework | |
To start the code, after the download, just enter your bot's token | |
and your account ID and then execute the following: `python3 main.py` | |
""" | |
import telepot, re, time, subprocess | |
bot, sudo_id = telepot.Bot('Token'), 12998399 #Set your bot's token, and your account id | |
def runinterminal(self): | |
""" | |
This function executes commands on the terminal in the background | |
and returns what it collects from the terminal that will be executed in a subprocess. | |
""" | |
resp = None | |
try: | |
resp = subprocess.check_output(self, shell=True).decode('utf8') | |
if (len(resp) == 0) or (resp == "\n"): | |
resp = "OK" | |
except Exception as error: | |
resp = error | |
finally: | |
return resp | |
def handle(msg): | |
""" | |
This function will receive events, but will only handle events with one item: "text" | |
and also if your ID has been validated as true when compared to the chat ID | |
If the variables are true and the command sent starts with "$sh", | |
it will be executed in the terminal and returned in chat, The result. | |
Example: | |
your: $sh ls | |
bot: main.py | |
Note: I'm using "$sh" as a command prefix, but you can change it as you like | |
""" | |
if (("text" in msg) and (str(msg['chat']['id']) == str(sudo_id))): | |
cmd, v0 = msg['text'].lower().split(), "" | |
if (cmd[0] == '$sh'): | |
bot.sendMessage(msg['chat']['id'],runinterminal(msg["text"].replace(cmd[0], ""))) | |
if __name__ == '__main__': | |
bot.message_loop(handle) | |
while True: | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment