-
Notifications
You must be signed in to change notification settings - Fork 1
/
room_bot.py
64 lines (52 loc) · 1.82 KB
/
room_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sleekxmpp
import ssl
import logging
class SleekXMPPClient(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, room):
super(SleekXMPPClient, self).__init__(jid, password)
self.room = room
self.nick = 'ECHOBOT'
self.reply_message = 'Hello, %s! Welcome to our room.'
self.ssl_version = ssl.PROTOCOL_SSLv3
self.add_event_handler('session_start', self.onStart)
self.add_event_handler('groupchat_message', self.onGroupMessage)
self.add_event_handler(
'muc::%s::got_online' % self.room,
self.onMUConline,
)
self.register_plugin('xep_0030')
self.register_plugin('xep_0199')
self.register_plugin('xep_0045')
if self.connect():
logging.info('Bot connected...')
self.process(block=False)
else:
print 'Could not connect!'
def onStart(self, event):
self.send_presence()
self.get_roster()
self.plugin['xep_0045'].joinMUC(
self.room,
self.nick,
wait=True
)
def onGroupMessage(self, msg):
body = msg['body']
logging.info('Received message...\nFrom %s\n%s' % (msg['from'], body))
if body.startswith(self.nick):
msg.reply("Please, don't write to me, I'm just a bot.").send()
pass
def onMUConline(self, presence):
if presence['muc']['nick'] != self.nick:
self.send_message(
mto=presence['from'].bare,
mbody=self.reply_message % presence['muc']['nick'],
mtype='groupchat',
)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
client = SleekXMPPClient(
'echobot@cebuad002',
'tobohce',
)