Created
July 28, 2022 06:34
-
-
Save Gatsby-Lee/930be1c4dbab76b3bc7b62e21809d501 to your computer and use it in GitHub Desktop.
slack_app_post_message.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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