diff options
author | Lance Stout <lancestout@gmail.com> | 2011-08-17 21:21:37 -0700 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2011-08-17 21:21:37 -0700 |
commit | 961668d420d2241541f4facc64265932c66ad81c (patch) | |
tree | 3b31e69d1148a94b61541a85bba49adc8c5cea6e /examples/send_client.py | |
parent | 01061a0355b05c8452b1ef8ca998cd2c51ab4c9e (diff) | |
download | slixmpp-961668d420d2241541f4facc64265932c66ad81c.tar.gz slixmpp-961668d420d2241541f4facc64265932c66ad81c.tar.bz2 slixmpp-961668d420d2241541f4facc64265932c66ad81c.tar.xz slixmpp-961668d420d2241541f4facc64265932c66ad81c.zip |
Add guide for sending a message and then disconnecting.
Diffstat (limited to 'examples/send_client.py')
-rwxr-xr-x | examples/send_client.py | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/examples/send_client.py b/examples/send_client.py index fd99e8c9..b3673512 100755 --- a/examples/send_client.py +++ b/examples/send_client.py @@ -29,13 +29,18 @@ if sys.version_info < (3, 0): class SendMsgBot(sleekxmpp.ClientXMPP): """ - A simple SleekXMPP bot that will echo messages it - receives, along with a short thank you message. + A basic SleekXMPP bot that will log in, send a message, + and then log out. """ - def __init__(self, jid, password): + def __init__(self, jid, password, recipient, message): sleekxmpp.ClientXMPP.__init__(self, jid, password) + # The message we wish to send, and the JID that + # will receive it. + self.recipient = recipient + self.msg = message + # The session_start event will be triggered when # the bot establishes its connection with the server # and the XML streams are ready for use. We want to @@ -43,11 +48,6 @@ class SendMsgBot(sleekxmpp.ClientXMPP): # our roster. self.add_event_handler("session_start", self.start) - # The message event is triggered whenever a message - # stanza is received. Be aware that that includes - # MUC messages and error messages. - self.add_event_handler("message", self.message) - def start(self, event): """ Process the session_start event. @@ -63,27 +63,14 @@ class SendMsgBot(sleekxmpp.ClientXMPP): """ self.send_presence() self.get_roster() - msg = self.Message() - msg['to'] = 'user@example.com' - msg['type'] = 'chat' - msg['body'] = "Hello there!" - msg.send() - self.disconnect() - - def message(self, msg): - """ - Process incoming message stanzas. Be aware that this also - includes MUC messages and error messages. It is usually - a good idea to check the messages's type before processing - or sending replies. - Arguments: - msg -- The received message stanza. See the documentation - for stanza objects and the Message stanza to see - how it may be used. - """ - #msg.reply("Thanks for sending\n%(body)s" % msg).send() - print "Msg rceived from %(body)s: %(jid)s" % msg + self.send_message(mto=self.recipient, + mbody=self.msg, + mtype='chat') + + # Using wait=True ensures that the send queue will be + # emptied before ending the session. + self.disconnect(wait=True) if __name__ == '__main__': @@ -106,6 +93,10 @@ if __name__ == '__main__': help="JID to use") optp.add_option("-p", "--password", dest="password", help="password to use") + optp.add_option("-t", "--to", dest="to", + help="JID to send the message to") + optp.add_option("-m", "--message", dest="message", + help="message to send") opts, args = optp.parse_args() @@ -117,14 +108,16 @@ if __name__ == '__main__': opts.jid = raw_input("Username: ") if opts.password is None: opts.password = getpass.getpass("Password: ") + if opts.to is None: + opts.to = raw_input("Send To: ") + if opts.message is None: + opts.message = raw_input("Message: ") # Setup the EchoBot and register plugins. Note that while plugins may # have interdependencies, the order in which you register them does # not matter. - xmpp = SendMsgBot(opts.jid, opts.password) + xmpp = SendMsgBot(opts.jid, opts.password, opts.to, opts.message) xmpp.register_plugin('xep_0030') # Service Discovery - xmpp.register_plugin('xep_0004') # Data Forms - xmpp.register_plugin('xep_0060') # PubSub xmpp.register_plugin('xep_0199') # XMPP Ping # If you are working with an OpenFire server, you may need |