Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hayes committed Dec 17, 2011
0 parents commit 2490157
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
application: pdtestthrough
version: 1
runtime: python
api_version: 1

inbound_services:
- mail

handlers:
- url: /_ah/mail/.+
script: main.py
login: admin

- url: .*
script: main.py

11 changes: 11 additions & 0 deletions index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
indexes:

# AUTOGENERATED

# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
86 changes: 86 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

import logging
from urllib2 import Request, urlopen, URLError, HTTPError
import urllib
from django.utils import simplejson as json

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


def shorten(url):
gurl = 'http://goo.gl/api/url?url=%s' % urllib.unquote(url)
req = Request(gurl, data='')
req.add_header('User-Agent', 'toolbar')
logging.info('Shortening ' + gurl)
try:
res = urlopen(req)
results = json.load(res)
logging.info( res.code )
except HTTPError, e: #triggers on code 201
logging.info( e.code )
error_content = e.read()
results = json.JSONDecoder().decode(error_content)

return results['short_url']

class CallHandler(webapp.RequestHandler):
def get(self):
response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<Response><Say>Leave a message at the beep.</Say>"
"<Record action=\"http://pdtestthrough.appspot.com/record\" method=\"GET\"/>"
"<Say>I did not receive a recording</Say></Response>")
self.response.out.write(response)
logging.info('Recieved CALL ' + self.request.query_string)
class RecordHandler(webapp.RequestHandler):
def get(self):
response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Say>Thanks. Directing your message to the agent on call.</Say></Response>")
self.response.out.write(response)
logging.info('Recieved RECORDING0 ' + self.request.query_string)
recUrl = self.request.get("RecordingUrl")
phonenumber = self.request.get("From")


logging.info('Recieved RECORDING1 ' + recUrl)
if(recUrl):
logging.info('Found recording!')
else:
recUrl = "http%3A%2F%2Fwww.pagerduty.com%2F"
phonenumber = ""

logging.info('Recieved RECORDING2 ' + recUrl)

shrten = "Error"

try:
shrten = shorten('%s.mp3' % recUrl)
except HTTPError, e:
shrten = "HTTPError"
logging.warn( e.code )
except URLError, e:
shrten = "URLError"
logging.warn(e.reason)

logging.info('Shortened to: ' + shrten)

# Obviously use your own key:
incident = '{"service_key": "6f4d18600a9b012f6a9722000a9040cf","incident_key": "%s","event_type": "trigger","description": "%s %s"}'%(shrten,shrten,phonenumber)
try:
r = Request("http://events.pagerduty.com/generic/2010-04-15/create_event.json", incident) #Note according to the API this should be retried on failure
results = urlopen(r)
logging.info(results)
except HTTPError, e:
logging.warn( e.code )
except URLError, e:
logging.warn(e.reason)


def main():
application = webapp.WSGIApplication([
('/call', CallHandler),
('/record', RecordHandler)],
debug=True)
util.run_wsgi_app(application)

if __name__ == '__main__':
main()

0 comments on commit 2490157

Please sign in to comment.