|
| 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