Skip to content

Instantly share code, notes, and snippets.

@s-nt-s
Created October 2, 2018 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-nt-s/9cd013f4ede2799a412c0a82b28903ed to your computer and use it in GitHub Desktop.
Save s-nt-s/9cd013f4ede2799a412c0a82b28903ed to your computer and use it in GitHub Desktop.
Prueba de bot
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
import getpass
from optparse import OptionParser
import time
import sleekxmpp
class EchoBot(sleekxmpp.ClientXMPP):
"""
copy from http://sleekxmpp.com/getting_started/echobot.html
"""
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler("message", self.message)
def start(self, event):
self.send_presence()
self.get_roster()
def message(self, msg):
if msg['type'] in ('chat', 'normal'):
user = msg['from']
rpl = msg.reply()
for i in range(0, 5):
text = "Reply %s to %s" % (i, user)
rpl['body']=text
rpl.send()
time.sleep(1)
if __name__ == '__main__':
# Setup the command line arguments.
optp = OptionParser()
# Output verbosity options.
optp.add_option('-q', '--quiet', help='set logging to ERROR',
action='store_const', dest='loglevel',
const=logging.ERROR, default=logging.INFO)
optp.add_option('-d', '--debug', help='set logging to DEBUG',
action='store_const', dest='loglevel',
const=logging.DEBUG, default=logging.INFO)
optp.add_option('-v', '--verbose', help='set logging to COMM',
action='store_const', dest='loglevel',
const=5, default=logging.INFO)
# JID and password options.
optp.add_option("-j", "--jid", dest="jid",
help="JID to use")
optp.add_option("-p", "--password", dest="password",
help="password to use")
opts, args = optp.parse_args()
# Setup logging.
logging.basicConfig(level=opts.loglevel,
format='%(levelname)-8s %(message)s')
if opts.jid is None:
opts.jid = raw_input("Username: ")
if opts.password is None:
opts.password = getpass.getpass("Password: ")
xmpp = EchoBot(opts.jid, opts.password)
xmpp.register_plugin('xep_0030')
xmpp.register_plugin('xep_0004')
xmpp.register_plugin('xep_0060')
xmpp.register_plugin('xep_0199')
xmpp.connect()
xmpp.process()
@s-nt-s
Copy link
Author

s-nt-s commented Oct 2, 2018

El problema es que si escribo al mismo tiempo desde dos usuarios a este bot la respuesta es:

(13:26:36) cyttorak@xmpp.jp/933057321490487705739785908: hola
(13:26:37) pi@xmpp.jp: Reply 0 to cyttorak@xmpp.jp/933057321490487705739785908
(13:26:38) pi@xmpp.jp: Reply 1 to cyttorak@xmpp.jp/933057321490487705739785908
(13:26:39) pi@xmpp.jp: Reply 2 to cyttorak@xmpp.jp/933057321490487705739785908
(13:26:40) pi@xmpp.jp: Reply 3 to cyttorak@xmpp.jp/933057321490487705739785908
(13:26:41) pi@xmpp.jp: Reply 4 to cyttorak@xmpp.jp/933057321490487705739785908
(13:26:36) cyttorak@suchat.org/80504085811053912592141090: hola
(13:26:42) pi@xmpp.jp: Reply 0 to cyttorak@suchat.org/80504085811053912592141090
(13:26:43) pi@xmpp.jp: Reply 1 to cyttorak@suchat.org/80504085811053912592141090
(13:26:44) pi@xmpp.jp: Reply 2 to cyttorak@suchat.org/80504085811053912592141090
(13:26:45) pi@xmpp.jp: Reply 3 to cyttorak@suchat.org/80504085811053912592141090
(13:26:46) pi@xmpp.jp: Reply 4 to cyttorak@suchat.org/80504085811053912592141090

Es decir, hasta que no se ha respondido completamente al primer usuario no se contesta al segundo, cuando lo deseado es que los conteste en paralelo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment