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)
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()
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 __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 connect_bot(): def login_callback(error, data): global logged_in if error: logger.error('Could not login as {}'.format(bot['username'])) logger.error(error) return if not logged_in: logger.info('Login succesful') logged_in = True get_user_rooms() logger.info('Trying to login as {}'.format(bot['username'])) bot['driver'] = Driver(url=bot['rocketchat_url'], ssl=False) bot['driver'].connect() bot['driver'].login(user=bot['username'], password=bot['password'], callback=login_callback)
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)
} ] }] } replyDict = { "hello in chat window": "received your ‘hello in chat window’", "I clicked the airplane": "received your response about clicking the airplane" } def process_message(bot, message): text = message['msg'] if(text in attachmentDict): bot.send_attachment(message['rid'], attachmentDict[text]) elif(text in replyDict): bot.send_message(message['rid'], replyDict[text]) def start(bot): bot.connect() bot.login(user=bot_username, password=bot_password) bot.subscribe_to_messages() bot.add_prefix_handler('', process_message) while True: time.sleep(3600) if __name__ == '__main__': start(Driver(url=rocket_url, ssl=False, debug=True))
def run(self, dispatcher, tracker, domain): tais = My_bot() tais.hello = 'Ola meu nome e Tais' message = start(Driver(url=rocket_url, ssl=use_ssl, debug=True), tais) dispatcher.utter_message(message)