Slack

Overview

The Slack module leverages the Slack API and provides way to easily send notifications through Slack. It is recommended that you reference the Slack API documentation for additional details and information.

API Tokens

  • Slack API Tokens are required to use this module. To obtain an API Token, create a Slack App associated with the desired Slack workspace. Once you create the app, navigate to ‘OAuth & Permissions’ and add the following OAuth scopes:

    channels:read, users:read, chat:write, and files:write

    You can now install the Slack App, which will produce an API Token. Note that you can change the scopes any time, but you must reinstall the app each time (your API Token will stay the same).

  • Slack has rate limits on all its endpoints.

Quickstart

To call the Slack class you can either store the API Token as an environment variable SLACK_API_TOKEN or pass it in as an argument.

Initiate class via environment variable api token
from parsons import Slack
slack = Slack()
Pass api token directly
from parsons import Slack
slack = Slack(api_key='my-api-tkn')

You can then send messages:

Send a simple messsage
slack.message_channel("my_channel", "Hello from python script")
Share a file
slack.upload_file(["channel_1", "channel_2"], "my_slack_file.txt")

API

class parsons.notifications.slack.Slack(api_key=None)[source]
channels(fields=None, exclude_archived=False, types=None)[source]

Return a list of all channels in a Slack team.

Parameters:
  • fields – list A list of the fields to return. By default, only the channel id and name are returned. See https://api.slack.com/methods/conversations.list for a full list of available fields. Notes: nested fields are unpacked.

  • exclude_archived – bool Set to True to exclude archived channels from the list. Default is false.

  • types – list Mix and match channel types by providing a list of any combination of public_channel, private_channel, mpim (aka group messages), or im (aka 1-1 messages).

Returns:

Table

See Table for output options.

users(fields=None)[source]

Return a list of all users in a Slack team.

Parameters:

fields – list A list of the fields to return. By default, only the user id and name and deleted status are returned. See https://api.slack.com/methods/users.list for a full list of available fields. Notes: nested fields are unpacked.

Returns:

Table

See Table for output options.

classmethod message(channel, text, webhook=None, parent_message_id=None)[source]

Send a message to a Slack channel with a webhook instead of an api_key. You might not have the full-access API key but still want to notify a channel

Parameters:
  • channel – str The name or id of a public_channel, a private_channel, or an im (aka 1-1 message).

  • text – str Text of the message to send.

  • webhook – str If you have a webhook url instead of an api_key Looks like: https://hooks.slack.com/services/Txxxxxxx/Bxxxxxx/Dxxxxxxx

  • parent_message_id – str The ts value of the parent message. If used, this will thread the message.

message_channel(channel, text, parent_message_id=None, **kwargs)[source]

Send a message to a Slack channel.

Parameters:
  • channel – str The name or id of a public_channel, a private_channel, or an im (aka 1-1 message).

  • text – str Text of the message to send.

  • parent_message_id – str The ts value of the parent message. If used, this will thread the message.

Keyword Arguments:
Returns:

dict

A response json

upload_file(channels, filename, filetype=None, initial_comment=None, title=None, is_binary=False)[source]

Upload a file to Slack channel(s).

Parameters:
  • channels – list or str The list of channel names or IDs where the file will be shared. Can be a single channel name/ID (str) or a list of channel names/IDs.

  • filename – str The path to the file to be uploaded.

  • filetype – str A file type identifier. If None, type will be inferred base on file extension. This is used to determine what fields are available for that object. See https://api.slack.com/types/file for a list of valid types and for more information about the file object.

  • initial_comment – str The text of the message to send along with the file.

  • title – str Title of the file to be uploaded.

  • is_binary – bool If True, open this file in binary mode. This is needed if uploading binary files. Defaults to False.

Returns:

A response json

Return type:

dict