source: doc/examples/muc_client.tac @ 156:d79ffefc3ac1

wokkel-muc-client-support-24
Last change on this file since 156:d79ffefc3ac1 was 156:d79ffefc3ac1, checked in by Ralph Meijer <ralphm@…>, 9 years ago

Add a simple MUC client example that responds to greetings, fix a typo.

File size: 2.2 KB
Line 
1"""
2An XMPP MUC client.
3
4This XMPP Client logs in as C{user@example.org}, joins the room
5C{'room@muc.example.org'} using the nick C{'greeter'} and responds to
6greetings addressed to it. If another occupant writes C{'greeter: hello'}, it
7will return the favor.
8
9This example uses L{MUCClient} instead of the protocol-only
10L{MUCClientProtocol<wokkel.muc.MUCClientProtocol>} so that it can hook into
11its C{receivedGroupChat}. L{MUCClient} implements C{groupChatReceived} and
12makes a distinction between messages setting the subject, messages that a
13part of the room's conversation history, and 'live' messages. In this case,
14we only want to inspect and respond to the 'live' messages.
15"""
16
17from twisted.application import service
18from twisted.words.protocols.jabber.jid import JID
19from wokkel.client import XMPPClient
20from wokkel.muc import MUCClient
21
22# Configuration parameters
23
24THIS_JID = JID('user@example.org')
25ROOM_JID = JID('room@muc.example.org')
26NICK = u'greeter'
27SECRET = 'secret'
28LOG_TRAFFIC = True
29
30class MUCGreeter(MUCClient):
31    """
32    I join a room and respond to greetings.
33    """
34
35    def __init__(self, roomJID, nick):
36        MUCClient.__init__(self)
37        self.roomJID = roomJID
38        self.nick = nick
39
40
41    def connectionInitialized(self):
42        """
43        Once authorized, join the room.
44        """
45        MUCClient.connectionInitialized(self)
46        self.join(self.roomJID, self.nick)
47
48    def receivedGroupChat(self, room, user, message):
49        """
50        Called when a groupchat message was received.
51
52        Check if the message was addressed to my nick and if it said
53        C{'hello'}. Respond by sending a message to the room addressed to
54        the sender.
55        """
56        if message.body.startswith(self.nick + u":"):
57            nick, text = message.body.split(':', 1)
58            text = text.strip().lower()
59            if text == u'hello':
60                body = u"%s: Hi!" % (user.nick)
61                self.groupChat(self.roomJID, body)
62
63
64# Set up the Twisted application
65
66application = service.Application("MUC Client")
67
68client = XMPPClient(THIS_JID, SECRET)
69client.logTraffic = LOG_TRAFFIC
70client.setServiceParent(application)
71
72mucHandler = MUCGreeter(ROOM_JID, NICK)
73mucHandler.setHandlerParent(client)
Note: See TracBrowser for help on using the repository browser.