Skip to main content

Slack notifications

Installation

pip install slack-notifications

Simple usage

import os

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


slack.send_notify('channel-name', username='Bot', text='@channel This is test message')

or

import os

from slack_notifications import Slack


slack = Slack('<token>')
slack.send_notify('channel-name', username='Bot', text='@channel This is test message')

Message

import os

from slack_notifications import Slack, Attachment


slack = Slack('<token>')
message = slack.send_notify('channel-name', username='Bot', text='@channel This is test message')

message.text = 'This is test message'
message.update()

message.add_reaction('<name>')
message.remove_reaction('<name>')

message.upload_file('./test.yml', filetype='yaml')

message.attachments.append(
    Attachment(
        title='Attachment title',
        pretext='Attachment pretext',
        text='Attachment text',
        footer='Attachment footer',
        color='green',
    ),
)
message.update()

Use attachments

import os

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


attachment = slack.Attachment(
    title='Attachment title',
    pretext='Attachment pretext',
    text='Attachment text',
    footer='Attachment footer',
    color='green',
)

slack.send_notify('channel-name', username='Bot', text='@channel This is test message', attachments=[attachment])

See program API

Attachment fields

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


attachment = slack.Attachment(
    title='Attachment title',
    pretext='Attachment pretext',
    text='Attachment text',
    footer='Attachment footer',
    fields=[
        slack.Attachment.Field(
            title='Field title',
            value='Field value',
        ),
    ],
    color='green',
)

slack.send_notify('channel-name', username='Bot', text='@channel This is test message', attachments=[attachment])

Simple Text Block

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


block = slack.SimpleTextBlock(
    'Text example',
    fields=[
        slack.SimpleTextBlock.Field(
            'Text field',
        ),
        slack.SimpleTextBlock.Field(
            'Text field',
            emoji=True,
        ),
    ],
)

slack.send_notify('channel-name', username='Bot', text='@channel This is test message', blocks=[block])

Action Block

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


block = slack.ActionsBlock(
    elements=[
        slack.ButtonBlock(
            'Yes', 
            action_id='action1',
            value='some_data1',
            style='primary'
        ),
        slack.ButtonBlock(
            'No', 
            action_id='action2',
            value='some_data2',
            style='danger'
        ),
    ],
)

slack.send_notify('channel-name', username='Bot', text='@channel This is test message', blocks=[block])

Use mrkdwn module

import slack_notifications as slack


block = slack.SimpleTextBlock(
    'Text example',
    fields=[
        slack.SimpleTextBlock.Field(
            slack.mrkdwn.bold('Text field'),
        ),
        slack.SimpleTextBlock.Field(
            slack.mrkdwn.italic('Text field'),
            emoji=True,
        ),
    ],
)

Mattermost interface

Simple usage

import os

import slack_notifications.mattermost as mattermost


mattermost.ACCESS_TOKEN = 'xxx'
mattermost.BASE_URL_ENV_NAME = 'http://your-mattermost-url.com/api/v4'
mattermost.TEAM_ID_ENV_NAME = 'xxx'

mattermost.send_notify('channel-name', username='Bot', text='@channel This is test message')

or

import os

from slack_notifications.mattermost import Mattermost


mattermost = Mattermost('http://your-mattermost-url.com/api/v4',
                   token='<token>',
                   team_id='xxx')
mattermost.send_notify('channel-name', username='Bot', text='@channel This is test message')

Use fields for Mattermost

import slack_notifications.mattermost as mattermost
import slack_notifications as slack


mattermost.ACCESS_TOKEN = 'xxx'
mattermost.BASE_URL = 'http://your-mattermost-url.com/api/v4'
mattermost.TEAM_ID = 'xxx'


block = slack.SimpleTextBlock(
    'Text example',
    fields=[
        slack.SimpleTextBlock.Field(
            'Text field',
        ),
        slack.SimpleTextBlock.Field(
            'Text field',
            emoji=True,
        ),
    ],
)

mattermost.send_notify('channel-name', username='Bot', text='@channel This is test message', blocks=[block])

Use mrkdwn module for Mattermost

import slack_notifications as slack
from slack_notifications.mattermost import mrkdwn


block = slack.SimpleTextBlock(
    'Text example',
    fields=[
        slack.SimpleTextBlock.Field(
            mrkdwn.bold('Text field'),
        ),
        slack.SimpleTextBlock.Field(
            mrkdwn.italic('Text field'),
            emoji=True,
        ),
    ],
)

See program API

Init color

import slack_notifications as slack


slack.init_color('green', '#008000')

Call slack resource

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


response = slack.call_resource(slack.Resource('users.info', 'GET'), params={'user': 'W1234567890'})

Resource iterator

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


for user in slack.resource_iterator(slack.Resource('users.list', 'GET'), 'members'):
    pass

Raise exception if error was given

import slack_notifications as slack


slack.ACCESS_TOKEN = 'xxx'


slack.send_notify('channel-name', username='Bot', text='@channel This is test message', raise_exc=True)

Program API

send_notify

  • channel
  • text: str = None
  • username: str = None
  • icon_url: str = None
  • icon_emoji: str = None
  • link_names: bool = True
  • raise_exc: bool = False
  • attachments: List[Attachment] = None
  • blocks: List[BaseBlock] = None

upload_file

  • channel
  • file
  • title: str = None,
  • content: str = None,
  • filename: str = None,
  • thread_ts: str = None,
  • filetype: str = 'text',
  • raise_exc: bool = False

call_resource

  • resource: Resource
  • raise_exc: bool = False
  • **kwargs (requests lib options)

resource_iterator

  • resource: Resource
  • from_key: str
  • cursor: str = None
  • raise_exc: bool = False
  • limit: int = DEFAULT_RECORDS_LIMIT

init_color

  • name: str
  • code: str

Attachment

  • image_url: str = None,
  • thumb_url: str = None,
  • author_name: str = None,
  • author_link: str = None,
  • author_icon: str = None,
  • title: str = None,
  • title_link: str = None,
  • text: str = None,
  • pretext: str = None,
  • footer: str = None,
  • footer_icon: str = None,
  • timestamp: str = None,
  • fields: List[Attachment.Field] = None,
  • color: str = None

Attachment.Field

  • title: str = None
  • value: str = None
  • short: bool = False

SimpleTextBlock

  • text: str
  • mrkdwn: bool = True
  • block_id: str = None
  • fields: List[SimpleTextBlock.Field] = None

SimpleTextBlock.Field

  • text: str
  • emoji: bool = False
  • mrkdwn: bool = True

DividerBlock

  • block_id: str = None

ImageBlock

  • image_url: str
  • title: str = None
  • alt_text: str = None
  • mrkdwn: bool = True
  • block_id: str = None

ContextBlock

  • elements: List[Union[ContextBlock.TextElement, ContextBlock.ImageElement]]
  • block_id: str = None

ContextBlock.TextElement

  • text: str
  • mrkdwn: bool = True

ContextBlock.ImageElement

  • image_url: str
  • alt_text: str = None

ActionsBlock

  • elements: List[ButtonBlock]

ButtonBlock

  • text: str
  • action_id: str
  • value: str
  • style: str = None

Release files for slack-notifications 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slack-notifications 1.0.1
File Size Uploaded
slack_notifications-1.0.1.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slack-notifications 1.0.1
File Interpreter ABI Platform
slack_notifications-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.7 kB

Release files / slack_notifications-1.0.1.tar.gz

Download URL slack_notifications-1.0.1.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
83840ff6ba6de22f54285c87213b779d7e1e81b220b1f36fd342d40100745bee
BLAKE2b-256 checksum
How to use checksums
bd98d30d81755545505e90be96929ec83fdf85cb7ced527d3aad5e0f15b99be5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / slack_notifications-1.0.1-py3-none-any.whl

Download URL slack_notifications-1.0.1-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9110b890c995a4902c8b2cbdb56a65036aa12ca4e9e567ae7ad4e7fc93dcfbd0
BLAKE2b-256 checksum
How to use checksums
2ac9b43a9a903d84db279b6b8de11152d973fbf14ef5b66e98ee32bbaf8527a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page