forked from salmanfarisvp/TelegramBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegrambot.py
46 lines (37 loc) · 915 Bytes
/
telegrambot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#coder :- Salman Faris
import sys
import time
import telepot
import RPi.GPIO as GPIO
#LED
def on(pin):
GPIO.output(pin,GPIO.HIGH)
return
def off(pin):
GPIO.output(pin,GPIO.LOW)
return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Got command: %s' % command)
if command == 'on':
bot.sendMessage(chat_id, on(11))
elif command =='off':
bot.sendMessage(chat_id, off(11))
bot = telepot.Bot('Bot Token')
bot.message_loop(handle)
print('I am listening...')
while 1:
try:
time.sleep(10)
except KeyboardInterrupt:
print('\n Program interrupted')
GPIO.cleanup()
exit()
except:
print('Other error or exception occured!')
GPIO.cleanup()