From 1b6f34d3eaea300210cf298a2d8eac1781cd2c1d Mon Sep 17 00:00:00 2001 From: harshit-lyzr Date: Mon, 18 Nov 2024 13:36:37 +0530 Subject: [PATCH] [Added] Slack Integration --- __init__.py | 0 lyzr_agent_api/__init__.py | 2 ++ lyzr_agent_api/utils/__init__.py | 0 lyzr_agent_api/utils/slack.py | 55 ++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 __init__.py create mode 100644 lyzr_agent_api/utils/__init__.py create mode 100644 lyzr_agent_api/utils/slack.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lyzr_agent_api/__init__.py b/lyzr_agent_api/__init__.py index 66e124e..4cadbb5 100644 --- a/lyzr_agent_api/__init__.py +++ b/lyzr_agent_api/__init__.py @@ -7,6 +7,7 @@ from lyzr_agent_api.models.user import UserCreate, UserUpdate from lyzr_agent_api.models.tools import OpenAPISchema from lyzr_agent_api.client import AgentAPI +from .utils.slack import SlackLyzrBot __all__ = [ "ChatRequest", @@ -22,4 +23,5 @@ "UserUpdate", "AgentAPI", "OpenAPISchema", + "SlackLyzrBot" ] diff --git a/lyzr_agent_api/utils/__init__.py b/lyzr_agent_api/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lyzr_agent_api/utils/slack.py b/lyzr_agent_api/utils/slack.py new file mode 100644 index 0000000..2f98f80 --- /dev/null +++ b/lyzr_agent_api/utils/slack.py @@ -0,0 +1,55 @@ +from slack_bolt.adapter.socket_mode import SocketModeHandler +from slack_sdk import WebClient +from slack_bolt import App +import os +from lyzr_agent_api.client import AgentAPI +from lyzr_agent_api.models.chat import ChatRequest + + +class SlackLyzrBot: + def __init__(self, slack_bot_token, slack_app_token, lyzr_api_key, agent_id): + # Initialize Slack App and WebClient + self.app = App(token=slack_bot_token) + self.client = WebClient(slack_bot_token) + + # Initialize Lyzr Agent API + self.lyzr_client = AgentAPI(x_api_key=lyzr_api_key) + self.agent_id = agent_id + + # Register event handlers + self.app.event("app_mention")(self.handle_app_mention) + + def handle_app_mention(self, body, logger): + """Handles 'app_mention' events.""" + try: + # Extract the prompt from the Slack message + prompt = str(body["event"]["text"]).split(">")[1].strip() + + # Chat with the Lyzr agent + response = self.lyzr_client.chat_with_agent( + json_body=ChatRequest( + user_id=body["event"]["user"], + agent_id=self.agent_id, + message=prompt, + session_id=body["event"]["channel"], + ) + ) + + # Post the agent's response back to Slack + self.client.chat_postMessage( + channel=body["event"]["channel"], + thread_ts=body["event"]["event_ts"], + text=f"{response['response']}" + ) + except Exception as e: + logger.error(f"Error handling app mention: {e}") + self.client.chat_postMessage( + channel=body["event"]["channel"], + thread_ts=body["event"]["event_ts"], + text="Sorry, something went wrong while processing your request. :cry:" + ) + + def start(self, slack_app_token): + """Starts the Slack bot.""" + handler = SocketModeHandler(self.app, slack_app_token) + handler.start() \ No newline at end of file