-
Notifications
You must be signed in to change notification settings - Fork 8
/
sms_twilio.py
169 lines (132 loc) · 5.33 KB
/
sms_twilio.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
165
166
167
168
169
try:
import json
except ImportError:
import django.utils.simplejson as json
from datastore import Post, SmsMessage, Phone, RegisteredEmail, ImokUser
from twitter import twitter_post
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template, util
from google.appengine.ext.webapp.util import login_required
from google.appengine.api import mail
import re
import twilio
import personFinder
import logging
import settings as s
URL_BASE = '/%s/Accounts/%s' % (s.TWILIO_API_VERSION, s.TWILIO_ACCOUNT_ID)
SID_REGEX = re.compile(r'<Sid>([^<]*)</Sid>')
STATUS_REGEX = re.compile(r'<Status>([^<]*)</Status>')
def sendSms(sms_message):
data = { 'From': s.SMS_GATEWAY,
'To': sms_message.phone_number,
'Body': sms_message.message,
'StatusCallback': 'https://%s/smsgateway/twilio/callback' % s.HOST }
account = twilio.Account(s.TWILIO_ACCOUNT_ID, s.TWILIO_AUTH_TOKEN)
response = account.request(URL_BASE + '/SMS/Messages', method='POST', vars=data)
sms_message.twilio_sid = SID_REGEX.search(response).groups()[0]
sms_message.status = 'sent'
sms_message.put()
class IncomingHandler(webapp.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
util = twilio.Utils(s.TWILIO_ACCOUNT_ID, s.TWILIO_AUTH_TOKEN)
if not util.validateRequest(self.request.url, self.request.POST, self.request.headers['X-Twilio-Signature']):
logging.error("Invalid request. Probably not Twilio.")
sms_sid = self.request.get('SmsSid')
phoneNumber = Phone.normalize_number(self.request.get('From'))
text = self.request.get('Body')
if not (text and phoneNumber):
logging.error('WTF. No phoneNumber or message text.')
return
smsMessage = SmsMessage(phone_number=phoneNumber,
message=text,
direction='incoming',
twilio_sid=sms_sid,
status='unclaimed')
phone_entity = Phone.all().filter('number =', phoneNumber).get()
if not phone_entity:
db.put([smsMessage])
#self.response.out.write(json.dumps({'result': 'ok'}))
return
user = phone_entity.user
self.savePostAndPush(text, phoneNumber, user, smsMessage)
def savePostAndPush(self, text, phoneNumber, user, smsMessage=None, bogusTimestamp=None):
post = Post.fromText(text)
if bogusTimestamp != None:
post.datetime = bogusTimestamp
post.unique_id = Post.gen_unique_key()
post.user = user
objects = [post]
if smsMessage != None:
smsMessage.status = 'queued'
objects.append(smsMessage)
db.put(objects)
######################################################################
# send email
# FIX: this block of code does not belong in sms_twilio
imok_user = ImokUser.getProfileForUser(user)
email_query = RegisteredEmail.all().filter('userName =', user).order('emailAddress')
debugOutput = []
for email in email_query:
template_data = {
'message': post.message,
'link': post.permalink(self.request.host_url),
'unsubscribe_link': email.permalink(self.request.host_url),
'user': imok_user
}
body = template.render(s.template_path('email.txt'), template_data)
debugOutput.append(body)
mail.send_mail(sender=s.MAILER_EMAIL,
to=email.emailAddress,
subject="IMOk status",
body=body)
twitter_post(imok_user, post.message)
######################################################################
# post to person finder
if 0:
debugText = personFinder.postToPersonFinder(post)
debugOutput.append(debugText)
######################################################################
# send confirmation SMS
if smsMessage != None:
smsMessage.status = 'processed'
smsMessage.put()
response_sms = SmsMessage(phone_number=phoneNumber,
message="I'm OK: Message received, %d contact(s) notified." % email_query.count(),
direction="outgoing",
status="queued")
#response_sms.put()
sendSms(response_sms)
#self.response.out.write(message)
#self.response.out.write(json.dumps({'result': 'ok'}))
return debugOutput
class CallbackHandler(webapp.RequestHandler):
def post(self):
sms_sid = self.request.get('SmsSid', '')
sms_status = self.request.get('SmsStatus', '')
if sms_sid == '' or sms_status == '':
logging.error('No SmsSid or SmsStatus!')
return
sms_message = SmsMessage.all().filter('twilio_sid =', sms_sid).get()
if not sms_message:
logging.error('No SmsMessage for the given SmsSid')
return
if sms_status == 'sent':
sms_message.status = 'delivered'
elif sms_status == 'failed':
logging.error('SMS failed to send: %s' % sms_sid)
sms_message.status = 'failed'
else:
logging.warn('Unknown status: %s' % sms_status)
return
sms_message.put()
def main():
application = webapp.WSGIApplication([
('/smsgateway/twilio/incoming', IncomingHandler),
('/smsgateway/twilio/callback', CallbackHandler),
], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()