Skip to content

Instantly share code, notes, and snippets.

@erwan
Created January 20, 2011 10:50
Show Gist options
  • Save erwan/787723 to your computer and use it in GitHub Desktop.
Save erwan/787723 to your computer and use it in GitHub Desktop.
Jabber GAE Bot Screencast
application: thebabelfishbot
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: bot.py
inbound_services:
- xmpp_message
from google.appengine.api import xmpp
from translate import translate
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class XMPPHandler(webapp.RequestHandler):
def get(self):
print "XMPP GET"
def post(self):
message = xmpp.Message(self.request.POST)
message.reply(" => " + translation(message.body, "fr", "en"))
application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
# Google Translate
import urllib
from google.appengine.api import urlfetch
from django.utils import simplejson
def translate(text, source, target):
url = "https://www.googleapis.com/language/translate/v2?key=AIzaSyDblc-_ylyRNlJPvoUxG8bLR6Yajj-vSLM&q=" + urllib.quote(text) + "&source="+source+"&target="+target
result = urlfetch.fetch(url)
return "" + simplejson.loads(result.content)["data"]["translations"][0]["translatedText"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment