Introduction
Hi, Habr!
Recently I wanted to install a mouse control application on my computer, but I had problems connecting the phone and I had an idea to do something similar myself, but how? After going through a few options, I remembered about the telegram bots that may well be appropriate to do something similar in the text version, for example, you write the bot
"/ open chrome" and you have Chrome on your computer.
What do we need?
I will write a bot in Python and I will use the following libraries: flask, pyngrok, requests, win32com.client, os, subprocess. Further in the article I will tell why what library we need. We also need a Telegram on the phone, tablet, or even on another computer. And we also need a telegram bot to which we will write how you can create it yourself, because there are already enough such articles on Habré!
How will it all work?
When we write to our bot, it will send a post-request with data about the message and about the user who sent it to our url address, after which our python script depending on the text will perform certain actions such as launching applications, entering text or even shutdown computer.
')
Implementation
First, create our python file, which I’ll call main.py, import all the libraries we need (but first you need to install them via pip):
from flask import Flask, request from pyngrok import ngrok import requests import subprocess import os import win32com.client
Now, to receive requests from the bot, we need to start Flask and then start Pyngrok to access our site not only on the local network:
app = Flask(__name__)
Now we’ll do the bot setup - we’ll make the requests come to our server:
token = 'token BotFather'
Let's create a function so that the bot can respond to our messages:
def sendMessage(chat_id,text): url = URL+"sendMessage?chat_id="+str(chat_id)+"&text="+str(text) requests.get(url)
Now go to the function that will process the messages from the bot and execute the commands:
@app.route('/', methods=['POST','GET']) # index def index(): if request.method=='POST': # post r = request.get_json() # json chat_id = r["message"]["chat"]["id"] # id username = r["message"]["from"]["username"] # username text = r["message"]["text"] # msg = text.split() # cmd = msg[0] # l = len(msg) # if username==' username': # if cmd=='/cmd' and l>1: # /cmd proc = subprocess.Popen(text[5:], shell=True, stdout=subprocess.PIPE).communicate() # /cmd sendMessage(chat_id, proc[0].decode('cp866')) # Telegram elif cmd=='/ntp': # /ntp os.system('notepad.exe') # elif cmd=='/off': # /off os.system('shutdown -p') # elif cmd=='/p': # /p shell.SendKeys(text[3:]) # return 'Hello World!' # - ngrok_url
And you need to add a few lines in order for Flask to start working:
if __name__=='__main__': app.run()
A few more words from the author
Perhaps this script will not be able to completely replace computer management, but you can come up with a lot of commands from turning on the music and running your development environment. Thank you all for reading this! If you have any problems or have any questions, write in the comments, and I have it all!