async def notify(store, text): # pragma: no cover notification_providers = [ await models.Notification.get(notification_id) for notification_id in store.notifications ] for provider in notification_providers: notifiers.notify(provider.provider, message=text, **provider.data)
def notify_email( self, message: str = None, to: str = None, username: str = None, password: str = None, **kwargs: dict, ) -> bool: """Notify using email service :param message: notification message :param to: target of email message :param username: email service username :param password: email service password :return: True is notification was success, False if not """ response = notify( "email", message=message, to=to, username=username, password=password, **kwargs, ) return self._handle_response(response)
def notify_twilio( self, message: str = None, number_from: str = None, number_to: str = None, account_sid: str = None, token: str = None, **kwargs: dict, ) -> bool: """Notify using Twilio service :param message: notification message :param number_from: number where the message comes from :param number_to: number where the messages goes to :param account_sid: Twilio account SID :param token: Twilio account token :return: True is notification was success, False if not """ response = notify( "twilio", message=message, from_=number_from, to=number_to, account_sid=account_sid, auth_token=token, **kwargs, ) return self._handle_response(response)
def _send_push_notifications( title, subtitle, message, provider_config_factory: Optional[Callable[[str, str, str], dict]] = None, ): """Send push notifications.""" try: import notifiers except (ImportError, ModuleNotFoundError): raise KlaxonExit( os.linesep.join( [ "notifiers enabled but not installed", "$ pip(x) install klaxon[notifiers]", ] ) ) if "notifiers" not in config: raise KlaxonExit("notifiers key not found in configuration") message = message.strip('"').strip("'") provider_config = ( get_notifiers_provider_config(message, subtitle, title) if provider_config_factory is None else provider_config_factory(message, subtitle, title) ) for provider in config["notifiers"]: name = provider["name"] kwargs = { **provider_config.get(name, {}), **{k: v for k, v in provider.items() if k != "name"}, } if ( "message" in notifiers.get_notifier(name).required["required"] and "message" not in kwargs ): kwargs["message"] = message notifiers.notify(name, **kwargs)
def test_direct_notify_positive(self, mock_provider): rsp = notify(mock_provider.name, required="foo", message="foo") assert not rsp.errors assert rsp.status == SUCCESS_STATUS assert rsp.data == { "required": "foo", "message": "foo", "option_with_default": "foo", }
def test_direct_notify_positive(self, mock_provider): rsp = notify(mock_provider.name, required='foo', message='foo') assert not rsp.errors assert rsp.status == SUCCESS_STATUS assert rsp.data == { 'required': 'foo', 'message': 'foo', 'option_with_default': 'foo' }
def notify_pushover( self, message: str = None, user: str = None, token: str = None, **kwargs: dict ) -> bool: """Notify using Pushover service :param message: notification message :param user: target user for the notification :param token: service token :return: True is notification was success, False if not """ response = notify("pushover", message=message, user=user, token=token, **kwargs) return self._handle_response(response)
def notify(self, message: str) -> Mapping[str, Response]: returns = {} for service, defaults in self.__config.items(): r = notify(service, message=message, **defaults) returns[service] = r if self._warn and not r.ok and not defaults.get( "raise_on_errors", False): if callable(self._warn): self._warn(r) else: logger.error( f"Error ({r.status}) notifying via {service}: {r.errors}" ) return returns
def notify_telegram( self, message: str = None, chat_id: str = None, token: str = None, **kwargs: dict, ) -> bool: """Notify using Telegram service :param message: notification message :param chat_id: target chat id for the notification :param token: service token :return: True is notification was success, False if not """ response = notify( "telegram", message=message, chat_id=chat_id, token=token, **kwargs ) return self._handle_response(response)
def notify_slack( self, message: str = None, channel: str = None, webhook_url: str = None, **kwargs: dict, ) -> bool: """Notify using Slack service :param message: notification message :param channel: target channel for the notification :param webhook_url: Slack webhook url :return: True is notification was success, False if not """ response = notify( "slack", message=message, webhook_url=webhook_url, channel=channel, **kwargs, ) return self._handle_response(response)
def test_direct_notify_negative(self): with pytest.raises(NoSuchNotifierError, match="No such notifier with name"): notify("foo", message="whateverz")
def test_direct_notify_negative(self): with pytest.raises(NoSuchNotifierError, match='No such notifier with name'): notify('foo', message='whateverz')
async def notify(store, text): # pragma: no cover notification_providers = await utils.database.get_objects( models.Notification, store.notifications) for provider in notification_providers: notifiers.notify(provider.provider, message=text, **provider.data)
#!/usr/bin/env python from notifiers import notify notify('pushover', user='******', token='bar', message='test')