|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import logging |
| 18 | + |
| 19 | +# [START xmpp-imports] |
| 20 | +from google.appengine.api import xmpp |
| 21 | +# [END xmpp-imports] |
| 22 | +import mock |
| 23 | +import webapp2 |
| 24 | + |
| 25 | +# Mock roster of users |
| 26 | +roster = mock.Mock() |
| 27 | + |
| 28 | + |
| 29 | +class SubscribeHandler(webapp2.RequestHandler): |
| 30 | + def post(self): |
| 31 | + # [START track] |
| 32 | + # Split the bare XMPP address (e.g., user@gmail.com) |
| 33 | + # from the resource (e.g., gmail), and then add the |
| 34 | + # address to the roster. |
| 35 | + sender = self.request.get('from').split('/')[0] |
| 36 | + roster.add_contact(sender) |
| 37 | + # [END track] |
| 38 | + |
| 39 | + |
| 40 | +class PresenceHandler(webapp2.RequestHandler): |
| 41 | + def post(self): |
| 42 | + # [START presence] |
| 43 | + # Split the bare XMPP address (e.g., user@gmail.com) |
| 44 | + # from the resource (e.g., gmail), and then add the |
| 45 | + # address to the roster. |
| 46 | + sender = self.request.get('from').split('/')[0] |
| 47 | + roster.add_contact(sender) |
| 48 | + # [END presence] |
| 49 | + |
| 50 | + |
| 51 | +class SendPresenceHandler(webapp2.RequestHandler): |
| 52 | + def post(self): |
| 53 | + # [START send-presence] |
| 54 | + jid = self.request.get('jid') |
| 55 | + xmpp.send_presence(jid, status="My app's status") |
| 56 | + # [END send-presence] |
| 57 | + |
| 58 | + |
| 59 | +class ErrorHandler(webapp2.RequestHandler): |
| 60 | + def post(self): |
| 61 | + # [START error] |
| 62 | + # In the handler for _ah/xmpp/error |
| 63 | + # Log an error |
| 64 | + error_sender = self.request.get('from') |
| 65 | + error_stanza = self.request.get('stanza') |
| 66 | + logging.error('XMPP error received from {} ({})' |
| 67 | + .format(error_sender, error_stanza)) |
| 68 | + # [END error] |
| 69 | + |
| 70 | + |
| 71 | +# [START chat] |
| 72 | +class XMPPHandler(webapp2.RequestHandler): |
| 73 | + def post(self): |
| 74 | + print "REQUEST POST IS %s " % self.request.POST |
| 75 | + message = xmpp.Message(self.request.POST) |
| 76 | + if message.body[0:5].lower() == 'hello': |
| 77 | + message.reply("Greetings!") |
| 78 | +# [END chat] |
| 79 | + |
| 80 | +app = webapp2.WSGIApplication([ |
| 81 | + ('/_ah/xmpp/message/chat/', XMPPHandler), |
| 82 | + ('/_ah/xmpp/subscribe', SubscribeHandler), |
| 83 | + ('/_ah/xmpp/presence/available', PresenceHandler), |
| 84 | + ('/_ah/xmpp/error/', ErrorHandler), |
| 85 | + ('/send_presence', SendPresenceHandler), |
| 86 | +]) |
0 commit comments