-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.py
83 lines (78 loc) · 2.6 KB
/
telegram.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 1 sudo apt-get install python-pip
# 2 sudo pip install telepot
import time, datetime
import RPi.GPIO as GPIO
import telepot
from telepot.loop import MessageLoop
first_led = 7
second_led = 29
third_led = 31
fourth_led = 33
now = datetime.datetime.now()
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#LED first_led
GPIO.setup(first_led, GPIO.OUT)
GPIO.output(first_led, 0) #Off initially
#LED second_led
GPIO.setup(second_led, GPIO.OUT)
GPIO.output(second_led, 0) #Off initially
#LED third_led
GPIO.setup(third_led, GPIO.OUT)
GPIO.output(third_led, 0) #Off initially
##LED fourth_led
GPIO.setup(fourth_led, GPIO.OUT)
GPIO.output(fourth_led, 0) #Off initially
def action(msg):
chat_id = msg['chat']['id']
command = msg['text']
print ('Received: %s' % command)
if 'on' in command:
message = "on"
if 'first_led' in command:
message = message + "first_led"
GPIO.output(first_led, 1)
if 'second_led' in command:
message = message + "second_led"
GPIO.output(second_led, 1)
if 'third_led' in command:
message = message + "third_led"
GPIO.output(third_led, 1)
if 'fourth_led' in command:
message = message + "fourth_led"
GPIO.output(fourth_led, 1)
if 'all' in command:
GPIO.output(first_led, 1)
GPIO.output(second_led, 1)
GPIO.output(third_led, 1)
GPIO.output(fourth_led, 1)
message = message + "light(s)"
telegram_bot.sendMessage (chat_id, message)
if 'off' in command:
message = "off "
if 'first_led' in command:
message = message + "first_led"
GPIO.output(first_led, 0)
if 'second_led' in command:
message = message + "second_led"
GPIO.output(second_led, 0)
if 'third_led' in command:
message = message + "third_led"
GPIO.output(third_led, 0)
if 'fourth_led' in command:
message = message + "fourth_led"
GPIO.output(fourth_led, 0)
if 'all' in command:
message = message + "all"
GPIO.output(first_led, 0)
GPIO.output(second_led, 0)
GPIO.output(third_led, 0)
GPIO.output(fourth_led, 0)
message = message + "light(s)"
telegram_bot.sendMessage (chat_id, message)
telegram_bot = telepot.Bot('1993353550:AAHT4l_R0YXCsanE0MtOGLtMPy4mQEMMtY0')
print (telegram_bot.getMe())
MessageLoop(telegram_bot, action).run_as_thread()
print ('Up and Running....')
while 1:
time.sleep(10)