Skip to content

Commit 7953a72

Browse files
committed
add work objects sample
1 parent c82bad8 commit 7953a72

File tree

9 files changed

+233
-0
lines changed

9 files changed

+233
-0
lines changed

messaging/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Messaging
2+
3+
Core Slack functionality!
4+
5+
Read the [docs](https://docs.slack.dev/messaging/) to learn about messaging in Slack.
6+
7+
## What's on display
8+
9+
- **[Work Objects](https://docs.slack.dev/messaging/work-objects/)**: Preview and interact with your app data in Slack.

messaging/work-objects/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__pycache__
2+
.mypy_cache
3+
.pytest_cache
4+
.ruff_cache
5+
.venv
6+
.slack

messaging/work-objects/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Work Objects Showcase
2+
3+
Your app can respond to links being shared in Slack with Work Object metadata to display a link preview. Users can click the preview to view authenticated data in the flexpane. If supported, users can edit app data directly from the flexpane.
4+
5+
You can also [post](https://docs.slack.dev/messaging/work-objects/#implementation-notifications) Work Object metadata directly with `chat.postMessage`, without a link being shared.
6+
7+
Read the [docs](https://docs.slack.dev/messaging/work-objects/) to learn more!
8+
9+
## Running locally
10+
11+
### 1. Setup environment variables
12+
13+
```zsh
14+
# Replace with your tokens
15+
export SLACK_BOT_TOKEN=<your-bot-token>
16+
export SLACK_APP_TOKEN=<your-app-level-token>
17+
```
18+
19+
### 2. Setup your local project
20+
21+
```zsh
22+
# Setup virtual environment
23+
python3 -m venv .venv
24+
source .venv/bin/activate
25+
26+
# Install the dependencies
27+
pip install -r requirements.txt
28+
```
29+
30+
### 3. Start the server
31+
```zsh
32+
python3 src/app.py
33+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"display_information": {
3+
"name": "Work Objects for Bolt Python App"
4+
},
5+
"features": {
6+
"rich_previews": {
7+
"is_active": true,
8+
"entity_types": [
9+
"slack#/entities/task"
10+
]
11+
},
12+
"unfurl_domains": [
13+
"tmpdomain.com"
14+
],
15+
"app_home": {
16+
"home_tab_enabled": false,
17+
"messages_tab_enabled": true,
18+
"messages_tab_read_only_enabled": false
19+
},
20+
"bot_user": {
21+
"display_name": "WorkObjectsApp",
22+
"always_online": true
23+
}
24+
},
25+
"oauth_config": {
26+
"scopes": {
27+
"bot": ["channels:history", "chat:write", "im:history", "links:read", "links:write"]
28+
}
29+
},
30+
"settings": {
31+
"event_subscriptions": {
32+
"bot_events": ["link_shared", "entity_details_requested"]
33+
},
34+
"interactivity": {
35+
"is_enabled": true
36+
},
37+
"org_deploy_enabled": true,
38+
"socket_mode_enabled": true,
39+
"token_rotation_enabled": false
40+
}
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mypy==1.18.2
2+
pytest==9.0.1
3+
ruff==0.14.5
4+
#slack_sdk==3.39.0
5+
slack-cli-hooks<1.0.0

messaging/work-objects/src/__init__.py

Whitespace-only changes.

messaging/work-objects/src/app.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import logging
3+
from slack_bolt import App
4+
from slack_bolt.adapter.socket_mode import SocketModeHandler
5+
from slack_sdk import WebClient
6+
from slack_sdk.models.metadata import EventAndEntityMetadata
7+
from metadata import sample_entities
8+
9+
10+
# Initializes your app with your bot token
11+
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
12+
13+
14+
# Listens for the link_shared event
15+
# https://docs.slack.dev/reference/events/link_shared/
16+
@app.event("link_shared")
17+
def link_shared_callback(event: dict, client: WebClient, logger: logging.Logger):
18+
try:
19+
for link in event["links"]:
20+
entity = sample_entities[link["url"]]
21+
if entity is not None:
22+
client.chat_unfurl(
23+
channel=event["channel"],
24+
ts=event["message_ts"],
25+
metadata=EventAndEntityMetadata(entities=[entity]),
26+
)
27+
else:
28+
logger.info("No entity found with URL " + link)
29+
except Exception as e:
30+
logger.error(
31+
f"An error occurred while handling the entity_details_requested event: {type(e).__name__} - {str(e)}",
32+
exc_info=e,
33+
)
34+
35+
36+
# Listens for the entity_details_requested event, which is sent when a user opens the flexpane
37+
# https://docs.slack.dev/reference/events/entity_details_requested/
38+
@app.event("entity_details_requested")
39+
def entity_details_callback(event: dict, client: WebClient, logger: logging.Logger):
40+
try:
41+
entity_url = event["entity_url"]
42+
entity = sample_entities[entity_url]
43+
if entity is not None:
44+
client.entity_presentDetails(
45+
trigger_id=event["trigger_id"], metadata=entity
46+
)
47+
else:
48+
logger.info("No entity found with URL " + entity_url)
49+
except Exception as e:
50+
logger.error(
51+
f"An error occurred while handling the entity_details_requested event: {type(e).__name__} - {str(e)}",
52+
exc_info=e,
53+
)
54+
55+
56+
# Listens for the view_submission event sent when the user submits edits in the flexpane
57+
# https://docs.slack.dev/tools/bolt-js/concepts/view-submissions/
58+
# https://docs.slack.dev/messaging/work-objects/#editing-view-submission
59+
@app.view_submission("work-object-edit")
60+
def work_object_edit_callback(view: dict, body: dict, client: WebClient, logger: logging.Logger, ack):
61+
try:
62+
ack()
63+
64+
entity_url = view["entity_url"]
65+
entity = sample_entities[entity_url]
66+
67+
attributes = entity.entity_payload.entity_attributes
68+
attributes.title.text = view["state"]["values"]["title"]["title.input"]["value"]
69+
entity.entity_payload.entity_attributes = attributes
70+
71+
entity.entity_payload.fields.description.value = view["state"]["values"]["description"]["description.input"]["value"]
72+
73+
if entity is not None:
74+
client.entity_presentDetails(
75+
trigger_id=body["trigger_id"], metadata=entity
76+
)
77+
else:
78+
logger.info("No entity found with URL " + entity_url)
79+
except Exception as e:
80+
logger.error(
81+
f"An error occurred while handling the entity_details_requested event: {type(e).__name__} - {str(e)}",
82+
exc_info=e,
83+
)
84+
85+
86+
# Start your app
87+
if __name__ == "__main__":
88+
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from slack_sdk.models.metadata import (
2+
EntityMetadata,
3+
EntityType,
4+
ExternalRef,
5+
EntityPayload,
6+
EntityAttributes,
7+
EntityTitle,
8+
TaskEntityFields,
9+
EntityStringField,
10+
EntityTitle,
11+
EntityAttributes,
12+
TaskEntityFields,
13+
EntityStringField,
14+
EntityEditSupport,
15+
EntityCustomField,
16+
ExternalRef,
17+
)
18+
19+
# Update the URL here to match your app's domain
20+
sample_task_unfurl_url = "https://wo-python-nov2025.com/task"
21+
22+
sample_entities = {
23+
sample_task_unfurl_url: EntityMetadata(
24+
entity_type="slack#/entities/task",
25+
url=sample_task_unfurl_url,
26+
app_unfurl_url=sample_task_unfurl_url,
27+
external_ref=ExternalRef(id="sample_task_id"),
28+
entity_payload=EntityPayload(
29+
attributes=EntityAttributes(
30+
title=EntityTitle(
31+
text="My Title",
32+
edit=EntityEditSupport(enabled=True),
33+
)
34+
),
35+
fields=TaskEntityFields(
36+
description=EntityStringField(
37+
value="Hello World!",
38+
edit=EntityEditSupport(enabled=True)
39+
)
40+
),
41+
custom_fields=[
42+
EntityCustomField(
43+
key="hello-world",
44+
label="hello-world",
45+
value="hello-world",
46+
type="string",
47+
)
48+
],
49+
),
50+
)
51+
}

messaging/work-objects/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)