Untitled

From Voluminous Tortoise, 8 Years ago, written in Python, viewed 3 times.
URL https://paste.blessuren.de/view/a3f1ba45 Embed
Download Paste or View Raw
  1. from subprocess import check_output
  2. import json
  3. import os
  4. import time
  5. import random
  6. import datetime
  7. import telepot
  8. from telepot.loop import MessageLoop
  9.  
  10. '''
  11. This script is based on the tutorial:
  12. http://www.instructables.com/id/Set-up-Telegram-Bot-on-Raspberry-Pi/
  13. '''
  14.  
  15. access_ids      = [ 310989044, ]          # array of user ids that can communicate with the bot
  16. master_chat_id  = 310989044               # the chat id of the master that gets security notifications
  17.  
  18. def pivxGetAccountBalance():
  19.     try:
  20.         j = json.loads(pivxGetInfo())
  21.         return j["balance"]
  22.     except Exception as e:
  23.         bot.sendMessage(master_chat_id, "Error in pivxGetAccountBalance occured: %s" % str(e))
  24.         return -1.
  25.  
  26. def pivxGetInfo():
  27.     try:
  28.         os.chdir("/home/pivx")
  29.         return str(check_output(["./pivx-cli", "getinfo"]))
  30.     except CalledProcessError:
  31.         raise Exception("Error while executing `/home/pivx/pivx-cli getinfo` occured")
  32.  
  33. def pivxCheckStaking():
  34.     try:
  35.         os.chdir("/home/pivx")
  36.         return str(check_output(["./check_staking.sh", ""]))
  37.     except CalledProcessError:
  38.         raise Exception("Error while executing `/home/pivx/check_staking.sh` occured")
  39.  
  40. def printHelp(chat_id):
  41.     msg = "";
  42.     msg += "/help - display this help.\r\n"
  43.     msg += "/pivxinfo - reply with output of `pivx-cli getinfo`.\r\n"
  44.     msg += "/pivxstaking - reply with output of `check_staking.sh`.\r\n"
  45.     msg += "/roll - reply with a random integer between 1 and 6, like rolling a dice.\r\n"
  46.     msg += "/time - reply with the current time, like a clock.\r\n"
  47.     bot.sendMessage(chat_id, msg)
  48.  
  49. def handle(msg):
  50.     chat_id = msg['chat']['id']
  51.     command = msg['text']
  52.     user_id = msg['from']['id']
  53.    
  54.     print 'Got command: %s' % command
  55.    
  56.     if user_id not in access_ids:
  57.         bot.sendMessage(chat_id, "Access denied")
  58.         bot.sendMessage(master_chat_id, "Access violation detected: %s" % str(msg))
  59.         return
  60.     try:
  61.         if command == '/help':
  62.             printHelp(chat_id)
  63.         elif command == '/pivxinfo':
  64.             bot.sendMessage(chat_id, pivxGetInfo())
  65.         elif command == '/pivxstaking':
  66.             bot.sendMessage(chat_id, pivxCheckStaking())
  67.         elif command == '/roll':
  68.             bot.sendMessage(chat_id, random.randint(1,6))
  69.         elif command == '/time':
  70.             bot.sendMessage(chat_id, str(datetime.datetime.now()))
  71.     except Exception as e:
  72.         bot.sendMessage(chat_id, str(e))
  73.  
  74. bot = telepot.Bot('123:XXX')
  75.  
  76. MessageLoop(bot, handle).run_as_thread()
  77. print 'I am listening ...'
  78.  
  79. old_balance = pivxGetAccountBalance()
  80. while 1:
  81.     time.sleep(120)
  82.     new_balance = pivxGetAccountBalance()
  83.     diff = new_balance - old_balance
  84.     if diff != 0:
  85.         bot.sendMessage(master_chat_id, "Staking reward received: %f piv" % diff)
  86.         old_balance = new_balance
  87.  

Reply to "Untitled"

Here you can reply to the paste above