Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. import subprocess
  2. import telebot
  3. import urllib
  4. import urllib2
  5. import time
  6. import os
  7.  
  8. TOKEN = ''
  9. bot = telebot.TeleBot(TOKEN)
  10. chatid =
  11.  
  12.  
  13. username = ""
  14. password = ""
  15.  
  16. motionSaveDirectory = "/media/mountpoint"
  17.  
  18. print "Start listening"
  19.  
  20. bot.send_message(chatid, "Ciao tutti")
  21.  
  22.  
  23. @bot.message_handler(commands=['status'])
  24. def status (message):
  25.     print "Status Message received"
  26.  
  27.     #Add password manager
  28.     password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  29.     #Add username and password
  30.     url = "http://192.168.1.5:8080/0/detection/status"
  31.     password_mngr.add_password(None, url, username, password)
  32.     handler=  urllib2.HTTPBasicAuthHandler(password_mngr)
  33.  
  34.  
  35.     #Create "opener" (Open Director Instance)
  36.     opener = urllib2.build_opener(handler)
  37.     #Use the opener
  38.     result = opener.open(url)
  39.     #Install the opener
  40.     urllib2.install_opener(opener)
  41.  
  42.     string = result.read()
  43.  
  44.     msg = string[string.index("status"): string.index("</body>")]
  45.     print msg
  46.    
  47.     bot.reply_to(message, msg)
  48.  
  49. @bot.message_handler(commands=['start'])
  50. def send_foto (message):
  51.     print "Start Message received"
  52.  
  53.     #Add password manager
  54.     password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  55.  
  56.     #Add username and password
  57.     url = "http://192.168.1.5:8080/0/detection/start"
  58.     password_mngr.add_password(None, url, username, password)
  59.  
  60.     handler=  urllib2.HTTPBasicAuthHandler(password_mngr)
  61.  
  62.     #Create "opener" (Open Director Instance)
  63.     opener = urllib2.build_opener(handler)
  64.  
  65.     #Use the opener
  66.     opener.open(url)
  67.  
  68.     #Install the opener
  69.     urllib2.install_opener(opener)
  70.    
  71.     bot.reply_to(message, "Start Motion")
  72.  
  73. @bot.message_handler(commands=['pause'])
  74. def pause (message):
  75.     print "Start Message received"
  76.  
  77.     #Add password manager
  78.     password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  79.  
  80.     #Add username and password
  81.     url = "http://192.168.1.5:8080/0/detection/pause"
  82.     password_mngr.add_password(None, url, username, password)
  83.  
  84.     handler=  urllib2.HTTPBasicAuthHandler(password_mngr)
  85.  
  86.     #Create "opener" (Open Director Instance)
  87.     opener = urllib2.build_opener(handler)
  88.  
  89.     #Use the opener
  90.     opener.open(url)
  91.  
  92.     #Install the opener
  93.     urllib2.install_opener(opener)
  94.    
  95.     bot.reply_to(message, "Pause Motion")
  96.  
  97.  
  98.  
  99. @bot.message_handler(commands=['foto'])
  100. def send_foto (message):
  101.     print "Foto Message received"
  102.  
  103.     #Add password manager
  104.     password_mngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  105.  
  106.     #Add username and password
  107.     url = "http://localhost:8080/0/config/set?emulate_motion=on"
  108.     password_mngr.add_password(None, url, username, password)
  109.  
  110.     handler=  urllib2.HTTPBasicAuthHandler(password_mngr)
  111.  
  112.     #Create "opener" (Open Director Instance)
  113.     opener = urllib2.build_opener(handler)
  114.  
  115.     #Use the opener
  116.     opener.open(url)
  117.  
  118.     #Install the opener
  119.     urllib2.install_opener(opener)
  120.  
  121.     time.sleep( 0.2 )
  122.  
  123.     url = "http://localhost:8080/0/config/set?emulate_motion=off"
  124.     password_mngr.add_password(None, url, username, password)
  125.     handler=  urllib2.HTTPBasicAuthHandler(password_mngr)
  126.     opener = urllib2.build_opener(handler)
  127.     opener.open(url)
  128.     urllib2.install_opener(opener)
  129.  
  130. @bot.message_handler(commands=['help'])
  131. def help (message):
  132.     msg = """
  133.    ********
  134.    Bot Help
  135.    ********
  136.    To write a command use "/" and the command name, for example "/status"
  137.    Commands:
  138.    ----------------
  139.    Motion commands:
  140.    ----------------
  141.    \t /status \t-> Give the status of motion [ACTIVE|PAUSED]
  142.    \t /start \t-> Start motion
  143.    \t /pause \t-> Pause motion
  144.    \t /foto \t-> Send a foto from webcam
  145.    ----------------
  146.    System commands:
  147.    ----------------
  148.    \t /reboot \t-> Reboot system
  149.    \t /size \t-> Get the size of a given directory (where are saving motion images)
  150.    \t /ping \t-> Ping to 10.8.0.1 , show if it is connected to openvpn
  151.        \t /help \t-> Show this help message
  152.  
  153.    If you write an unknown message the bot will respond with the same like a ping
  154.    
  155.    In adittion, acces to streaming using openvpn:
  156.    sudo openvpn --config rrb.ovpn \t <- Start openvpn
  157.    http://10.8.0.100:8080 \t <- Configuration web interface
  158.    http://10.8.0.100:8081 \t <- Acces to camera 1
  159.    Username:rako
  160.    """
  161.     print "Giving help"    
  162.         bot.reply_to(message, msg)        
  163.  
  164. @bot.message_handler(commands=['reboot'])
  165. def reboot(message):        
  166.     bot.reply_to(message, "Rebooting system")
  167.     command = "/usr/bin/sudo /sbin/shutdown -r now"
  168.     import subprocess
  169.     process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
  170.     output = process.communicate()[0]
  171.     print output
  172.  
  173. @bot.message_handler(commands=['size'])
  174. def size(message):
  175.     response = subprocess.check_output('du -h ' + motionSaveDirectory, shell=True)
  176.     #msg =  response[:response.index("t") ]
  177.     msg = response[0:response.index("\n")]
  178.     print msg
  179.     bot.reply_to(message, msg)
  180.    
  181. @bot.message_handler(commands=['ping'])
  182. def reboot(message):
  183.     hostname = "10.8.0.1" #example
  184.     response = os.system("ping -c 1 " + hostname)
  185.  
  186.     #and then check the response...
  187.     if response == 0:
  188.             bot.reply_to(message, "is up!")
  189.         print hostname, 'is up!'
  190.     else:
  191.             bot.reply_to(message,"is down!")
  192.         print hostname, 'is down!'
  193.  
  194. @bot.message_handler(func=lambda message: True)
  195. def echo_message (message):
  196.     print "Message received"
  197.         bot.reply_to(message, message.text)
  198.  
  199.  
  200. bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement