Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import telebot
- import urllib
- import urllib2
- import time
- import os
- TOKEN = ''
- bot = telebot.TeleBot(TOKEN)
- chatid =
- username = ""
- password = ""
- motionSaveDirectory = "/media/mountpoint"
- print "Start listening"
- bot.send_message(chatid, "Ciao tutti")
- @bot.message_handler(commands=['status'])
- def status (message):
- print "Status Message received"
- #Add password manager
- password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- #Add username and password
- url = "http://192.168.1.5:8080/0/detection/status"
- password_mngr.add_password(None, url, username, password)
- handler= urllib2.HTTPBasicAuthHandler(password_mngr)
- #Create "opener" (Open Director Instance)
- opener = urllib2.build_opener(handler)
- #Use the opener
- result = opener.open(url)
- #Install the opener
- urllib2.install_opener(opener)
- string = result.read()
- msg = string[string.index("status"): string.index("</body>")]
- print msg
- bot.reply_to(message, msg)
- @bot.message_handler(commands=['start'])
- def send_foto (message):
- print "Start Message received"
- #Add password manager
- password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- #Add username and password
- url = "http://192.168.1.5:8080/0/detection/start"
- password_mngr.add_password(None, url, username, password)
- handler= urllib2.HTTPBasicAuthHandler(password_mngr)
- #Create "opener" (Open Director Instance)
- opener = urllib2.build_opener(handler)
- #Use the opener
- opener.open(url)
- #Install the opener
- urllib2.install_opener(opener)
- bot.reply_to(message, "Start Motion")
- @bot.message_handler(commands=['pause'])
- def pause (message):
- print "Start Message received"
- #Add password manager
- password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- #Add username and password
- url = "http://192.168.1.5:8080/0/detection/pause"
- password_mngr.add_password(None, url, username, password)
- handler= urllib2.HTTPBasicAuthHandler(password_mngr)
- #Create "opener" (Open Director Instance)
- opener = urllib2.build_opener(handler)
- #Use the opener
- opener.open(url)
- #Install the opener
- urllib2.install_opener(opener)
- bot.reply_to(message, "Pause Motion")
- @bot.message_handler(commands=['foto'])
- def send_foto (message):
- print "Foto Message received"
- #Add password manager
- password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- #Add username and password
- url = "http://localhost:8080/0/config/set?emulate_motion=on"
- password_mngr.add_password(None, url, username, password)
- handler= urllib2.HTTPBasicAuthHandler(password_mngr)
- #Create "opener" (Open Director Instance)
- opener = urllib2.build_opener(handler)
- #Use the opener
- opener.open(url)
- #Install the opener
- urllib2.install_opener(opener)
- time.sleep( 0.2 )
- url = "http://localhost:8080/0/config/set?emulate_motion=off"
- password_mngr.add_password(None, url, username, password)
- handler= urllib2.HTTPBasicAuthHandler(password_mngr)
- opener = urllib2.build_opener(handler)
- opener.open(url)
- urllib2.install_opener(opener)
- @bot.message_handler(commands=['help'])
- def help (message):
- msg = """
- ********
- Bot Help
- ********
- To write a command use "/" and the command name, for example "/status"
- Commands:
- ----------------
- Motion commands:
- ----------------
- \t /status \t-> Give the status of motion [ACTIVE|PAUSED]
- \t /start \t-> Start motion
- \t /pause \t-> Pause motion
- \t /foto \t-> Send a foto from webcam
- ----------------
- System commands:
- ----------------
- \t /reboot \t-> Reboot system
- \t /size \t-> Get the size of a given directory (where are saving motion images)
- \t /ping \t-> Ping to 10.8.0.1 , show if it is connected to openvpn
- \t /help \t-> Show this help message
- If you write an unknown message the bot will respond with the same like a ping
- In adittion, acces to streaming using openvpn:
- sudo openvpn --config rrb.ovpn \t <- Start openvpn
- http://10.8.0.100:8080 \t <- Configuration web interface
- http://10.8.0.100:8081 \t <- Acces to camera 1
- Username:rako
- """
- print "Giving help"
- bot.reply_to(message, msg)
- @bot.message_handler(commands=['reboot'])
- def reboot(message):
- bot.reply_to(message, "Rebooting system")
- command = "/usr/bin/sudo /sbin/shutdown -r now"
- import subprocess
- process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
- output = process.communicate()[0]
- print output
- @bot.message_handler(commands=['size'])
- def size(message):
- response = subprocess.check_output('du -h ' + motionSaveDirectory, shell=True)
- #msg = response[:response.index("t") ]
- msg = response[0:response.index("\n")]
- print msg
- bot.reply_to(message, msg)
- @bot.message_handler(commands=['ping'])
- def reboot(message):
- hostname = "10.8.0.1" #example
- response = os.system("ping -c 1 " + hostname)
- #and then check the response...
- if response == 0:
- bot.reply_to(message, "is up!")
- print hostname, 'is up!'
- else:
- bot.reply_to(message,"is down!")
- print hostname, 'is down!'
- @bot.message_handler(func=lambda message: True)
- def echo_message (message):
- print "Message received"
- bot.reply_to(message, message.text)
- bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement