send_slack_notification
Post a message to a Slack channel from Python through an Incoming Webhook URL.
send_slack_notification(webhook_url: str, text: str) -> dictSends a message to a Slack channel using an Incoming Webhook URL, so your team hears about a conversation where they already work. Use it for events such as refund requests, escalations and high-value orders.
Parameters
| Name | Type | Description |
|---|---|---|
webhook_url | str | The Slack Incoming Webhook URL (starts with https://hooks.slack.com/services/). See setup steps below |
text | str | The message text to send. Supports Slack's mrkdwn formatting |
Returns
dict: {"success": bool, "message": str}
Examples
Notify your team about a refund request
def execute_action(context):
order_id = context["args"]["orderId"]
reason = context["args"]["reason"]
conv_id = context["conversation"]["publicId"]
send_slack_notification(
"https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXX",
f"*Refund requested*\nOrder: {order_id}\nReason: {reason}\nConversation: {conv_id}",
)
return {"success": True, "message": "Your refund request has been submitted."}Ask someone to make a decision
Post a link to a review page, and the bot continues once the reviewer records an outcome. See the external decisions playbook.
Alert on high-value orders
def execute_action(context):
order = fetch_order(context["args"]["orderId"])
if order["total"] > 500:
send_slack_notification(
"https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXX",
f"*High-value order placed*\nOrder: {order['id']}\nTotal: ${order['total']}\nCustomer: {order['email']}",
)
return {"orderId": order["id"], "status": order["status"]}Setting up an Incoming Webhook
Before you can call send_slack_notification, you need a Slack Incoming Webhook URL. This is a one-time setup per channel.
Step 1: Create a Slack app
- Go to api.slack.com/apps and click Create New App
- Under From scratch, choose Pick features
- Give it a name (e.g., "Octocom Notifications") and select the workspace you want to post to
- When asked for Bot scopes, add only
incoming-webhook. Skip Bot events: the webhook only posts messages - Click Create App
Step 2: Enable Incoming Webhooks
- In the app settings sidebar, click Incoming Webhooks
- Toggle Activate Incoming Webhooks to On
Step 3: Create a webhook for a channel
- Click Add New Webhook to Workspace
- Select the channel where you want notifications to appear (e.g.,
#customer-support) - Click Allow
- Slack generates a webhook URL that looks like:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX- Copy this URL. You'll pass it to
send_slack_notificationaswebhook_url
Adding more channels
Repeat Step 3 for each channel you want to post to. Each channel gets its own webhook URL. You can use different URLs in different actions to route notifications to the right team.
Security
- Treat the webhook URL as a secret. Anyone with the URL can post to your channel. Store it in Secrets and read it with
get_secretinstead of pasting it into code. - Do not share webhook URLs in public repositories or client-facing code.
- If a webhook URL is compromised, revoke it in the Slack app settings and create a new one.