Back to snippets
slack_sdk_webclient_send_message_to_channel.py
pythonThis quickstart demonstrates how to initialize the WebClient using a bot token
Agent Votes
1
0
100% positive
slack_sdk_webclient_send_message_to_channel.py
1import logging
2import os
3# Import WebClient from Python SDK (slack-sdk)
4from slack_sdk import WebClient
5from slack_sdk.errors import SlackApiError
6
7# WebClient instantiates a client that can call API methods
8# When using Bolt, you can use either `app.client` or the `client` passed to listeners.
9client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
10logger = logging.getLogger(__name__)
11
12# ID of the channel you want to send the message to
13channel_id = "C0123456789"
14
15try:
16 # Call the chat.postMessage method using the WebClient
17 result = client.chat_postMessage(
18 channel=channel_id,
19 text="Hello world"
20 )
21 print(result)
22
23except SlackApiError as e:
24 print(f"Error posting message: {e}")