# Reporting
# kortical.reporting.slack
This library is used to send notification messages to Slack.
# Example usage
from kortical.reporting import slack
BOT_TOKEN = 'xoxb-sekhfvbq9247bqbvq974jb2pvi0ouvb'
slack.init(slack_bot_token=BOT_TOKEN)
slack.post_simple_message_to_slack(channels='<channel-name>',
title='New notification from Darth Vader!',
message='I am your father.')
If using this library in an app, here is a more realistic example of configuring Slack with environment config and a default test channel:
from kortical.environment import get_environment_config, get_environment_name
from kortical.reporting import slack
from kortical.secret import secret
environment_config = get_environment_config(format='yaml')
BOT_TOKEN = secret.get('slack_bot_token')
slack.init(slack_bot_token=BOT_TOKEN,
notify_slack=environment_config.get('notify_slack', False),
slack_test_channel='test_channel')
# Surround a message with tick marks to make it a code snippet
slack.post_simple_message_to_slack(
channels=['<channel-1-name>', '<channel-2-name>'],
title=f':boom: ERROR - There is a problem with your app ({get_environment_name()}) :BOOM:',
message=f'```<stack-trace>```')
# init
This function must be run to set up and configure slack reporting.
Inputs
slack_bot_token
- A unique identifier (starting"xoxb-"
) that represents a bot associated with your Slack app.notify_slack
(= True
) - Set this toFalse
to disable notifications; you might want to set this via config if you have an app that needs to alert in Production but nowhere else.slack_test_channel
(= None
) - If set up, this is a backup channel that will be alerted; even ifnotify_slack
is False.
Returns
None
# post_simple_message_to_slack
This function sends a message to Slack; for more granular control, see post_message_to_slack
.
Inputs
channels
- Slack channels you wish to notify (instr
or[str, ...]
format).title
- The title of the notification.message
- The body of the notification.
Returns
None
# post_message_to_slack
This function sends a message to Slack.
Inputs
channels
- Slack channels you wish to notify (instr
or[str, ...]
format).**kwargs
- any key word arguments; Slack usually requires notifications to be defined as a list calledblocks
.
Returns
None
# kortical.reporting.email
This library is used to send notification messages via email.
# Example usage
from kortical.reporting import email
email.init(
smtp_server="smtp.gmail.com",
smtp_port=465
)
email.send_email(
from_name="Darth Vader",
from_email="darth.vader@empire.com",
from_password="DeathStar66",
to_emails='luke.skywalker@tattoine.net',
subject='I am your father',
plain_text='Fulfill your destiny.'
)
If using this library in an app, here is a more realistic example of sending an email using a template:
from datetime import datetime
import os
import pandas as pd
from kortical.reporting import email
# Generate some results and save...
results_df.to_csv('results.csv')
email.init(
smtp_server="smtp.gmail.com",
smtp_port=465
)
html_template = os.path.join('email_templates', 'model_eval_results.html')
today = datetime.strftime(datetime.now(), '%Y-%m-%d')
email.send_email_from_template(
from_name="Darth Vader",
from_email="darth.vader@empire.com",
from_password="DeathStar66",
to_emails=["adam.bloggs@example.com", "billy.bloggs@example.com"],
subject=f"Model Evaluation Results - {today}",
html_template=html_template,
template_parameters={'date': today},
cc_emails=["charlie.bloggs@example.com"],
bcc_emails=["derek.bloggs@example.com"],
attachments='results.csv')
# init
This function must be run to set up and configure email notifications.
Inputs
smtp_server
- The server that handles the sending of emails from clients to other email servers. Common servers aresmtp.gmail.com
,smtp.office365.com
,smtp.live.com
,smtp.mail.yahoo.com
(Gmail, Outlook/Hotmail, Yahoo).smtp_port
- The network port used by the SMTP server. Common ports are25
,587
,465
.
Returns
None
# send_email
This function sends an email.
Inputs
from_name
- Name of the email senderfrom_email
- The email address from where the message should be sent.from_password
- The password for the email address above.to_emails
- Email addresses you wish to notify (instr
or[str, ...]
format).subject
- The title of the email.plain_text
(= None
) - The body of the email, in text format. At least one ofplain_text
andhtml_text
must be specified.html_text
(= None
) - The body of the email, in HTML format. At least one ofplain_text
andhtml_text
must be specified.attachments
(= None
) - Files you wish to attach (instr
or[str, ...]
format).cc_emails
(= None
) - Email addresses to copy in.bcc_emails
(= None
) - Email addresses to blind copy in.reply_to_name
(= None
) - Name corresponding to the email below.reply_to_email
(= None
) - If a recipient of the email wishes to reply, it will be sent to this email address.
Returns
None
# send_email_from_template
This function sends an email, using template files as a source for the email body.
Inputs
from_name
- Name of the email senderfrom_email
- The email address from where the message should be sent.from_password
- The password for the email address above.to_emails
- Email addresses you wish to notify (instr
or[str, ...]
format).subject
- The title of the email.plain_text_template
(= None
) - Path to a.txt
file. At least one ofplain_text_template
andhtml_text_template
must be specified.html_text_template
(= None
) - Path to a.html
file. At least one ofplain_text_template
andhtml_text_template
must be specified.template_parameters
(= None
) - A dictionary of parameters that should be inserted into the template file.attachments
(= None
) - Files you wish to attach (instr
or[str, ...]
format).cc_emails
(= None
) - Email addresses to copy in.bcc_emails
(= None
) - Email addresses to blind copy in.reply_to_name
(= None
) - Name corresponding to the email below.reply_to_email
(= None
) - If a recipient of the email wishes to reply, it will be sent to this email address.
Returns
None
← Platform API Datasets →