forked from fluffy-critter/BagelVoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmsg.py
executable file
·69 lines (60 loc) · 2.01 KB
/
sendmsg.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
#!/usr/bin/python
import session
import control
import model
import timeutil
from model import Event
from twilio.rest import TwilioRestClient
import twilio
import config
import sys
import urllib
user = session.user()
form = session.form()
for check in ['Body', 'From', 'To']:
if not form.getfirst(check):
print "Status: 400 Bad Requeset\nContent-type: text/html\n\nMissing parameter: %s" % check
sys.exit()
inbox = control.getInbox(form, user, 'From')
client = TwilioRestClient(user.twilio_sid, user.twilio_auth_token)
with model.transaction():
body = form.getfirst('Body')
try:
message = client.messages.create(
body=body,
from_=form.getfirst('From'),
to=form.getfirst('To'),
status_callback=config.configuration['root-url'] + '/sms.py/outgoing'
)
except twilio.TwilioRestException as e:
print "Status: 400 Bad Request\nContent-type: text/html\n\n%s" % str(e)
sys.exit()
now = timeutil.getTime()
try:
# It's possible that the callback fired before we got here...
event = Event.get(Event.sid == message.sid)
event.message_body = body
event.last_update = now
event.save()
conversation = event.conversation
conversation.last_update = now
conversation.save()
except Event.DoesNotExist:
# ... but not likely
peer = control.getPeer(form, user, whoValue=message.to)
conversation = control.getConversation(form, inbox, peer)
event = Event.create(
sid=message.sid,
inbound=False,
inbox=inbox,
conversation=conversation,
message_body=body,
time=now,
last_update=now,
status=message.status,
type="text")
conversation.last_update = now
conversation.save()
print '''Content-type: text/html
Location: %s%s
Redirecting to the app''' % (config.configuration['root-url'], urllib.unquote(form.getfirst('redir') or '.'))