Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chrismatthieu/317763 to your computer and use it in GitHub Desktop.
Save chrismatthieu/317763 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, RequestHandler, WSGIApplication
from google.appengine.api.urlfetch import fetch as urlfetch, GET, POST
from wsgiref.handlers import CGIHandler
HEADER = """
<html><head><title>Teleku Example</title>
</head><body>
<h1>Teleku High/Low Game</h1>
"""
FOOTER = "</body></html>"
class MainHandler(webapp.RequestHandler):
def get(self):
write = self.response.out.write; write(HEADER)
write(FOOTER)
def post(self):
deal = random.randint(1, 10)
doc = '<?xml version="1.0" encoding="UTF-8"?><phoneml><speak>welcome to the game of high low. the dealer randomly selected %s will his next number be higher or lower? press 1 or say higher or press 2 or say lower</speak><input options="1,2,higher,lower">/guess/%s</input></phoneml>' % (deal, deal)
self.response.headers["Content-Type"] = 'application/xml'
self.response.headers['Content-Length'] = str(len(doc))
self.response.out.write(doc)
class Guess(webapp.RequestHandler):
def post(self, deal):
newdeal = random.randint(1, 10)
guess = self.request.get('callerinput')
if guess in ('1', 'higher'):
if newdeal > int(deal):
gamestatus = "winner"
else:
gamestatus = "loser"
if guess in ('2', 'lower'):
if newdeal < int(deal):
gamestatus = "winner"
else:
gamestatus = "loser"
doc = '<?xml version="1.0" encoding="UTF-8"?><phoneml><speak>the dealer randomly selected %s you are a %s will his next number be higher or lower? press 1 or say higher or press 2 or say lower</speak><input options="1,2,higher,lower">/guess/%s</input></phoneml>' % (newdeal, gamestatus, newdeal)
self.response.headers["Content-Type"] = 'application/xml'
self.response.headers['Content-Length'] = str(len(doc))
self.response.out.write(doc)
def main():
application = webapp.WSGIApplication([
('/guess/(.*)', Guess),
('/', MainHandler)
], debug=True)
CGIHandler().run(application)
# util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment