Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

How reply several conversations at the same time? #494

Open
s-nt-s opened this issue Oct 4, 2018 · 0 comments
Open

How reply several conversations at the same time? #494

s-nt-s opened this issue Oct 4, 2018 · 0 comments

Comments

@s-nt-s
Copy link

s-nt-s commented Oct 4, 2018

Hi

I need that my bot can reply to several users without delay, but I have seen that my bot only reply to a user when it has already finished with the previous one.

For example:

#!/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()

And then two user chat with my bot at the same time:

User 1:

(13:26:36) [email protected]/933057321490487705739785908: Hi!
(13:26:37) [email protected]: Reply 0 to [email protected]/933057321490487705739785908
(13:26:38) [email protected]: Reply 1 to [email protected]/933057321490487705739785908
(13:26:39) [email protected]: Reply 2 to [email protected]/933057321490487705739785908
(13:26:40) [email protected]: Reply 3 to [email protected]/933057321490487705739785908
(13:26:41) [email protected]: Reply 4 to [email protected]/933057321490487705739785908

User 2:

(13:26:36) [email protected]/80504085811053912592141090: Hi!
(13:26:42) [email protected]: Reply 0 to [email protected]/80504085811053912592141090
(13:26:43) [email protected]: Reply 1 to [email protected]/80504085811053912592141090
(13:26:44) [email protected]: Reply 2 to [email protected]/80504085811053912592141090
(13:26:45) [email protected]: Reply 3 to [email protected]/80504085811053912592141090
(13:26:46) [email protected]: Reply 4 to [email protected]/80504085811053912592141090

As you can see in the timestamp, user 2 only get a reply when [email protected] have already finished with user 1.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant