š« CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: integrate slack app (#1058)
koch309276fe7d0e1272
diff --git a/camel/bots/__init__.py b/camel/bots/__init__.pyindex a09c2b0ee..d276018ad 100644--- a/camel/bots/__init__.py+++ b/camel/bots/__init__.py@@ -12,9 +12,23 @@# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from .discord_app import DiscordApp+from .slack.models import (+ SlackAppMentionEventBody,+ SlackAppMentionEventProfile,+ SlackAuthProfile,+ SlackEventBody,+ SlackEventProfile,+)+from .slack.slack_app import SlackAppfrom .telegram_bot import TelegramBot__all__ = ['DiscordApp',+ 'SlackApp',+ 'SlackAppMentionEventBody',+ 'SlackAppMentionEventProfile',+ 'SlackAuthProfile',+ 'SlackEventBody',+ 'SlackEventProfile','TelegramBot',]diff --git a/camel/bots/slack/__init__.py b/camel/bots/slack/__init__.pynew file mode 100644index 000000000..922806ad2--- /dev/null+++ b/camel/bots/slack/__init__.py@@ -0,0 +1,30 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from .models import (+ SlackAppMentionEventBody,+ SlackAppMentionEventProfile,+ SlackAuthProfile,+ SlackEventBody,+ SlackEventProfile,+)+from .slack_app import SlackApp++__all__ = [+ 'SlackApp',+ 'SlackAppMentionEventBody',+ 'SlackAppMentionEventProfile',+ 'SlackAuthProfile',+ 'SlackEventBody',+ 'SlackEventProfile',+]diff --git a/camel/bots/slack/models.py b/camel/bots/slack/models.pynew file mode 100644index 000000000..3d8191ddc--- /dev/null+++ b/camel/bots/slack/models.py@@ -0,0 +1,158 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from typing import Optional++from pydantic import BaseModel+++class SlackAuthProfile(BaseModel):+ r"""Represents the authorization profile within a Slack event.++ Events will contain a single, compact authorizations field that shows one+ installation of your app that the event is visible to.+ In other words, lists of authorizations will be truncated to one element.++ If there's more than one installing party that your app is keeping track+ of, it's best not to rely on the single party listed in authorizations to+ be any particular one.++ To get a full list of who can see events, call the apps.event.+ authorizations.list method after obtaining an app-level token. Read more on+ the changes here; they have taken effect for existing apps as of+ February 24, 2021.++ References:++ - https://api.slack.com/apis/events-api#authorizations+ - https://api.slack.com/changelog/2020-09-15-events-api-truncate-authed-users#no_context+ """++ enterprise_id: Optional[str] = None+ """The ID of the enterprise associated with the authorization."""++ team_id: str+ """The ID of the team associated with the authorization."""++ user_id: str+ """The ID of the user associated with the authorization."""++ is_bot: bool+ """Whether the authorized user is a bot."""++ is_enterprise_install: bool+ """Whether the authorization is for an enterprise installation."""+++class SlackEventProfile(BaseModel):+ r"""Represents the detailed profile of a Slack event, including user,+ message, and context data.+ """++ user: str+ """The ID of the user associated with the event."""++ type: str+ """The type of the event (e.g., 'message')."""++ ts: str+ """A timestamp representing when the event was triggered."""++ thread_ts: Optional[str] = None+ """The timestamp of the parent message in a thread."""++ client_msg_id: str+ """A unique ID generated by the client for the message (if available)."""++ text: str+ """The message content text."""++ team: str+ """The ID of the team that the event is associated with."""++ blocks: list+ """The list of message blocks, providing structured information."""++ channel: str+ """The ID of the Slack channel where the event happened."""++ event_ts: str+ """The event-specific timestamp when it occurred."""++ channel_type: Optional[str]+ """The type of Slack channel (e.g., 'channel', 'im')."""+++class SlackEventBody(BaseModel):+ r"""Represents the entire body of a Slack event, including the event+ profile, authorization, and context.+ """++ token: str+ """The token to verify the source of the event."""++ team_id: str+ """The ID of the team where the event is happening."""++ context_team_id: Optional[str]+ """The team ID for the shared channel context, if applicable."""++ context_enterprise_id: Optional[str] = None+ """The enterprise ID for the shared channel context, if applicable."""++ api_app_id: str+ """The unique identifier for the Slack app that received the event."""++ event: SlackEventProfile+ """A detailed profile of the event"""++ type: str+ """The overall type of event received (e.g., 'event_callback')."""++ event_id: str+ """A unique identifier assigned to this event by Slack."""++ event_time: int+ """The timestamp (in seconds) representing when the event was triggered."""++ authorizations: Optional[list[SlackAuthProfile]] = None+ """An optional list of authorizations that describe which installation can+ see the event."""++ is_ext_shared_channel: bool+ """Indicates if the event is part of a shared channel between different+ organizations."""++ event_context: str+ """A unique string representing the context of the event."""+++class SlackAppMentionEventProfile(SlackEventProfile):+ r"""Represents the detailed profile of a Slack event where the app was+ mentioned in a message.+ """++ channel_type: Optional[str] = None+ """The type of Slack channel. it's None for app mentions."""+++class SlackAppMentionEventBody(SlackEventBody):+ r"""Represents the entire body of a Slack event where the app was mentioned+ in a message.+ """++ context_team_id: Optional[str] = None+ """A detailed profile of the event. it's None for app mentions."""++ event: SlackAppMentionEventProfile+ """A detailed profile of the event"""diff --git a/camel/bots/slack/slack_app.py b/camel/bots/slack/slack_app.pynew file mode 100644index 000000000..e3606634b--- /dev/null+++ b/camel/bots/slack/slack_app.py@@ -0,0 +1,255 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import logging+import os+from typing import TYPE_CHECKING, Any, Dict, Optional++from slack_sdk.oauth.installation_store.async_installation_store import (+ AsyncInstallationStore,+)+from starlette import requests, responses++from camel.bots.slack.models import (+ SlackAppMentionEventBody,+ SlackAppMentionEventProfile,+ SlackEventBody,+ SlackEventProfile,+)+from camel.utils import dependencies_required++if TYPE_CHECKING:+ from slack_bolt.context.async_context import AsyncBoltContext+ from slack_bolt.context.say.async_say import AsyncSay+ from slack_sdk.web.async_client import AsyncWebClient++logging.basicConfig(level=logging.INFO)+logger = logging.getLogger(__name__)+++class SlackApp:+ r"""Represents a Slack app that is powered by a Slack Bolt `AsyncApp`.++ This class is responsible for initializing and managing the Slack+ application by setting up event handlers, running the app server, and+ handling events such as messages and mentions from Slack.++ Args:+ token (Optional[str]): Slack API token for authentication.+ scopes (Optional[str]): Slack app scopes for permissions.+ signing_secret (Optional[str]): Signing secret for verifying Slack+ requests.+ client_id (Optional[str]): Slack app client ID.+ client_secret (Optional[str]): Slack app client secret.+ redirect_uri_path (str): The URI path for OAuth redirect, defaults to+ "/slack/oauth_redirect".+ installation_store (Optional[AsyncInstallationStore]): The installation+ store for handling OAuth installations.+ """++ @dependencies_required('slack_bolt')+ def __init__(+ self,+ token: Optional[str] = None,+ scopes: Optional[str] = None,+ signing_secret: Optional[str] = None,+ client_id: Optional[str] = None,+ client_secret: Optional[str] = None,+ redirect_uri_path: str = "/slack/oauth_redirect",+ installation_store: Optional[AsyncInstallationStore] = None,+ ) -> None:+ r"""Initializes the SlackApp instance by setting up the Slack Bolt app+ and configuring event handlers and OAuth settings.++ Args:+ token (Optional[str]): The Slack API token.+ scopes (Optional[str]): The scopes for Slack app permissions.+ signing_secret (Optional[str]): The signing secret for verifying+ requests.+ client_id (Optional[str]): The Slack app client ID.+ client_secret (Optional[str]): The Slack app client secret.+ redirect_uri_path (str): The URI path for handling OAuth redirects+ (default is "/slack/oauth_redirect").+ installation_store (Optional[AsyncInstallationStore]): An optional+ installation store for OAuth installations.+ """+ from slack_bolt.adapter.starlette.async_handler import (+ AsyncSlackRequestHandler,+ )+ from slack_bolt.app.async_app import AsyncApp+ from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings++ self.token: Optional[str] = token or os.getenv("SLACK_TOKEN")+ self.scopes: Optional[str] = scopes or os.getenv("SLACK_SCOPES")+ self.signing_secret: Optional[str] = signing_secret or os.getenv(+ "SLACK_SIGNING_SECRET"+ )+ self.client_id: Optional[str] = client_id or os.getenv(+ "SLACK_CLIENT_ID"+ )+ self.client_secret: Optional[str] = client_secret or os.getenv(+ "SLACK_CLIENT_SECRET"+ )++ if not all([self.token, self.scopes, self.signing_secret]):+ raise ValueError(+ "`SLACK_TOKEN`, `SLACK_SCOPES`, and `SLACK_SIGNING_SECRET` "+ "environment variables must be set. Get it here: "+ "`https://api.slack.com/apps`."+ )++ # Setup OAuth settings if client ID and secret are provided+ if self.client_id and self.client_secret:+ self._app = AsyncApp(+ oauth_settings=AsyncOAuthSettings(+ client_id=self.client_id,+ client_secret=self.client_secret,+ scopes=self.scopes,+ redirect_uri_path=redirect_uri_path,+ ),+ logger=logger,+ signing_secret=self.signing_secret,+ installation_store=installation_store,+ token=self.token,+ )+ else:+ # Initialize Slack Bolt AsyncApp with settings+ self._app = AsyncApp(+ logger=logger,+ signing_secret=self.signing_secret,+ installation_store=installation_store,+ token=self.token,+ )++ self._handler = AsyncSlackRequestHandler(self._app)+ self.setup_handlers()++ def setup_handlers(self) -> None:+ r"""Sets up the event handlers for Slack events, such as `app_mention`+ and `message`.++ This method registers the `app_mention` and `on_message` event handlers+ with the Slack Bolt app to respond to Slack events.+ """+ self._app.event("app_mention")(self.app_mention)+ self._app.event("message")(self.on_message)++ def run(+ self,+ port: int = 3000,+ path: str = "/slack/events",+ host: Optional[str] = None,+ ) -> None:+ r"""Starts the Slack Bolt app server to listen for incoming Slack+ events.++ Args:+ port (int): The port on which the server should run (default is+ 3000).+ path (str): The endpoint path for receiving Slack events (default+ is "/slack/events").+ host (Optional[str]): The hostname to bind the server (default is+ None).+ """+ self._app.start(port=port, path=path, host=host)++ async def handle_request(+ self, request: requests.Request+ ) -> responses.Response:+ r"""Handles incoming requests from Slack through the request handler.++ Args:+ request (Request): A Starlette request object representing the+ incoming request.++ Returns:+ The response generated by the Slack Bolt handler.+ """+ return await self._handler.handle(request)++ async def app_mention(+ self,+ context: "AsyncBoltContext",+ client: "AsyncWebClient",+ event: Dict[str, Any],+ body: Dict[str, Any],+ say: "AsyncSay",+ ) -> None:+ r"""Event handler for `app_mention` events.++ This method is triggered when someone mentions the app in Slack.++ Args:+ context (AsyncBoltContext): The Slack Bolt context for the event.+ client (AsyncWebClient): The Slack Web API client.+ event (Dict[str, Any]): The event data for the app mention.+ body (Dict[str, Any]): The full request body from Slack.+ say (AsyncSay): A function to send a response back to the channel.+ """+ event_profile = SlackAppMentionEventProfile(**event)+ event_body = SlackAppMentionEventBody(**body)++ logger.info(f"app_mention, context: {context}")+ logger.info(f"app_mention, client: {client}")+ logger.info(f"app_mention, event_profile: {event_profile}")+ logger.info(f"app_mention, event_body: {event_body}")+ logger.info(f"app_mention, say: {say}")++ async def on_message(+ self,+ context: "AsyncBoltContext",+ client: "AsyncWebClient",+ event: Dict[str, Any],+ body: Dict[str, Any],+ say: "AsyncSay",+ ) -> None:+ r"""Event handler for `message` events.++ This method is triggered when the app receives a message in Slack.++ Args:+ context (AsyncBoltContext): The Slack Bolt context for the event.+ client (AsyncWebClient): The Slack Web API client.+ event (Dict[str, Any]): The event data for the message.+ body (Dict[str, Any]): The full request body from Slack.+ say (AsyncSay): A function to send a response back to the channel.+ """+ await context.ack()++ event_profile = SlackEventProfile(**event)+ event_body = SlackEventBody(**body)++ logger.info(f"on_message, context: {context}")+ logger.info(f"on_message, client: {client}")+ logger.info(f"on_message, event_profile: {event_profile}")+ logger.info(f"on_message, event_body: {event_body}")+ logger.info(f"on_message, say: {say}")++ logger.info(f"Received message: {event_profile.text}")++ def mention_me(+ self, context: "AsyncBoltContext", body: SlackEventBody+ ) -> bool:+ r"""Check if the bot is mentioned in the message.++ Args:+ context (AsyncBoltContext): The Slack Bolt context for the event.+ body (SlackEventBody): The body of the Slack event.++ Returns:+ bool: True if the bot is mentioned in the message, False otherwise.+ """+ message = body.event.text+ bot_user_id = context.bot_user_id+ mention = f"<@{bot_user_id}>"+ return mention in messagediff --git a/examples/bots/slack_bot.py b/examples/bots/slack_bot.pynew file mode 100644index 000000000..e515cb190--- /dev/null+++ b/examples/bots/slack_bot.py@@ -0,0 +1,209 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import logging+from typing import List, Optional, Union++from slack_bolt.context.async_context import AsyncBoltContext+from slack_bolt.context.say.async_say import AsyncSay+from slack_sdk.web.async_client import AsyncWebClient++from camel.agents import ChatAgent+from camel.bots import SlackApp, SlackEventBody+from camel.messages import BaseMessage+from camel.retrievers import AutoRetriever+from camel.types import StorageType++try:+ from unstructured.documents.elements import Element+except ImportError:+ Element = None++logging.basicConfig(level=logging.INFO)+logger = logging.getLogger(__name__)+++class BotAgent:+ def __init__(+ self,+ contents: Union[str, List[str], Element, List[Element]] = None,+ auto_retriever: Optional[AutoRetriever] = None,+ similarity_threshold: float = 0.5,+ vector_storage_local_path: str = "local_data/",+ top_k: int = 1,+ return_detailed_info: bool = True,+ ):+ r"""Initialize the BotAgent instance.++ Args:+ contents (Union[str, List[str], Element, List[Element]], optional)+ : The content to be retrieved.+ auto_retriever (Optional[AutoRetriever], optional): An instance of+ AutoRetriever for vector search.+ similarity_threshold (float): Threshold for vector similarity when+ retrieving content.+ vector_storage_local_path (str): Path to local vector storage for+ the retriever.+ top_k (int): Number of top results to retrieve.+ return_detailed_info (bool): Whether to return detailed+ information from the retriever.+ """+ assistant_sys_msg: BaseMessage = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content='''+ Objective:+ You are a customer service bot designed to assist users+ with inquiries related to our open-source project.+ Your responses should be informative, concise, and helpful.++ Instructions:+ Understand User Queries: Carefully read and understand the+ user's question. Focus on keywords and context to+ determine the user's intent.+ Search for Relevant Information: Use the provided dataset+ and refer to the RAG (file to find answers that+ closely match the user's query. The RAG file+ contains detailed interactions and should be your+ primary resource for crafting responses.+ Provide Clear and Concise Responses: Your answers should+ be clear and to the point. Avoid overly technical+ language unless the user's query indicates+ familiarity with technical terms.+ Encourage Engagement: Where applicable, encourage users+ to contribute to the project or seek further+ assistance.++ Response Structure:+ Greeting: Begin with a polite greeting or acknowledgment.+ Main Response: Provide the main answer to the user's query.+ Additional Information: Offer any extra tips or direct the+ user to additional resources if necessary.+ Closing: Close the response politely, encouraging+ further engagement if appropriate.+ bd+ Tone:+ Professional: Maintain a professional tone that+ instills confidence in the user.+ Friendly: Be approachable and friendly to make users+ feel comfortable.+ Helpful: Always aim to be as helpful as possible,+ guiding users to solutions.+ ''',+ )++ self._agent = ChatAgent(+ assistant_sys_msg,+ message_window_size=10,+ )++ self._auto_retriever = None+ self._contents = contents+ self._top_k = top_k+ self._similarity_threshold = similarity_threshold+ self._return_detailed_info = return_detailed_info++ self._auto_retriever = auto_retriever or AutoRetriever(+ vector_storage_local_path=vector_storage_local_path,+ storage_type=StorageType.QDRANT,+ )++ async def process(self, message: str) -> str:+ user_raw_msg = message+ logger.info("User message:", user_raw_msg)+ if self._auto_retriever:+ retrieved_content = self._auto_retriever.run_vector_retriever(+ query=user_raw_msg,+ contents=self._contents,+ top_k=self._top_k,+ similarity_threshold=self._similarity_threshold,+ return_detailed_info=self._return_detailed_info,+ )+ user_raw_msg = (+ f"Here is the query to you: {user_raw_msg}\n"+ f"Based on the retrieved content: {retrieved_content}, \n"+ f"answer the query"+ )++ user_msg = BaseMessage.make_user_message(+ role_name="User", content=user_raw_msg+ )+ assistant_response = self._agent.step(user_msg)+ return assistant_response.msg.content+++class SlackBot(SlackApp):+ def __init__(+ self,+ agent: BotAgent,+ token: Optional[str] = None,+ scopes: Optional[str] = None,+ signing_secret: Optional[str] = None,+ client_id: Optional[str] = None,+ client_secret: Optional[str] = None,+ ):+ """Initializes the SlackBot instance to handle Slack messages and+ communicate with BotAgent.++ Args:+ agent (BotAgent): The BotAgent responsible for processing messages+ and generating responses.+ token (Optional[str]): Slack API token for authentication.+ scopes (Optional[str]): Slack app scopes for permissions.+ signing_secret (Optional[str]): Signing secret for verifying Slack+ requests.+ client_id (Optional[str]): Slack app client ID.+ client_secret (Optional[str]): Slack app client secret.+ """+ super().__init__(+ token=token,+ scopes=scopes,+ signing_secret=signing_secret,+ client_id=client_id,+ client_secret=client_secret,+ )+ self.agent: BotAgent = agent++ async def on_message(+ self,+ context: "AsyncBoltContext",+ client: "AsyncWebClient",+ event: dict,+ body: dict,+ say: "AsyncSay",+ ):+ """Handles incoming Slack messages, processes them with BotAgent, and+ responds.++ Args:+ context (AsyncBoltContext): Context object that contains+ information about the Slack event.+ client (AsyncWebClient): Slack Web API client for making API+ requests.+ event (dict): Event data containing details of the incoming Slack+ message.+ body (dict): Full request body from Slack.+ say (AsyncSay): A function to send a response back to the Slack+ channel.+ """+ await context.ack()+ event_body = SlackEventBody(**body)+ user_raw_msg = event_body.event.text+ response = await agent.process(user_raw_msg)+ await say(response)+++if __name__ == "__main__":+ agent = BotAgent()++ slack_bot = SlackBot(agent=agent)+ slack_bot.run(3000)diff --git a/examples/bots/slack_bot_use_msg_queue.py b/examples/bots/slack_bot_use_msg_queue.pynew file mode 100644index 000000000..1f9b0b1c4--- /dev/null+++ b/examples/bots/slack_bot_use_msg_queue.py@@ -0,0 +1,302 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import asyncio+import logging+import queue+import threading+from typing import List, Optional, Union++from slack_bolt.context.async_context import AsyncBoltContext+from slack_bolt.context.say.async_say import AsyncSay+from slack_sdk.web.async_client import AsyncWebClient++from camel.agents import ChatAgent+from camel.bots import SlackApp, SlackEventBody+from camel.messages import BaseMessage+from camel.retrievers import AutoRetriever+from camel.types import StorageType++try:+ from unstructured.documents.elements import Element+except ImportError:+ Element = None++logging.basicConfig(level=logging.INFO)+logger = logging.getLogger(__name__)+++class BotAgent:+ def __init__(+ self,+ contents: Union[str, List[str], Element, List[Element]] = None,+ auto_retriever: Optional[AutoRetriever] = None,+ similarity_threshold: float = 0.5,+ vector_storage_local_path: str = "local_data/",+ top_k: int = 1,+ return_detailed_info: bool = True,+ ):+ r"""Initialize the BotAgent instance.++ Args:+ contents (Union[str, List[str], Element, List[Element]], optional)+ : The content to be retrieved.+ auto_retriever (Optional[AutoRetriever], optional): An instance of+ AutoRetriever for vector search.+ similarity_threshold (float): Threshold for vector similarity when+ retrieving content.+ vector_storage_local_path (str): Path to local vector storage for+ the retriever.+ top_k (int): Number of top results to retrieve.+ return_detailed_info (bool): Whether to return detailed+ information from the retriever.+ """+ assistant_sys_msg: BaseMessage = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content='''+ Objective:+ You are a customer service bot designed to assist users+ with inquiries related to our open-source project.+ Your responses should be informative, concise, and helpful.++ Instructions:+ Understand User Queries: Carefully read and understand the+ user's question. Focus on keywords and context to+ determine the user's intent.+ Search for Relevant Information: Use the provided dataset+ and refer to the RAG (file to find answers that+ closely match the user's query. The RAG file+ contains detailed interactions and should be your+ primary resource for crafting responses.+ Provide Clear and Concise Responses: Your answers should+ be clear and to the point. Avoid overly technical+ language unless the user's query indicates+ familiarity with technical terms.+ Encourage Engagement: Where applicable, encourage users+ to contribute to the project or seek further+ assistance.++ Response Structure:+ Greeting: Begin with a polite greeting or acknowledgment.+ Main Response: Provide the main answer to the user's query.+ Additional Information: Offer any extra tips or direct the+ user to additional resources if necessary.+ Closing: Close the response politely, encouraging+ further engagement if appropriate.+ bd+ Tone:+ Professional: Maintain a professional tone that+ instills confidence in the user.+ Friendly: Be approachable and friendly to make users+ feel comfortable.+ Helpful: Always aim to be as helpful as possible,+ guiding users to solutions.+ ''',+ )++ self._agent = ChatAgent(+ assistant_sys_msg,+ message_window_size=10,+ )++ self._auto_retriever = None+ self._contents = contents+ self._top_k = top_k+ self._similarity_threshold = similarity_threshold+ self._return_detailed_info = return_detailed_info++ self._auto_retriever = auto_retriever or AutoRetriever(+ vector_storage_local_path=vector_storage_local_path,+ storage_type=StorageType.QDRANT,+ )++ async def process(self, message: str) -> str:+ r"""Process the user message, retrieve relevant content, and generate+ a response.++ Args:+ message (str): The user's query message.++ Returns:+ str: The assistant's response message.+ """+ user_raw_msg = message+ print("User message:", user_raw_msg)+ if self._auto_retriever:+ retrieved_content = self._auto_retriever.run_vector_retriever(+ query=user_raw_msg,+ contents=self._contents,+ top_k=self._top_k,+ similarity_threshold=self._similarity_threshold,+ return_detailed_info=self._return_detailed_info,+ )+ user_raw_msg = (+ f"Here is the query to you: {user_raw_msg}\n"+ f"Based on the retrieved content: {retrieved_content}, \n"+ f"answer the query"+ )++ user_msg = BaseMessage.make_user_message(+ role_name="User", content=user_raw_msg+ )+ assistant_response = self._agent.step(user_msg)+ return assistant_response.msg.content+++class SlackBot(SlackApp):+ r"""SlackBot class that extends the SlackApp class to handle Slack events+ and integrate with a message queue for asynchronous processing.++ This class initializes the Slack app and adds a message handler to process+ Slack events, specifically for incoming messages.++ Args:+ msg_queue (queue.Queue): A thread-safe queue to communicate between+ threads.+ token (Optional[str]): Slack API token for authentication.+ scopes (Optional[str]): Slack app scopes for permissions.+ signing_secret (Optional[str]): Signing secret for verifying Slack+ requests.+ client_id (Optional[str]): Slack app client ID.+ client_secret (Optional[str]): Slack app client secret.+ """++ def __init__(+ self,+ msg_queue: queue.Queue,+ token: Optional[str] = None,+ scopes: Optional[str] = None,+ signing_secret: Optional[str] = None,+ client_id: Optional[str] = None,+ client_secret: Optional[str] = None,+ ):+ r"""Initializes the SlackBot instance with a message queue and the+ required Slack authentication details.++ Args:+ msg_queue (queue.Queue): A thread-safe queue to communicate between+ threads.+ token (Optional[str]): Slack API token for authentication.+ scopes (Optional[str]): Slack app scopes for permissions.+ signing_secret (Optional[str]): Signing secret for verifying Slack+ requests.+ client_id (Optional[str]): Slack app client ID.+ client_secret (Optional[str]): Slack app client secret.+ """+ super().__init__(+ token=token,+ scopes=scopes,+ signing_secret=signing_secret,+ client_id=client_id,+ client_secret=client_secret,+ )+ self._queue: queue.Queue = msg_queue++ async def on_message(+ self,+ context: "AsyncBoltContext",+ client: "AsyncWebClient",+ event: dict,+ body: dict,+ say: "AsyncSay",+ ):+ r"""Event handler for processing incoming Slack messages.++ This method is called when a message event is received from Slack.+ It acknowledges the message and adds the event body and say function to+ the queue for further processing.++ Args:+ context (AsyncBoltContext): Context object that contains+ information about the Slack event.+ client (AsyncWebClient): Slack Web API client for making API+ requests.+ event (dict): Event data containing details of the incoming Slack+ message.+ body (dict): Full request body from Slack.+ say (AsyncSay): A function to send a response back to the Slack+ channel.+ """+ await context.ack()+ event_body = SlackEventBody(**body)+ self._queue.put((event_body, say))+++async def process_message(agent: BotAgent, msg_queue: queue.Queue):+ r"""Process messages from the queue asynchronously.++ This function continuously retrieves messages from the message queue,+ processes them using the `BotAgent` instance, and sends the response back+ to Slack using the `say` function.++ Args:+ agent (BotAgent): An instance of the `BotAgent` class that handles the+ processing of the incoming message.+ msg_queue (queue.Queue): A thread-safe queue that stores Slack event+ messages and the corresponding `say` functions for response.+ """+ while True:+ event_body, say = msg_queue.get()++ logger.info(f"Received message: {event_body.event.text}")+ user_raw_msg = event_body.event.text++ # Process the message using the agent and send response back to Slack.+ response = await agent.process(user_raw_msg)+ await say(response)+ msg_queue.task_done()+++def start_async_queue_processor(agent: BotAgent, msg_queue: queue.Queue):+ r"""Start an asynchronous queue processor in a new event loop.++ This function creates a new asyncio event loop in a separate thread to+ asynchronously process messages from the queue. It will run until all+ the messages in the queue have been processed.++ Args:+ agent (BotAgent): The agent responsible for processing the messages.+ msg_queue (queue.Queue): The message queue that contains Slack events+ and responses.+ """+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)++ # Schedule the asynchronous message processing task in this event loop+ loop.run_until_complete(process_message(agent, msg_queue))+++if __name__ == "__main__":+ r"""Main entry point for running the Slack bot application.++ This section initializes the required components including the message+ queue, agent, and the SlackBot instance. It also starts a separate thread+ for asynchronous message processing to avoid blocking the Slack bot's main+ event loop. The `slack_bot.run()` function will handle incoming Slack+ events on the main thread, while the separate thread will handle processing+ the messages from the queue.+ """+ msg_queue = queue.Queue()++ agent = BotAgent()++ thread = threading.Thread(+ target=start_async_queue_processor, args=(agent, msg_queue)+ )+ thread.start()++ # Initialize the SlackBot with the message queue.+ slack_bot = SlackBot(msg_queue=msg_queue)+ slack_bot.run(3000)++ thread.join()diff --git a/examples/test/bots/test_slack_app.py b/examples/test/bots/test_slack_app.pynew file mode 100644index 000000000..41ae0a63b--- /dev/null+++ b/examples/test/bots/test_slack_app.py@@ -0,0 +1,148 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+import unittest+from unittest.mock import AsyncMock, MagicMock, patch++from camel.bots import SlackApp+++class TestSlackApp(unittest.TestCase):+ def setUp(self):+ # Temporarily set environment variables for testing+ os.environ["SLACK_TOKEN"] = "fake_token"+ os.environ["SLACK_SCOPES"] = "channels:read,chat:write"+ os.environ["SLACK_SIGNING_SECRET"] = "fake_signing_secret"+ os.environ["SLACK_CLIENT_ID"] = "fake_client_id"+ os.environ["SLACK_CLIENT_SECRET"] = "fake_client_secret"++ def tearDown(self):+ # Clean up environment variables after the tests, if they exist+ for var in [+ "SLACK_TOKEN",+ "SLACK_SCOPES",+ "SLACK_SIGNING_SECRET",+ "SLACK_CLIENT_ID",+ "SLACK_CLIENT_SECRET",+ ]:+ if var in os.environ:+ del os.environ[var]++ @patch('slack_bolt.app.async_app.AsyncApp')+ def test_init_without_token_raises_error(self, mock_async_app):+ # Temporarily clear SLACK_TOKEN, SLACK_SCOPES and SLACK_SIGNING_SECRET+ # to test the ValueError+ for var in [+ "SLACK_TOKEN",+ "SLACK_SCOPES",+ "SLACK_SIGNING_SECRET",+ ]:+ if var in os.environ:+ del os.environ[var]++ with self.assertRaises(ValueError):+ SlackApp()++ @patch('slack_bolt.app.async_app.AsyncApp')+ def test_init_with_token(self, mock_async_app):+ app = SlackApp(+ token="fake_token1",+ scopes="channels:read,chat:write,commands",+ signing_secret="fake_signing_secret1",+ client_id="fake_client_id1",+ client_secret="fake_client_secret1",+ )+ # Assert correct initialization of SlackApp attributes+ self.assertEqual(app.token, "fake_token1")+ self.assertEqual(app.scopes, "channels:read,chat:write,commands")+ self.assertEqual(app.signing_secret, "fake_signing_secret1")+ self.assertEqual(app.client_id, "fake_client_id1")+ self.assertEqual(app.client_secret, "fake_client_secret1")++ @patch('slack_bolt.app.async_app.AsyncApp')+ def test_setup_handlers(self, mock_async_app):+ mock_app = mock_async_app.return_value+ app = SlackApp()+ app.setup_handlers()++ # Ensure event handlers are properly registered+ mock_app.event.assert_any_call("app_mention")+ mock_app.event.assert_any_call("message")++ @patch('slack_bolt.app.async_app.AsyncApp')+ async def test_handle_request(self, mock_async_app):+ app = SlackApp()+ mock_handler = AsyncMock()+ app._handler = mock_handler++ mock_request = MagicMock() # Use MagicMock for the request+ await app.handle_request(mock_request)++ # Verify the request was handled once+ mock_handler.handle.assert_called_once_with(mock_request)++ @patch('slack_bolt.app.async_app.AsyncApp')+ async def test_app_mention(self, mock_async_app):+ app = SlackApp()++ context = MagicMock()+ client = MagicMock()+ event = {"text": "Hello, SlackApp!"}+ body = {"body": "some body content"}+ say = AsyncMock()++ await app.app_mention(context, client, event, body, say)++ # Test that the event was logged and say() was called+ self.assertIn("app_mention", context.method_calls[0][0])+ self.assertIn("app_mention", client.method_calls[0][0])+ say.assert_called_once()++ @patch('slack_bolt.app.async_app.AsyncApp')+ async def test_on_message(self, mock_async_app):+ app = SlackApp()++ context = MagicMock()+ client = MagicMock()+ event = {"text": "This is a test message"}+ body = {"body": "some body content"}+ say = AsyncMock()++ await app.on_message(context, client, event, body, say)++ # Check that context.ack() and say() were called+ context.ack.assert_called_once()+ say.assert_called_once()++ @patch('slack_bolt.app.async_app.AsyncApp')+ async def test_mention_me(self, mock_async_app):+ app = SlackApp()++ context = MagicMock()+ context.bot_user_id = "U12345"+ body = MagicMock()+ body.event.text = "Hello <@U12345>"++ # Test when the bot is mentioned+ is_mentioned = app.mention_me(context, body)+ self.assertTrue(is_mentioned)++ # Test when the bot is not mentioned+ body.event.text = "Hello there!"+ is_not_mentioned = app.mention_me(context, body)+ self.assertFalse(is_not_mentioned)+++if __name__ == '__main__':+ unittest.main()diff --git a/poetry.lock b/poetry.lockindex a22801707..d98ae7556 100644--- a/poetry.lock+++ b/poetry.lock@@ -7564,6 +7564,20 @@ files = [{file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"},]+[[package]]+name = "slack-bolt"+version = "1.21.0"+description = "The Bolt Framework for Python"+optional = true+python-versions = ">=3.6"+files = [+ {file = "slack_bolt-1.21.0-py2.py3-none-any.whl", hash = "sha256:12f99fe26f7b1b762955053ebce303d4e2394cee1e3058e35a270185f2a31e40"},+ {file = "slack_bolt-1.21.0.tar.gz", hash = "sha256:438bee336440e78222064416c75e9353ee57e4c0377f2f41bda2212acad2387a"},+]++[package.dependencies]+slack-sdk = ">=3.33.1,<4"+[[package]]name = "slack-sdk"version = "3.33.1"@@ -9581,7 +9595,7 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools",type = ["pytest-mypy"][extras]-all = ["PyMuPDF", "accelerate", "agentops", "anthropic", "arxiv", "arxiv2text", "asknews", "azure-storage-blob", "beautifulsoup4", "botocore", "cohere", "datasets", "diffusers", "discord.py", "docker", "docx2txt", "duckduckgo-search", "firecrawl-py", "google-cloud-storage", "google-generativeai", "googlemaps", "imageio", "ipykernel", "jupyter_client", "litellm", "mistralai", "nebula3-python", "neo4j", "newspaper3k", "nltk", "openapi-spec-validator", "opencv-python", "pillow", "prance", "praw", "pyTelegramBotAPI", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "redis", "reka-api", "requests_oauthlib", "scholarly", "sentence-transformers", "sentencepiece", "slack-sdk", "soundfile", "textblob", "torch", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]+all = ["PyMuPDF", "accelerate", "agentops", "anthropic", "arxiv", "arxiv2text", "asknews", "azure-storage-blob", "beautifulsoup4", "botocore", "cohere", "datasets", "diffusers", "discord.py", "docker", "docx2txt", "duckduckgo-search", "firecrawl-py", "google-cloud-storage", "google-generativeai", "googlemaps", "imageio", "ipykernel", "jupyter_client", "litellm", "mistralai", "nebula3-python", "neo4j", "newspaper3k", "nltk", "openapi-spec-validator", "opencv-python", "pillow", "prance", "praw", "pyTelegramBotAPI", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "redis", "reka-api", "requests_oauthlib", "scholarly", "sentence-transformers", "sentencepiece", "slack-bolt", "slack-sdk", "soundfile", "textblob", "torch", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]encoders = ["sentence-transformers"]graph-storages = ["nebula3-python", "neo4j"]huggingface-agent = ["accelerate", "datasets", "diffusers", "opencv-python", "sentencepiece", "soundfile", "torch", "torch", "transformers"]@@ -9592,10 +9606,10 @@ rag = ["cohere", "nebula3-python", "neo4j", "pymilvus", "qdrant-client", "rank-bretrievers = ["cohere", "rank-bm25"]search-tools = ["duckduckgo-search", "wikipedia", "wolframalpha"]test = ["mock", "pytest", "pytest-asyncio"]-tools = ["PyMuPDF", "agentops", "arxiv", "arxiv2text", "asknews", "beautifulsoup4", "discord.py", "docker", "docx2txt", "duckduckgo-search", "firecrawl-py", "googlemaps", "imageio", "ipykernel", "jupyter_client", "newspaper3k", "nltk", "openapi-spec-validator", "pillow", "prance", "praw", "pyTelegramBotAPI", "pydub", "pygithub", "pyowm", "requests_oauthlib", "scholarly", "slack-sdk", "textblob", "unstructured", "wikipedia", "wolframalpha"]+tools = ["PyMuPDF", "agentops", "arxiv", "arxiv2text", "asknews", "beautifulsoup4", "discord.py", "docker", "docx2txt", "duckduckgo-search", "firecrawl-py", "googlemaps", "imageio", "ipykernel", "jupyter_client", "newspaper3k", "nltk", "openapi-spec-validator", "pillow", "prance", "praw", "pyTelegramBotAPI", "pydub", "pygithub", "pyowm", "requests_oauthlib", "scholarly", "slack-bolt", "slack-sdk", "textblob", "unstructured", "wikipedia", "wolframalpha"]vector-databases = ["pymilvus", "qdrant-client"][metadata]lock-version = "2.0"python-versions = ">=3.10.0,<=3.13"-content-hash = "1a7f66248a4365fa2ba96812080f5d0e2970ddb0fd946665c8f9995f355116f8"+content-hash = "8b54fe2bc824b437fd864cecfc1449ed50bf1df962ff6d9e291e13ce3eff3fc9"diff --git a/pyproject.toml b/pyproject.tomlindex d5259e18f..80117d1ef 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -78,6 +78,7 @@ openapi-spec-validator = { version = "^0.7.1", optional = true }unstructured = { extras = ["all-docs"], version = "^0.10", optional = true }firecrawl-py = { version = "^1.0.0", optional = true }slack-sdk = { version = "^3.27.2", optional = true }+slack-bolt = {version = "^1.20.1", optional = true}pydub = { version = "^0.25.1", optional = true }pygithub = { version = "^2.3.0", optional = true }imageio = { extras = ["pyav"], version = "^2.34.2" }@@ -185,6 +186,7 @@ tools = ["imageio","pillow","slack-sdk",+ "slack-bolt","pydub","pygithub","pyTelegramBotAPI",@@ -257,6 +259,7 @@ all = ["arxiv2text","imageio","slack-sdk",+ "slack-bolt","pydub","pillow","docker",@@ -414,6 +417,7 @@ module = ["pymilvus","pillow","slack-sdk",+ "slack-bolt","pydub","pygithub","litellm",