Skip to content

Instantly share code, notes, and snippets.

@hitesh83
Last active May 26, 2017 06:05
Show Gist options
  • Save hitesh83/1e3dfbe1a0042a0454e8d5999e9c991e to your computer and use it in GitHub Desktop.
Save hitesh83/1e3dfbe1a0042a0454e8d5999e9c991e to your computer and use it in GitHub Desktop.
Telegram Bot
import time
import random
import datetime
import subprocess
import picamera
import cat
import telepot
from telepot.loop import MessageLoop
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print 'Got command: %s' % command
if command == '/rol':
bot.sendMessage(chat_id, random.randint(1,6))
elif command == '/9gag':
gag = subprocess.check_output(['sh', '/home/pi/9gag.sh'])
print '' , gag
time.sleep(10)
bot.sendPhoto(chat_id=chat_id, photo=open('/home/pi/9gag/9gag.png', 'rb'))
elif command == '/roll':
bot.sendMessage(chat_id, random.randint(2,12))
elif command == '/time':
bot.sendMessage(chat_id, str(datetime.datetime.now()))
elif command == '/tpune':
tpune = subprocess.check_output(['/home/pi/ansiweather//ansiweather', '-l' , 'Pune','-a','false'])
print '', tpune
bot.sendMessage(chat_id, str(tpune))
elif command == '/cat':
bot.sendMessage(chat_id, 'Cat Photo coming soon.....')
cat.getCat(directory='/home/pi/picam', filename='cat', format='jpg')
time.sleep(10)
bot.sendPhoto(chat_id=chat_id, photo=open('/home/pi/picam/cat.jpg', 'rb'))
elif command == '/tsbc':
tsbc = subprocess.check_output(['/home/pi/ansiweather//ansiweather', '-l' , 'Bangalore','-a','false'])
print '', tsbc
bot.sendMessage(chat_id, str(tsbc))
elif command == '/tmum':
tmum = subprocess.check_output (['/home/pi/ansiweather//ansiweather', '-l' , 'Mumbai' , '-a' , 'false'])
print '', tmum
bot.sendMessage(chat_id, str(tmum))
elif command == '/df':
df = subprocess.check_output(['df', '-h'])
print '', df
bot.sendMessage(chat_id, str(df))
elif command == '/free':
free = subprocess.check_output(['free','-h'])
print '', free
bot.sendMessage(chat_id, str(free))
elif command == '/date':
date = subprocess.check_output(['date'])
print '', date
bot.sendMessage(chat_id, str(date))
elif command == '/cpu':
cpu = subprocess.check_output(['cat', '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq'])
print '', cpu
bot.sendMessage(chat_id, 'CPU Freq='+str(cpu))
elif command == '/uptime':
uptime = subprocess.check_output(['uptime', '-p'])
print '', uptime
bot.sendMessage(chat_id, str(uptime))
elif command== '/hi':
bot.sendMessage(chat_id, 'Hello, How are you?')
elif command == '/hello':
bot.sendMessage(chat_id, 'Hi')
elif command == '/i_am_fine':
bot.sendMessage(chat_id, 'I am also fine, thank you')
elif command == '/bye':
bot.sendMessage(chat_id, 'Good Bye')
elif command == '/reboot':
bot.sendMessage(chat_id, 'Rebooting')
time.sleep(10)
reboot = subprocess.Popen(['reboot'])
elif command == '/tmp':
tmp = subprocess.check_output(['/opt/vc/bin/vcgencmd', 'measure_temp'])
print '', tmp
bot.sendMessage(chat_id, str(tmp))
elif command == '/pic':
bot.sendMessage(chat_id, 'Taking Photo Please wait...')
camera=picamera.PiCamera()
camera.start_preview()
time.sleep(10)
camera.resolution = (1024, 768)
camera.exposure_compensation = 2
camera.capture('./capture.jpg')
camera.close()
bot.sendPhoto(chat_id=chat_id, photo=open('./capture.jpg', 'rb'))
bot = telepot.Bot('Enter Your Telegram Bot API Key')
MessageLoop(bot, handle).run_as_thread()
print 'I am listening ...'
while 1:
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment