Notificator

Notificator

class notif.notificator.Notificator(on_error_sleep_time: int)[source]

Abstract class to define a notificator. Force implementation of method send_notification and specify how to send a notification error.

Parameters

on_error_sleep_time (int) – When an error occurs for the sending of the notification, it will wait this time to retry one more time. Time is in seconds.

abstract send_notification(message: str, subject: Optional[str] = None)None[source]

Abstract method to send a notification.

Parameters
  • message (str) – The message to send as a notification message through the notificator.

  • subject (str) – The subject of the notification. If None, the default message is use. By default None.

send_notification_error(error: Exception)None[source]

Send a notification error message through the notificator, used with the wrapper.

Parameters

error (Exception) – The exception raise during the script execution.

Slack Notificator

class notif.notificator.SlackNotificator(webhook_url: str, on_error_sleep_time: int = 120)[source]

Notificator to send a notification into a Slack channel.

Parameters
  • webhook_url (str) – a webhook URL given by Slack to post content into a channel. See here for more detail.

  • on_error_sleep_time (int) – When an error occurs for the sending of a notification, it will wait this time (in seconds) to retry one more time. Default is 120 sec.

Example:

notif = SlackNotificator(webhook_url="webhook_url")
notif.send_notification("The script is finish")
send_notification(message: str, subject: Optional[str] = None)None[source]

Send a notification message to the webhook URL.

Parameters
  • message (str) – The message to send as a notification message to the webhook URL.

  • subject (str) – The subject of the notification. If None, the default message ‘Python script Slack notification’ is used. Note that the subject is formatted, the text is bolded and a new line is appended after the subject creates a ‘title’ effect. Default is None.

Email Notificator

class notif.notificator.EmailNotificator(sender_email: str, sender_login_credential: str, destination_email: str, smtp_server: smtplib.SMTP, on_error_sleep_time: int = 120)[source]

Notificator to send a notification email.

Parameters
  • sender_email (str) – The email of the sender.

  • sender_login_credential (str) – The login credential of the sender email.

  • destination_email (str) – The recipient of the email can be the same as the sender_email.

  • smtp_server (smtplib.SMTP) – The SMTP server to relay the email.

  • on_error_sleep_time (int) – When an error occurs for the sending of a notification, it will wait this time (in seconds) to retry one more time. Default is 120 sec.

Examples

Using gmail server:

sender_email = "my_email"
sender_login_credential = "my_password"
destination_email = sender_email
smtp_server = smtplib.SMTP('smtp.gmail.com', 587)

notif = EmailNotificator(sender_email, sender_login_credential,
                               destination_email, smtp_server)
notif.send_notification(message="text")

Using hotmail server:

sender_email = "my_email"
sender_login_credential = "my_password"
destination_email = "other_email"
smtp_server = smtplib.SMTP('smtp.live.com', 587)

notif = EmailNotificator(sender_email, sender_login_credential,
                               destination_email, smtp_server)
notif.send_notification(message="text")

Channel Notificator

class notif.notificator.ChannelNotificator(channel_url: str, on_error_sleep_time: int = 120)[source]

Wrapper around notify_run to send a notification to a phone or a desktop. Can have multiple devices in the channel.

Parameters
  • channel_url (str) – A channel_rul created on notify.run <https://notify.run/>.

  • on_error_sleep_time (int) – When an error occurs for the sending of a notification, it will wait this time (in seconds) to retry one more time. Default is 120 sec.

Example

notif = ChannelNotificator(channel_url="https://notify.run/some_channel_id")
notif.send_notification('Hi there!')

Microsoft Teams Notificator

class notif.notificator.TeamsNotificator(webhook_url: str, on_error_sleep_time: int = 120)[source]

Notificator to send a notification into a Microsoft Teams channel.

Parameters
  • webhook_url (str) – A webhook URL given by Microsoft Teams to post content into a channel. See this for more detail.

  • on_error_sleep_time (int) – When an error occurs for the sending of a notification, it will wait this time (in seconds) to retry one more time. Default is 120 sec.

Example:

notif = TeamsNotificator(webhook_url="webhook_url")
notif.send_notification("The script is finish")