Exemplo n.º 1
0
class RocketChatBot(OutputChannel):
    @classmethod
    def name(cls):
        return "rocketchat"

    def __init__(self, user, password, server, ssl=False):
        from rocketchat_py_sdk.driver import Driver

        self.username = user
        self.connector = Driver(url=server, ssl=ssl)
        self.users = {}
        self.user = user
        self.password = password

        self.logged_in = False
        try:
            self.connector.connect()
            self.login()
        except:
            raise Exception(
                '\n\n\n\n\nRocket-chat is not running! \n'
                'Be sure to run docker-compose up rocketchat\n'
                'or change your run-rocketchat for run-console on docker-compose.yml'
            )

    def login(self):
        while not self.logged_in:
            logger.info('Trying to login to rocketchat as {}'.format(
                self.user))
            self.connector.login(user=self.user,
                                 password=self.password,
                                 callback=self._login_callback)
            time.sleep(10)

    """
    Internal callback handlers
    """

    def _login_callback(self, error, data):
        if error:
            logger.error('[-] callback error:')
            logger.error(error)
        else:
            self.logged_in = True
            logger.info("[+] callback success")
            logger.debug(data)

    """
    Messages handlers
    """

    def send_text_message(self, recipient_id, message):
        if recipient_id not in self.users:
            self.users[recipient_id] = RocketchatHandleMessages(
                recipient_id, self)

        for message_part in message.split("\n\n"):
            self.users[recipient_id].add_message(message_part)
Exemplo n.º 2
0
class RocketChatBot(OutputChannel):
    @classmethod
    def name(cls):
        return "rocketchat"

    def __init__(self, user, password, server, ssl=False):
        from rocketchat_py_sdk.driver import Driver

        self.username = user
        self.connector = Driver(url=server, ssl=ssl)
        self.users = {}
        self.user = user
        self.password = password

        self.logged_in = False

        self.connector.connect()
        self.login()

    def login(self):
        while not self.logged_in:
            logger.info('Trying to login to rocketchat as {}'
                        .format(self.user))
            self.connector.login(user=self.user, password=self.password,
                                 callback=self._login_callback)
            time.sleep(10)

    """
    Internal callback handlers
    """
    def _login_callback(self, error, data):
        if error:
            logger.error('[-] callback error:')
            logger.error(error)
        else:
            self.logged_in = True
            logger.info("[+] callback success")
            logger.debug(data)

    """
    Messages handlers
    """
    async def send_text_message(self, recipient_id, text, **kwargs):
        if recipient_id not in self.users:
            self.users[recipient_id] = RocketchatHandleMessages(recipient_id,
                                                                self)

        messages = text.split("\n\n")

        t = threading.Thread(
            target=self.users[recipient_id].send_messages,
            args=(messages, ))
        t.start()
Exemplo n.º 3
0
class RocketChatBot(OutputChannel):
    @classmethod
    def name(cls):
        return "rocketchat"

    def __init__(self, user, password, server, ssl):
        from rocketchat_py_sdk.driver import Driver

        self.username = user
        self.connector = Driver(url=server , ssl=ssl)
        self.users = {}
        self.user = user
        self.password = password

        self.logged_in = False

        self.connector.connect()
        self.login()

    def login(self):
        if not self.logged_in:
            self.connector.login(user=self.user, password=self.password,
                                 callback=self._login_callback)

    """
    Internal callback handlers
    """
    def _login_callback(self, error, data):
        if error:
            logger.error('[-] callback error:')
            logger.error(error)
        else:
            self.logged_in = True
            logger.info("[+] callback success")
            logger.debug(data)
            self.connector.subscribe_to_messages()

    """
    Messages handlers
    """
    def send_text_message(self, recipient_id, message):
        if recipient_id not in self.users:
            self.users[recipient_id] = RocketchatHandleMessages(recipient_id,
                                                                self)
        self.users[recipient_id].add_message(message)