Uses webhooks to post messages into a slack channel.
Project description
collective.ftwslacker
Introduction
The collective.ftwslacker 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=[
'collective.ftwslacker',
])
or to your buildout configuration:
[instance]
eggs += collective.ftwslacker
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 collective.ftwslacker 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 parameters to configure the request:
from collective.ftwslacker import notify_slack
notify_slack(webhook_url='https://hooks.slack.com/services/xxx',
timeout=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 collective.ftwslacker 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.
collective.ftwslacker 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 collective.ftwslacker 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 collective.ftwslacker 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 environment 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 especially useful for handling notification coming 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 collective.ftwslacker 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 user logins 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 collective.ftwslacker 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, collective.ftwslacker will post the user
login to the default channel. If you set FTW_LOGGER_SLACK_WEBHOOK, collective.ftwslacker 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.
Links
- Main project repository: https://github.com/collective/collective.ftwslacker
- Issue tracker: https://github.com/collective/collective.ftwslacker/issues
Copyright
This package is copyright by 4teamwork.
collective.ftwslacker is licensed under GNU General Public License, version 2.
Changelog
2.0.0a1 (2025-12-12)
Breaking changes:
- Rename distribution to
collective.ftwslacker@gforcada #3
2.0.0a0 (2025-12-10)
Breaking changes:
- Replace
pkg_resourcesnamespace with PEP 420 native namespace. Support only Plone 6.2 and Python 3.10+. @gforcada #3928
New features:
- Modernize scaffolding to use
plone.meta@gforcada
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file collective_ftwslacker-2.0.0a1.tar.gz.
File metadata
- Download URL: collective_ftwslacker-2.0.0a1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7669170a6dd1d915c64f3a31988482446f0d26c5118dcec34acf12695139d8dc
|
|
| MD5 |
c0bbc09426aacebfee25adc93179f16d
|
|
| BLAKE2b-256 |
a87a07c69aec4e64b7e6e2ad1b3f3bb165b5afbeee9ac3d4109a380b38eeac8e
|
File details
Details for the file collective_ftwslacker-2.0.0a1-py3-none-any.whl.
File metadata
- Download URL: collective_ftwslacker-2.0.0a1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c8f43acadde0bf9a4be0844a2f906b9a6ebc0aa76aa14363f8917da68f291ac
|
|
| MD5 |
8d77ff6f1ad18840ca24aa4a1ae9fdbd
|
|
| BLAKE2b-256 |
af8a402fd757bdea4edc751b15f3760a098233bd7f123264791762f3dde73f33
|