Created
January 20, 2011 10:50
-
-
Save erwan/787723 to your computer and use it in GitHub Desktop.
Jabber GAE Bot Screencast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application: thebabelfishbot | |
version: 1 | |
runtime: python | |
api_version: 1 | |
handlers: | |
- url: /.* | |
script: bot.py | |
inbound_services: | |
- xmpp_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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