from subprocess import check_output import os import time import random import datetime import telepot from telepot.loop import MessageLoop ''' After **inserting token** in the source code, run it: """ $ python2.7 diceyclock.py """ [Here is a tutorial](http://www.instructables.com/id/Set-up-Telegram-Bot-on-Raspberry-Pi/) teaching you how to setup a bot on Raspberry Pi. This simple bot does nothing but accepts two commands: - `/roll` - reply with a random integer between 1 and 6, like rolling a dice. - `/time` - reply with the current time, like a clock. ''' def pivxGetInfo(): out = "" try: os.chdir("/home/pivx") out = check_output(["./pivx-cli", "getinfo"]) except CalledProcessError: out = "Error while executing `/home/pivx/pivx-cli getinfo` occured" return out def pivxCheckStaking(): out = "" try: os.chdir("/home/pivx") out = check_output(["./check_staking.sh", ""]) except CalledProcessError: out = "Error while executing `/home/pivx/check_staking.sh` occured" return out def printHelp(chat_id): msg = ""; msg += "/help - display this help.\r\n" msg += "/failedaccess - display messages that were received from an unauthorized user.\r\n" msg += "/pivxinfo - reply with output of `pivx-cli getinfo`.\r\n" msg += "/pivxstaking - reply with output of `check_staking.sh`.\r\n" msg += "/roll - reply with a random integer between 1 and 6, like rolling a dice.\r\n" msg += "/time - reply with the current time, like a clock.\r\n" bot.sendMessage(chat_id, msg) def printFailedAccess(chat_id): global failed_access if len(failed_access) == 0: bot.sendMessage(chat_id, "No unauthorized access registered.") for item in failed_access: bot.sendMessage(chat_id, str(item)) failed_access = [] def handle(msg): chat_id = msg['chat']['id'] command = msg['text'] user_id = msg['from']['id'] print 'Got command: %s' % command if user_id != 310989044: bot.sendMessage(chat_id, "Access denied") failed_access.append(msg) return if command == '/help': printHelp(chat_id) elif command == '/failedaccess': printFailedAccess(chat_id) elif command == '/pivxinfo': bot.sendMessage(chat_id, pivxGetInfo()) elif command == '/pivxstaking': bot.sendMessage(chat_id, pivxCheckStaking()) elif command == '/roll': bot.sendMessage(chat_id, random.randint(1,6)) elif command == '/time': bot.sendMessage(chat_id, str(datetime.datetime.now())) failed_access = [] bot = telepot.Bot('111111:XXXXXXXXXXXXXXXXXX') MessageLoop(bot, handle).run_as_thread() print 'I am listening ...' while 1: time.sleep(10)