Skip to main content

Python API and CLI for sending message in Slack

Project description

Python API and CLI for sending message in Slack

PyPI - Version slack-to Versions Python Versions

toslack is a Python package that helps you send messages, images or files to slack channels with simple settings. Command line tools are also provided to do the same thing.

Requirements

  • Python Version: Ensure you are using Python 3.6 or higher.
  • Dependencies:
    • slack_sdk (version 3.31.0)
    • dataclasses (built-in for Python 3.6+)
    • typing (for type hinting)

Installation

pip install toslack

Sending a message to Slack

One of the most common use-cases is sending a message to Slack. If your app's bot user is not in a channel yet, invite the bot user before running the code snippet (or add chat:write.public to Bot Token Scopes for posting in any public channels).

import os
from toslack import SlackMessage
from toslack import SlackClient

client = SlackClient(token=os.environ['SLACK_BOT_TOKEN'], channel=os.environ['TO_SLACK_CHANNEL'])
message = SlackMessage(client=client)
message.post(text="Hello world!")

Or posting your message by the command line.

python slack.py --token xxx --channel C123456 post $message

Uploading files to Slack

You can just include a path to the file directly in the API call and upload it that way.

import os
from toslack import SlackMessage
from toslack import SlackClient

client = SlackClient(token=os.environ['SLACK_BOT_TOKEN'], channel=os.environ['TO_SLACK_CHANNEL'])
message = SlackMessage(client=client)
message.upload(file_path="./tmp.txt")

Or upload by specifying the file path on the command line.

python slack.py --token xxx --channel C123456 upload $file_path

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

toslack-0.1.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

toslack-0.1.0-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

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