Skip to content

Instantly share code, notes, and snippets.

@Gatsby-Lee
Created July 28, 2022 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gatsby-Lee/930be1c4dbab76b3bc7b62e21809d501 to your computer and use it in GitHub Desktop.
Save Gatsby-Lee/930be1c4dbab76b3bc7b62e21809d501 to your computer and use it in GitHub Desktop.
slack_app_post_message.py
import logging
import os
# Import WebClient from Python SDK (github.com/slackapi/python-slack-sdk)
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
# WebClient instantiates a client that can call API methods
# When using Bolt, you can use either `app.client` or the `client` passed to listeners.
channel_id = "#general"
token = ""
client = WebClient(token=token)
logger = logging.getLogger(__name__)
# ID of the channel you want to send the message to
try:
# Call the chat.postMessage method using the WebClient
result = client.chat_postMessage(channel=channel_id, text="Hello world")
logger.info(result)
except SlackApiError as e:
logger.error(f"Error posting message: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment