async def run_bot(self): self.log.debug("Initializing Bot") async with semaphore.Bot(self.phone_number, socket_path=self.socket, profile_name=self.profile_name, profile_picture=self.profile_picture, raise_errors=True) as bot: # We do not really use the underlying bot framework, but just use our own Pure-Text Handler bot.register_handler(re.compile(""), self.message_handler) bot.set_exception_handler(self.exception_callback) self.log.debug("Starting Semaphore") await bot.start()
def build_client(config, db, tg_client, stickers_client): bot = semaphore.Bot( config['signal']['username'], socket_path=config['signal'].get('signald_socket_path', '/var/run/signald/signald.sock'), ) bot.tg_client = tg_client bot.db = db bot.stickers_client = stickers_client bot.source_code_url = config['source_code_url'] for pattern, callback in handlers: bot.register_handler(pattern, callback) return bot
async def send_message_to_users(self, message: str, users: List[str]) -> None: """ Send a message to specific or all users Args: message: Message to send users: List of user ids or None for all signal users """ if not users: users = map(lambda x: x.platform_id, self.bot.get_all_users()) message = UserHintService.format_commands(message, self.bot.command_formatter) async with semaphore.Bot(self.phone_number, socket_path=self.socket, profile_name=self.profile_name, profile_picture=self.profile_picture, raise_errors=True) as bot: for user in users: disable_unicode = not self.bot.get_user_setting(user, BotUserSettings.FORMATTING) await bot.send_message(user, adapt_text(str(message), just_strip=disable_unicode))
async def send_unconfirmed_reports(self) -> None: """ Send unconfirmed daily reports to the specific users """ if not self.bot.user_messages_available(): return async with semaphore.Bot(self.phone_number, socket_path=self.socket, profile_name=self.profile_name, profile_picture=self.profile_picture, raise_errors=True) as bot: backoff_time = random.uniform(2, 6) message_counter = 0 for report_type, userid, message in self.bot.get_available_user_messages(): self.log.info(f"Try to send report {message_counter}") disable_unicode = not self.bot.get_user_setting(userid, BotUserSettings.FORMATTING) for elem in message: success = False rate_limited = False try: success = await bot.send_message(userid, adapt_text(elem.message, just_strip=disable_unicode), attachments=elem.images) except InternalError as e: if "org.whispersystems.signalservice.api.push.exceptions.RateLimitException" in e.exceptions: rate_limited = True break elif "org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException" in e.exceptions \ or "org.whispersystems.signalservice.api.push.exceptions.NotFoundException" in e.exceptions: self.log.warning( f"Account does not exist anymore, delete it: {userid}") self.bot.delete_user(userid) break elif "org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException" in e.exceptions: self.log.warning(f"ProofRequired for {userid}") break else: raise e except RateLimitError as e: self.log.error(f"Invalid Send Request: {e.message}") rate_limited = True break except NoSuchAccountError as e: self.log.warning( f"Account does not exist anymore, delete it: {e.account}") self.bot.delete_user(userid) break except UnknownGroupError: self.log.warning( f"Group does not exist anymore, delete it: {userid}") self.bot.delete_user(userid) break except (NoSendPermissionError, InvalidRecipientError) as e: self.log.warning( f"We cant send to {userid}, disabling user: {e.message}") self.bot.disable_user(userid) break except UnknownError as e: self.log.error(f"Unknown Signald Error {e.error_type}: {e.error}") raise e except SignaldError as e: self.log.error(f"Unknown Signald Error {e}") raise e if success: self.log.warning(f"({message_counter}) Sent daily report to {userid}") self.bot.confirm_message_send(report_type, userid) else: self.log.error( f"({message_counter}) Error sending daily report to {userid}") backoff_time = self.backoff_timer(backoff_time, rate_limited) message_counter += 1