mainloop works; ping/pong works

This commit is contained in:
urlbot
2014-12-01 11:44:10 +01:00
parent dd951c31d0
commit 397fc4730c

52
bot.py
View File

@@ -3,29 +3,57 @@
import xmpp
from local_config import conf
import time
t = -time.time()
def message_handler(connect_object, message_node):
print 'connect_object:'
print connect_object
print 'message_node:'
print message_node
# hopefully the backlog is processed in this time
# FIXME: find a better way.
if (t + time.time() < 1):
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
jid = xmpp.protocol.JID(conf('jid'))
client = xmpp.Client(jid.getDomain()) #, debug=[])
client = xmpp.Client(jid.getDomain(), debug=[])
client.connect()
client.auth(jid.getNode(), conf('password'))
client.RegisterHandler('message', message_handler)
client.send(xmpp.Presence(to=(conf('room') + '/' + conf('nick'))))
if 0:
msg = xmpp.protocol.Message(body='''wee, I'm a native bot.''')
msg.setTo(conf('room'))
msg.setType('groupchat')
client.send(msg)
def send_msg(msg='''wee, I'm a native bot.'''):
client.send(
xmpp.protocol.Message(
to=conf('room'),
body=msg,
typ='groupchat'
)
)
import time
time.sleep(10)
while (t + time.time()) < 30:
client.Process(1)
client.disconnect()