Untitled

From Chunky Plover, 8 Years ago, written in Python, viewed 4 times.
URL https://paste.blessuren.de/view/04756537 Embed
Download Paste or View Raw
  1. from subprocess import check_output
  2. import os
  3. import time
  4. import random
  5. import datetime
  6. import telepot
  7. from telepot.loop import MessageLoop
  8.  
  9. '''
  10. After **inserting token** in the source code, run it:
  11.  
  12. """
  13. $ python2.7 diceyclock.py
  14. """
  15.  
  16. [Here is a tutorial](http://www.instructables.com/id/Set-up-Telegram-Bot-on-Raspberry-Pi/)
  17. teaching you how to setup a bot on Raspberry Pi. This simple bot does nothing
  18. but accepts two commands:
  19.  
  20. - `/roll` - reply with a random integer between 1 and 6, like rolling a dice.
  21. - `/time` - reply with the current time, like a clock.
  22. '''
  23.  
  24. def pivxGetInfo():
  25.     out = ""
  26.     try:
  27.         os.chdir("/home/pivx")
  28.         out = check_output(["./pivx-cli", "getinfo"])
  29.     except CalledProcessError:
  30.         out = "Error while executing `/home/pivx/pivx-cli getinfo` occured"
  31.     return out
  32.  
  33. def pivxCheckStaking():
  34.     out = ""
  35.     try:
  36.         os.chdir("/home/pivx")
  37.         out = check_output(["./check_staking.sh", ""])
  38.     except CalledProcessError:
  39.         out = "Error while executing `/home/pivx/check_staking.sh` occured"
  40.     return out
  41.  
  42. def printHelp(chat_id):
  43.     msg = "";
  44.     msg += "/help - display this help.\r\n"
  45.     msg += "/failedaccess - display messages that were received from an unauthorized user.\r\n"
  46.     msg += "/pivxinfo - reply with output of `pivx-cli getinfo`.\r\n"
  47.     msg += "/pivxstaking - reply with output of `check_staking.sh`.\r\n"
  48.     msg += "/roll - reply with a random integer between 1 and 6, like rolling a dice.\r\n"
  49.     msg += "/time - reply with the current time, like a clock.\r\n"
  50.     bot.sendMessage(chat_id, msg)
  51.  
  52. def printFailedAccess(chat_id):
  53.     global failed_access
  54.     if len(failed_access) == 0:
  55.         bot.sendMessage(chat_id, "No unauthorized access registered.")
  56.     for item in failed_access:
  57.         bot.sendMessage(chat_id, str(item))
  58.     failed_access = []
  59.  
  60. def handle(msg):
  61.     chat_id = msg['chat']['id']
  62.     command = msg['text']
  63.     user_id = msg['from']['id']
  64.    
  65.     print 'Got command: %s' % command
  66.    
  67.     if user_id != 310989044:
  68.         bot.sendMessage(chat_id, "Access denied")
  69.         failed_access.append(msg)
  70.         return
  71.  
  72.     if command == '/help':
  73.         printHelp(chat_id)
  74.     elif command == '/failedaccess':
  75.         printFailedAccess(chat_id)
  76.     elif command == '/pivxinfo':
  77.         bot.sendMessage(chat_id, pivxGetInfo())
  78.     elif command == '/pivxstaking':
  79.         bot.sendMessage(chat_id, pivxCheckStaking())
  80.     elif command == '/roll':
  81.         bot.sendMessage(chat_id, random.randint(1,6))
  82.     elif command == '/time':
  83.         bot.sendMessage(chat_id, str(datetime.datetime.now()))
  84.  
  85. failed_access = []
  86. bot = telepot.Bot('111111:XXXXXXXXXXXXXXXXXX')
  87.  
  88. MessageLoop(bot, handle).run_as_thread()
  89. print 'I am listening ...'
  90.  
  91. while 1:
  92.     time.sleep(10)

Reply to "Untitled"

Here you can reply to the paste above