|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Uncyclobot server example using SleekXMPP client library""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import getpass |
| 19 | +import json |
| 20 | +import logging |
| 21 | +import threading |
| 22 | +import urllib |
| 23 | + |
| 24 | +from flask import Flask, request |
| 25 | +import requests |
| 26 | +import sleekxmpp |
| 27 | + |
| 28 | + |
| 29 | +app = Flask(__name__) |
| 30 | + |
| 31 | +@app.route('/send_message', methods=['GET']) |
| 32 | +def send_message(): |
| 33 | + recipient = request.args.get('recipient') |
| 34 | + message = request.args.get('message') |
| 35 | + |
| 36 | + if chat_client and recipient and message: |
| 37 | + chat_client.send_message(mto=recipient, mbody=message) |
| 38 | + return 'message sent to {} with body: {}'.format(recipient, message) |
| 39 | + else: |
| 40 | + logging.info('chat client or recipient or message does not exist!') |
| 41 | + return 'message failed to send', 400 |
| 42 | + |
| 43 | + |
| 44 | +def run_server(host='0.0.0.0'): |
| 45 | + app.run(threaded=False, use_reloader=False, host=host) |
| 46 | + |
| 47 | + |
| 48 | +class UncycloBot(sleekxmpp.ClientXMPP): |
| 49 | + """A simple SleekXMPP bot that will take messages, look up their content on |
| 50 | + wikipedia and provide a link to the page if it exists. |
| 51 | + """ |
| 52 | + |
| 53 | + def __init__(self, jid, password): |
| 54 | + sleekxmpp.ClientXMPP.__init__(self, jid, password) |
| 55 | + |
| 56 | + # The session_start event will be triggered when |
| 57 | + # the bot establishes its connection with the server |
| 58 | + # and the XML streams are ready for use. We want to |
| 59 | + # listen for this event so that we we can initialize |
| 60 | + # our roster. |
| 61 | + self.add_event_handler('session_start', self.start) |
| 62 | + |
| 63 | + # The message event is triggered whenever a message |
| 64 | + # stanza is received. Be aware that that includes |
| 65 | + # MUC messages and error messages. |
| 66 | + self.add_event_handler('message', self.message) |
| 67 | + |
| 68 | + def start(self, event): |
| 69 | + """Process the session_start event. |
| 70 | +
|
| 71 | + Typical actions for the session_start event are requesting the roster |
| 72 | + and broadcasting an initial presence stanza. |
| 73 | +
|
| 74 | + Arguments: |
| 75 | + event -- An empty dictionary. The session_start event does not |
| 76 | + provide any additional data. |
| 77 | + """ |
| 78 | + self.send_presence() |
| 79 | + self.get_roster() |
| 80 | + |
| 81 | + def message(self, msg): |
| 82 | + """Process incoming message stanzas. |
| 83 | +
|
| 84 | + Be aware that this also includes MUC messages and error messages. It is |
| 85 | + usually a good idea to check the messages's type before processing or |
| 86 | + sending replies. If the message is the appropriate type, then the bot |
| 87 | + checks wikipedia to see if the message string exists as a page on the |
| 88 | + site. If so, it sends this link back to the sender in the reply. |
| 89 | +
|
| 90 | + Arguments: |
| 91 | + msg -- The received message stanza. See the SleekXMPP documentation |
| 92 | + for stanza objects and the Message stanza to see how it may be |
| 93 | + used. |
| 94 | + """ |
| 95 | + if msg['type'] in ('chat', 'normal'): |
| 96 | + msg_body = msg['body'] |
| 97 | + logging.info('Message sent was: {}'.format(msg_body)) |
| 98 | + encoded_body = urllib.quote_plus(msg_body) |
| 99 | + svrResponse = requests.get( |
| 100 | + 'https://en.wikipedia.org/w/api.php?' |
| 101 | + 'action=parse&prop=sections&format=json&page={}'.format( |
| 102 | + encoded_body)) |
| 103 | + doc = json.loads(svrResponse.content) |
| 104 | + try: |
| 105 | + page_id = str(doc['parse']['pageid']) |
| 106 | + defn_url = 'https://en.wikipedia.org/?curid={}'.format(page_id) |
| 107 | + msg.reply('find out more about: "{}" here: {}'.format( |
| 108 | + msg_body, defn_url)).send() |
| 109 | + except KeyError as e: |
| 110 | + logging.info('key error: {0}'.format(e)) |
| 111 | + msg.reply('I wasn\'t able to locate info on "{}" Sorry'.format( |
| 112 | + msg_body)).send() |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + # Setup the command line arguments. |
| 117 | + parser = argparse.ArgumentParser( |
| 118 | + description=__doc__, |
| 119 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 120 | + |
| 121 | + # Output verbosity options. |
| 122 | + parser.add_argument( |
| 123 | + '-q', '--quiet', help='set logging to ERROR', action='store_const', |
| 124 | + dest='loglevel', const=logging.ERROR, default=logging.INFO) |
| 125 | + parser.add_argument( |
| 126 | + '-d', '--debug', help='set logging to DEBUG', action='store_const', |
| 127 | + dest='loglevel', const=logging.DEBUG, default=logging.INFO) |
| 128 | + parser.add_argument( |
| 129 | + '-v', '--verbose', help='set logging to COMM', action='store_const', |
| 130 | + dest='loglevel', const=5, default=logging.INFO) |
| 131 | + |
| 132 | + # JID and password options. |
| 133 | + parser.add_argument('-j', '--jid', help='JID to use', required=True) |
| 134 | + parser.add_argument('-p', '--password', help='password to use') |
| 135 | + |
| 136 | + args = parser.parse_args() |
| 137 | + |
| 138 | + # Setup logging. |
| 139 | + logging.basicConfig(level=args.loglevel, |
| 140 | + format='%(levelname)-8s %(message)s') |
| 141 | + |
| 142 | + if args.password is None: |
| 143 | + args.password = getpass.getpass('Password: ') |
| 144 | + |
| 145 | + # Setup the UncycloBot and register plugins. Note that while plugins may |
| 146 | + # have interdependencies, the order in which you register them does |
| 147 | + # not matter. |
| 148 | + xmpp = UncycloBot(args.jid, args.password) |
| 149 | + xmpp.register_plugin('xep_0030') # Service Discovery |
| 150 | + xmpp.register_plugin('xep_0004') # Data Forms |
| 151 | + xmpp.register_plugin('xep_0060') # PubSub |
| 152 | + xmpp.register_plugin('xep_0199') # XMPP Ping |
| 153 | + |
| 154 | + chat_client = xmpp # set the global variable |
| 155 | + |
| 156 | + # start the app server and run it as a thread so that the XMPP server may |
| 157 | + # also start |
| 158 | + threading.Thread(target=run_server).start() |
| 159 | + |
| 160 | + # Connect to the XMPP server and start processing XMPP stanzas. |
| 161 | + if xmpp.connect(): |
| 162 | + xmpp.process(block=True) |
| 163 | + print('Done') |
| 164 | + else: |
| 165 | + print('Unable to connect.') |
0 commit comments