Skip to main content

Uses webhooks to post messages into a slack channel.

Project description

ftw.slacker

Introduction

The ftw.slacker is a Plone addon that provides an easy to use api to post messages into a Slack channel through Slack’s webhooks api.

For more information about slack webhooks see Slack’s documentation about Incoming Webhooks

Installation

Add the package as dependency to your setup.py:

setup(
      # ...
      install_requires=[
        'ftw.slacker',
      ])

or to your buildout configuration:

[instance]
eggs += ftw.slacker

and run buildout

bin/buildout

Usage

Setup Slack webhook

First of all, you need to setup a Slack webhook.

Read the Slack documentation about Incoming Webhooks and start setting up your own webhock by follow the incoming webhook integration.

Post message to Slack

Just import the notify_slack api function and call it.

from ftw.slacker import notify_slack

notify_slack('https://hooks.slack.com/services/xxx',
             text="my first post")

Done!

Configure the requests module

Following properties are passed to the requests module:

That means, you can call the api-function with this parameteres to configure the request:

from ftw.slacker import notify_slack

notify_slack(webhook_url='https://hooks.slack.com/services/xxx',
             timemout=10,
             verify=False,
             text="my first post")

Slack payload

Just add additional keyword arguments to the api-function. All parameters will be passed as payload to the Slack webhook.

from ftw.slacker import notify_slack

notify_slack('https://hooks.slack.com/services/xxx',
             text="my first post",
             attachments=[
                 {
                     "title": "Slack API Documentation",
                     "title_link": "https://api.slack.com/",
                     "text": "Optional text that appears within the attachment"
                 }
             ])

Webhook URL by environment variables

Normally you don’t want to store your webhook-url in your application code.

ftw.slacker supports configuration through environment-variables:

Set your environment variable:

export STANDARD_SLACK_WEBHOOK='https://hooks.slack.com/services/xxx'

or through buildout:

[instance]
environment-vars +=
    STANDARD_SLACK_WEBHOOK https://hooks.slack.com/services/xxx

and call the api-function without webhook_url parameter:

from ftw.slacker import notify_slack

notify_slack(text="my first post")

Override the environment variable

If you set the STANDARD_SLACK_WEBHOOK environment variable, you can still use a different slack webhook.

export STANDARD_SLACK_WEBHOOK='https://hooks.slack.com/services/default-channel-id'
from ftw.slacker import notify_slack

# Post message to service default-channel-id
notify_slack(text="my first post")

# Post message to service specific-channel-id
notify_slack('https://hooks.slack.com/services/specific-channel-id',
             text="my first post")

Deactivate Slack notification

Let’s imagine, you have a server with multiple deployments and all deployments should push to the same Slack webhook.

You can either configure the standard slack webhook envoronment variable through buildout for each deployment, or you just define the default webhook url once in your server environment:

export STANDARD_SLACK_WEBHOOK='https://hooks.slack.com/services/xxx'

Each application will post messages to this slack webhook.

Blacklist

Now you install a test-deployment on the same server where you want to deactivate the notifications.

For this purpose, you can set another environment variable in this specific deployment’s builodut.cfg to the value: deactivate. (see the static variable NOTIFICATION_DEACTIVATION_VALUE):

[instance]
environment-vars +=
    DEACTIVATE_SLACK_NOTIFICATION deactivate

All notifications performed by this deployment will be skipped.

Whitelist

You could even do a whitelist for your deployments.

export STANDARD_SLACK_WEBHOOK='https://hooks.slack.com/services/xxx'
export DEACTIVATE_SLACK_NOTIFICATION deactivate

And for all whitelisted deployments, use the following buildout configuration:

[instance]
environment-vars +=
    DEACTIVATE_SLACK_NOTIFICATION

This will reset the DEACTIVATE_SLACK_NOTIFICATION variable to ''

Deactivate through webhook_url

It’s also possible to deactivate just a specific notification.

Let’s say, you have set your STANDARD_SLACK_WEBHOOK. We’ve learned, that you can call notify_slack without any webhook_url to push notification to the standard webhook url or you can call it with a more specific webhook url notify_slack(webhook_url="xxx") to override the standard slack webhook.

Using the NOTIFICATION_DEACTIVATION_VALUE as the webhook_url will just deactivate the current notification and will not bubble up to the standard slack webhook.

This feature is expecially useful for handling notification comming from multiple external modules using the slacker-integration.

See the next chapter for more information about advanced usage.

Advance usage

Perhaps you’ve got different external modules using the ftw.slacker implementation and all of this modules providing a different default slack webhook url.

Let’s imagine, we have a module calling ftw.logger which logs all userlogins within your plonesite to a slack-channel.

It provides an additional environment variable called FTW_LOGGER_SLACK_WEBHOOK to post the logging-activities to a separate channel. So the implementation of this module may look like this:

from ftw.slacker import notify_slack
import os

def notify_user_login(user):
    notify_slack(os.environ.get('FTW_LOGGER_SLACK_WEBHOOK'),
                 text='User {} logged in'.format(user.username))

If you don’t set the FTW_LOGGER_SLACK_WEBHOOK variable, ftw.slacker will post the user login to the default channel. If you set FTW_LOGGER_SLACK_WEBHOOK, ftw.slacker will use this more specific channel for notifications.

Deactivating the whole notification system through the DEACTIVATE_SLACK_NOTIFICATION environment variable is not desired, because you still want to post other notifications, i.e. from your application which uses the standard slack webhook url.

For this puropose, you can just deactivate this specific notification branch by setting the environment variable FTW_LOGGER_SLACK_WEBHOOK to deactivate (see the static variable NOTIFICATION_DEACTIVATION_VALUE).

[instance]
environment-vars +=
    STANDARD_SLACK_WEBHOOK https://hooks.slack.com/services/xxx
    FTW_LOGGER_SLACK_WEBHOOK deactivate

Threading

All requests to the Slack-API will be handled within its own threads. All messages are sent in a separate thread so that it is non-blocking and does not crash the application on an error.

The function notify_slack returns the thread-object for further thread handlings (i.e. in testing) or none.

Changelog

1.1.0 (2019-11-27)

  • Remove support for Plone 5.0. [mbaechtold]

  • Test against Plone 5.1. [mbaechtold]

  • Test against Plone 5.2 and Python 3.7. [ericof]

1.0.2 (2017-07-31)

  • Use unittest instead unittest2 in tests. [elioschmutz]

1.0.1 (2017-07-27)

  • Fix README.rst errors. [elioschmutz]

1.0.0 (2017-07-27)

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ftw.slacker-1.1.0.tar.gz (9.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page