Skip to content

Commit

Permalink
Working sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwardo committed Apr 7, 2012
1 parent 51a76eb commit 0848084
Show file tree
Hide file tree
Showing 8 changed files with 2,752 additions and 5 deletions.
8 changes: 8 additions & 0 deletions sms-gateway/conf.py
@@ -0,0 +1,8 @@
GATEWAY_DOMAIN = 'chicago47-sms\.appspotmail\.com'
EMAIL_PATTERN = re.compile(r'^.*<\s*(\d+)@%s\s*>\s*$' % re.escape(GATEWAY_DOMAIN))
RECIPIENT = 'Chicago47 <don.schwarz@gmail.com>'
TWILIO_ACCOUNT = 'xxx'
TWILIO_TOKEN = 'xxx'
SEND_SMS_ENABLED = False
SMS_PHONE_NUMBER = '+1 773-309-4710'

40 changes: 35 additions & 5 deletions sms-gateway/main.py
@@ -1,34 +1,64 @@
#!/usr/bin/env python
#

import re
import logging
import webapp2

from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

from twilio.rest import TwilioRestClient

# All user-specified settings go in conf.py.
import conf

class ReceiveSmsHandler(webapp2.RequestHandler):
def get(self):
sender = self.request.get('From')
body = self.request.get('Body')
if sender[0] == '+':
sender = sender[1:]

self.response.out.write('Received SMS from %s: %s' % (sender, body))

# string@appid.appspotmail.com
mail.send_mail(sender="Gateway <%s@chicago47-sms.appspotmail.com>" % sender,
to="Chicago47 <don.schwarz@gmail.com>",
mail.send_mail(sender="%s <%s@%s>" % (sender, sender, GATEWAY_DOMAIN),
to=RECIPIENT,
subject="Received SMS message from %s" % sender,
body=body)

class ReceiveEmailHandler(InboundMailHandler):
def receive(self, message):
logging.info("Received a message from: " + message.sender)
sender = None
for to in [message.to]:
m = EMAIL_PATTERN.match(to)
if m:
sender = m.group(1)

if not sender:
raise Exception('Could not find sender in %s' % message.to())

bodies = message.bodies(content_type='text/plain')
payload = None
for body in bodies:
logging.info("charset: %s" % body[1].charset)
logging.info("encoding: %s" % body[1].encoding)
logging.info("payload: %s" % body[1].payload)
logging.debug("charset: %s" % body[1].charset)
logging.debug("encoding: %s" % body[1].encoding)
logging.debug("payload: %s" % body[1].payload)
payload = body[1].payload

client = TwilioRestClient(TWILIO_ACCOUNT, TWILIO_TOKEN)
if len(payload) > 155:
payload = payload[:155] + '...'

if SEND_SMS_ENABLED:
message = client.sms.messages.create(
to='+' + sender, from_=SMS_PHONE_NUMBER,
body=payload)
else:
logger.info('SEND_SMS_ENABLED is False, not sending SMS.')


class ViewResponseHandler(webapp2.RequestHandler):
def get(self):
Expand Down
Empty file.

0 comments on commit 0848084

Please sign in to comment.