Skip to content

Commit 1a86371

Browse files
authored
Add Agents & Assistants feature (#1162)
1 parent a09582c commit 1a86371

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2934
-11
lines changed

examples/assistants/app.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import logging
2+
import os
3+
import time
4+
5+
from slack_bolt.context.get_thread_context.get_thread_context import GetThreadContext
6+
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
from slack_bolt import App, Assistant, SetStatus, SetTitle, SetSuggestedPrompts, Say
10+
from slack_bolt.adapter.socket_mode import SocketModeHandler
11+
12+
app = App(token=os.environ["SLACK_BOT_TOKEN"])
13+
14+
15+
assistant = Assistant()
16+
# You can use your own thread_context_store if you want
17+
# from slack_bolt import FileAssistantThreadContextStore
18+
# assistant = Assistant(thread_context_store=FileAssistantThreadContextStore())
19+
20+
21+
@assistant.thread_started
22+
def start_thread(say: Say, set_suggested_prompts: SetSuggestedPrompts):
23+
say(":wave: Hi, how can I help you today?")
24+
set_suggested_prompts(
25+
prompts=[
26+
"What does SLACK stand for?",
27+
"When Slack was released?",
28+
]
29+
)
30+
31+
32+
@assistant.user_message(matchers=[lambda payload: "help page" in payload["text"]])
33+
def find_help_pages(
34+
payload: dict,
35+
logger: logging.Logger,
36+
set_title: SetTitle,
37+
set_status: SetStatus,
38+
say: Say,
39+
):
40+
try:
41+
set_title(payload["text"])
42+
set_status("Searching help pages...")
43+
time.sleep(0.5)
44+
say("Please check this help page: https://www.example.com/help-page-123")
45+
except Exception as e:
46+
logger.exception(f"Failed to respond to an inquiry: {e}")
47+
say(f":warning: Sorry, something went wrong during processing your request (error: {e})")
48+
49+
50+
@assistant.user_message
51+
def answer_other_inquiries(
52+
payload: dict,
53+
logger: logging.Logger,
54+
set_title: SetTitle,
55+
set_status: SetStatus,
56+
say: Say,
57+
get_thread_context: GetThreadContext,
58+
):
59+
try:
60+
set_title(payload["text"])
61+
set_status("Typing...")
62+
time.sleep(0.3)
63+
set_status("Still typing...")
64+
time.sleep(0.3)
65+
thread_context = get_thread_context()
66+
if thread_context is not None:
67+
channel = thread_context.channel_id
68+
say(f"Ah, you're referring to <#{channel}>! Do you need help with the channel?")
69+
else:
70+
say("Here you are! blah-blah-blah...")
71+
except Exception as e:
72+
logger.exception(f"Failed to respond to an inquiry: {e}")
73+
say(f":warning: Sorry, something went wrong during processing your request (error: {e})")
74+
75+
76+
app.use(assistant)
77+
78+
79+
@app.event("message")
80+
def handle_message_in_channels():
81+
pass # noop
82+
83+
84+
@app.event("app_mention")
85+
def handle_non_assistant_thread_messages(say: Say):
86+
say(":wave: I can help you out within our 1:1 DM!")
87+
88+
89+
if __name__ == "__main__":
90+
SocketModeHandler(app, app_token=os.environ["SLACK_APP_TOKEN"]).start()
91+
92+
# pip install slack_bolt
93+
# export SLACK_APP_TOKEN=xapp-***
94+
# export SLACK_BOT_TOKEN=xoxb-***
95+
# python app.py

examples/assistants/async_app.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import logging
2+
import os
3+
import asyncio
4+
5+
from slack_bolt.context.get_thread_context.async_get_thread_context import AsyncGetThreadContext
6+
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
from slack_bolt.async_app import AsyncApp, AsyncAssistant, AsyncSetTitle, AsyncSetStatus, AsyncSetSuggestedPrompts, AsyncSay
10+
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
11+
12+
app = AsyncApp(token=os.environ["SLACK_BOT_TOKEN"])
13+
14+
15+
assistant = AsyncAssistant()
16+
17+
18+
@assistant.thread_started
19+
async def start_thread(say: AsyncSay, set_suggested_prompts: AsyncSetSuggestedPrompts):
20+
await say(":wave: Hi, how can I help you today?")
21+
await set_suggested_prompts(
22+
prompts=[
23+
"What does SLACK stand for?",
24+
"When Slack was released?",
25+
]
26+
)
27+
28+
29+
@assistant.user_message(matchers=[lambda body: "help page" in body["event"]["text"]])
30+
async def find_help_pages(
31+
payload: dict,
32+
logger: logging.Logger,
33+
set_title: AsyncSetTitle,
34+
set_status: AsyncSetStatus,
35+
say: AsyncSay,
36+
):
37+
try:
38+
await set_title(payload["text"])
39+
await set_status("Searching help pages...")
40+
await asyncio.sleep(0.5)
41+
await say("Please check this help page: https://www.example.com/help-page-123")
42+
except Exception as e:
43+
logger.exception(f"Failed to respond to an inquiry: {e}")
44+
await say(f":warning: Sorry, something went wrong during processing your request (error: {e})")
45+
46+
47+
@assistant.user_message
48+
async def answer_other_inquiries(
49+
payload: dict,
50+
logger: logging.Logger,
51+
set_title: AsyncSetTitle,
52+
set_status: AsyncSetStatus,
53+
say: AsyncSay,
54+
get_thread_context: AsyncGetThreadContext,
55+
):
56+
try:
57+
await set_title(payload["text"])
58+
await set_status("Typing...")
59+
await asyncio.sleep(0.3)
60+
await set_status("Still typing...")
61+
await asyncio.sleep(0.3)
62+
thread_context = await get_thread_context()
63+
if thread_context is not None:
64+
channel = thread_context.channel_id
65+
await say(f"Ah, you're referring to <#{channel}>! Do you need help with the channel?")
66+
else:
67+
await say("Here you are! blah-blah-blah...")
68+
except Exception as e:
69+
logger.exception(f"Failed to respond to an inquiry: {e}")
70+
await say(f":warning: Sorry, something went wrong during processing your request (error: {e})")
71+
72+
73+
app.use(assistant)
74+
75+
76+
@app.event("message")
77+
async def handle_message_in_channels():
78+
pass # noop
79+
80+
81+
@app.event("app_mention")
82+
async def handle_non_assistant_thread_messages(say: AsyncSay):
83+
await say(":wave: I can help you out within our 1:1 DM!")
84+
85+
86+
async def main():
87+
handler = AsyncSocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
88+
await handler.start_async()
89+
90+
91+
if __name__ == "__main__":
92+
asyncio.run(main())
93+
94+
# pip install slack_bolt aiohttp
95+
# export SLACK_APP_TOKEN=xapp-***
96+
# export SLACK_BOT_TOKEN=xoxb-***
97+
# python async_app.py

0 commit comments

Comments
 (0)