-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarybot.py
executable file
·165 lines (126 loc) · 5.02 KB
/
binarybot.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
import json
from urllib2 import Request, urlopen, URLError
from urllib import quote_plus
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
import time
import picamera
import RPi.GPIO as GPIO
import Adafruit_DHT
import os
# start
offset = 1
register_openers()
GPIO.setmode(GPIO.BCM);
GPIO.setup(4,GPIO.OUT);
GPIO.setup(23,GPIO.OUT);
GPIO.output(4,GPIO.LOW);
GPIO.output(23,GPIO.LOW);
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
ADMIN_GROUP = {add admin group id here}
CHAT_GROUP = {add chat group id here}
memberid=[{add member id's here}]
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 17)
if humidity is not None and temperature is not None:
print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)
else:
print 'Failed to get reading. Try again!'
def sendLocation( chatid ):
try:
sendrequest = Request('https://api.telegram.org/{bot}:{api key}/sendLocation?chat_id=' + `chatid` + '&latitude=-26.711440&longitude=27.859285')
sendresponse = urlopen(sendrequest)
# todo read response
except Exception, e:
print 'send location error'
print e
def sendMessage( chatid, msg ):
try:
sendrequest = Request('https://api.telegram.org/{bot}:{api key}/sendMessage?chat_id=' + `chatid` + '&text=' + quote_plus(msg))
sendresponse = urlopen(sendrequest)
# todo read response
except Exception, e:
print 'send error'
print e
def sendPhoto( chatid, photofilename ):
try:
items = []
items.append(MultipartParam('chat_id', chatid))
items.append(MultipartParam.from_file('photo', photofilename))
datagen, headers = multipart_encode(items)
req = Request('https://api.telegram.org/{bot}:{api key}/sendPhoto', datagen, headers)
print req
rsp = urlopen(req)
print rsp
except Exception, e:
print 'send photo error'
print e
print URLError
#camera = picamera.PiCamera()
while True:
if not GPIO.input(22):
sendMessage(CHAT_GROUP, "Smoke Detected in the Space. Danger Will Robinson, Danger!")
try:
request = Request('https://api.telegram.org/{bot}:{api key}/getUpdates?offset=' + `offset` + '&timeout=100')
response = urlopen(request,timeout=120)
data = response.read()
result = json.loads(data)
messages = result['result']
msgcount = len(messages)
for x in xrange(msgcount):
print messages[x]['message']
offset = messages[x]['update_id'] + 1
userid = messages[x]['message']['from']['id']
fname = messages[x]['message']['from']['first_name']
message = ''
try:
message = messages[x]['message']['text']
except:
print 'no text'
print message
print userid
# parseMessage here
if message.upper() == '/PING':
sendMessage( userid, 'Pong' )
if message.upper() == '/TEMP':
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 17)
if humidity is not None and temperature is not None:
sendMessage(userid, 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
else:
sendMessage(userid,'Failed to get reading. Try again!')
if message.upper() == '/SPACECAM':
# # please wait
# camera.capture('/home/pi/binarybot/spacecam.jpg', resize=(800,600))
# time.sleep(1)
#sendPhoto(userid, '/home/pi/binarybot/spacecam.jpg')
os.system('fswebcam -r 640x480 -S 3 --jpeg 100 --save /home/pi/binarybot/spacecam.jpg')
sendPhoto(CHAT_GROUP, '/home/pi/binarybot/spacecam.jpg')
if message.upper() == '/USERID':
sendMessage(userid, "Your userid is {userid}".format(userid=userid))
if message.upper() == '/HELP':
sendMessage(userid, 'Commands Available /gate,/spacecam,/ping,/userid,/rabbithole - with great power comes great responsibility' )
if message.upper() == '/RABBITHOLE':
sendLocation(userid)
if message.upper() == '/GATE':
if userid in memberid:
GPIO.output(4,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(4,GPIO.LOW)
sendMessage( userid, 'Welcome to BinarySpace' )
sendMessage( ADMIN_GROUP, "Gate Triggered by {name}".format(name=fname) )
else:
sendMessage( userid, "I'm sorry {name}, I'm afraid I can't do that".format(name=fname))
if message.upper() == '/DOOR':
if userid in memberid:
GPIO.output(23,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(23,GPIO.LOW)
sendMessage( userid, 'Welcome to BinarySpace' )
sendMessage( ADMIN_GROUP, "Door Triggered by {name}".format(name=fname) )
else:
sendMessage( userid, "I'm sorry {name}, I'm afraid I can't do that".format(name=fname))
except KeyboardInterrupt:
raise
except:
print 'error'