mainloop works; ping/pong works
This commit is contained in:
52
bot.py
52
bot.py
@@ -3,29 +3,57 @@
|
|||||||
import xmpp
|
import xmpp
|
||||||
from local_config import conf
|
from local_config import conf
|
||||||
|
|
||||||
|
import time
|
||||||
|
t = -time.time()
|
||||||
|
|
||||||
def message_handler(connect_object, message_node):
|
def message_handler(connect_object, message_node):
|
||||||
print 'connect_object:'
|
# hopefully the backlog is processed in this time
|
||||||
print connect_object
|
# FIXME: find a better way.
|
||||||
print 'message_node:'
|
if (t + time.time() < 1):
|
||||||
print message_node
|
return None
|
||||||
|
|
||||||
|
msg_from = message_node.getFrom().getResource()
|
||||||
|
msg_body = message_node.getBody()
|
||||||
|
|
||||||
|
if not type(msg_body) in [str, unicode]:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if msg_body.startswith(conf('nick')):
|
||||||
|
connect_object.send(
|
||||||
|
xmpp.protocol.Message(
|
||||||
|
to=conf('room'),
|
||||||
|
body='hello %s!' % msg_from,
|
||||||
|
typ='groupchat'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# send_msg('hello %s!' % msg_from)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print '%20s: %s' %(msg_from, msg_body)
|
||||||
|
except Exception as e:
|
||||||
|
print e
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
jid = xmpp.protocol.JID(conf('jid'))
|
jid = xmpp.protocol.JID(conf('jid'))
|
||||||
|
|
||||||
client = xmpp.Client(jid.getDomain()) #, debug=[])
|
client = xmpp.Client(jid.getDomain(), debug=[])
|
||||||
client.connect()
|
client.connect()
|
||||||
client.auth(jid.getNode(), conf('password'))
|
client.auth(jid.getNode(), conf('password'))
|
||||||
client.RegisterHandler('message', message_handler)
|
client.RegisterHandler('message', message_handler)
|
||||||
|
|
||||||
client.send(xmpp.Presence(to=(conf('room') + '/' + conf('nick'))))
|
client.send(xmpp.Presence(to=(conf('room') + '/' + conf('nick'))))
|
||||||
|
|
||||||
if 0:
|
def send_msg(msg='''wee, I'm a native bot.'''):
|
||||||
msg = xmpp.protocol.Message(body='''wee, I'm a native bot.''')
|
client.send(
|
||||||
msg.setTo(conf('room'))
|
xmpp.protocol.Message(
|
||||||
msg.setType('groupchat')
|
to=conf('room'),
|
||||||
client.send(msg)
|
body=msg,
|
||||||
|
typ='groupchat'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
import time
|
while (t + time.time()) < 30:
|
||||||
time.sleep(10)
|
client.Process(1)
|
||||||
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user