-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp_bot.py
92 lines (63 loc) · 2.52 KB
/
xmpp_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#author:luozijun
#email:gnulinux@126.com
import logging
from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
"""
推荐使用 SleekXMPP
Lib: pyxmpp/xmpppy/pyjabber/SleekXMPP
Main Page:
https://github.com/fritzy/SleekXMPP
Install :
$:sudo pip install sleekxmpp
请不要使用 apt 系统里面的源,里面的代码并不是最新的。
Document:
XMPP协议翻译:http://wiki.jabbercn.org/RFC3920#.E5.AE.A2.E6.88.B7.E7.AB.AF
博文:http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378956.html
Good Idea:
不一样的聊天方式:https://smuxi.im/
"""
class EchoBot(ClientXMPP):
def __init__(self, jid, password):
ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.message)
# In BaseXMPP.py
self.send_message('asluozijun@jabber.org','你好nihao.')
# If you wanted more functionality, here's how to register plugins:
# self.register_plugin('xep_0030') # Service Discovery
# self.register_plugin('xep_0199') # XMPP Ping
# Here's how to access plugins once you've registered them:
# self['xep_0030'].add_feature('echo_demo')
# If you are working with an OpenFire server, you will
# need to use a different SSL version:
# import ssl
# self.ssl_version = ssl.PROTOCOL_SSLv3
def session_start(self, event):
self.send_presence()
self.get_roster()
# Most get_*/set_* methods from plugins use Iq stanzas, which
# can generate IqError and IqTimeout exceptions
#
# try:
# self.get_roster()
# except IqError as err:
# logging.error('There was an error getting the roster')
# logging.error(err.iq['error']['condition'])
# self.disconnect()
# except IqTimeout:
# logging.error('Server is taking too long to respond')
# self.disconnect()
def message(self, msg):
if msg['type'] in ('chat', 'normal'):
msg.reply("Thanks for sending\n%(body)s" % msg).send()
if __name__ == '__main__':
# Ideally use optparse or argparse to get JID,
# password, and log level.
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)-8s %(message)s')
xmpp = EchoBot('linuxcn@jabber.org', '13117025667l')
xmpp.connect()
xmpp.process(block=True)