-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Update slack_bot #1353
feat: Update slack_bot #1353
Changes from 5 commits
b395261
e7c4780
20eec2d
6391bb4
632709c
7584a8d
a993f45
ccf824a
bd62499
17d62a3
c33fc14
9150fdc
cfe9030
fbfccc4
afc26bb
5aa3043
09f1120
c60c3cf
9ba76fe
8ed9442
6e80581
2fe501d
f12547b
759ec6e
c18e0bf
c74b5bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= | ||
import logging | ||
import os | ||
from typing import TYPE_CHECKING, Any, Dict, Optional | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union | ||
|
||
from slack_sdk.oauth.installation_store.async_installation_store import ( | ||
AsyncInstallationStore, | ||
|
@@ -61,18 +61,21 @@ class SlackApp: | |
def __init__( | ||
self, | ||
token: Optional[str] = None, | ||
app_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, | ||
socket_mode: bool = True, | ||
) -> 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. | ||
app_token (Optional[str]): The Slack app token. | ||
scopes (Optional[str]): The scopes for Slack app permissions. | ||
signing_secret (Optional[str]): The signing secret for verifying | ||
requests. | ||
|
@@ -82,7 +85,13 @@ def __init__( | |
(default is "/slack/oauth_redirect"). | ||
installation_store (Optional[AsyncInstallationStore]): An optional | ||
installation store for OAuth installations. | ||
socket_mode (bool): A flag to enable socket mode for the Slack app, | ||
defaults to True. if False, you must set request URL in the | ||
slack website. | ||
""" | ||
from slack_bolt.adapter.socket_mode.async_handler import ( | ||
AsyncSocketModeHandler, | ||
) | ||
from slack_bolt.adapter.starlette.async_handler import ( | ||
AsyncSlackRequestHandler, | ||
) | ||
|
@@ -100,38 +109,57 @@ def __init__( | |
self.client_secret: Optional[str] = client_secret or os.getenv( | ||
"SLACK_CLIENT_SECRET" | ||
) | ||
self.app_token: Optional[str] = app_token or os.getenv( | ||
"SLACK_APP_TOKEN" | ||
) | ||
self.custom_handler: Optional[Callable[[str], str]] = None | ||
self.socket_mode: bool = socket_mode | ||
self._handler: Optional[ | ||
Union[AsyncSlackRequestHandler, AsyncSocketModeHandler] | ||
] = None | ||
if not self.socket_mode: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @Asher-hss , the slack app contains OAuth verification and Message communication. In socket mode, the OAuth process also needs to be allowed. I don't think the verification here is appropriate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose here is:
|
||
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) | ||
else: | ||
if not self.app_token: | ||
raise ValueError( | ||
"`SLACK_APP_TOKEN` environment variable must be set. " | ||
"Get it here: `https://api.slack.com/apps`." | ||
) | ||
|
||
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: | ||
|
@@ -144,6 +172,27 @@ def setup_handlers(self) -> None: | |
self._app.event("app_mention")(self.app_mention) | ||
self._app.event("message")(self.on_message) | ||
|
||
async def start(self) -> None: | ||
r"""Starts the Slack Bolt app asynchronously.""" | ||
from slack_bolt.adapter.socket_mode.async_handler import ( | ||
AsyncSocketModeHandler, | ||
) | ||
from slack_bolt.adapter.starlette.async_handler import ( | ||
AsyncSlackRequestHandler, | ||
) | ||
|
||
if not self._handler: | ||
self._handler = AsyncSocketModeHandler( | ||
self._app, app_token=self.app_token | ||
) | ||
await self._handler.start_async() | ||
elif isinstance(self._handler, AsyncSlackRequestHandler): | ||
logger.info( | ||
"AsyncSlackRequestHandler does not support " | ||
"start_async.Ensure it is integrated with " | ||
"a web framework." | ||
) | ||
|
||
def run( | ||
self, | ||
port: int = 3000, | ||
|
@@ -175,7 +224,24 @@ async def handle_request( | |
Returns: | ||
The response generated by the Slack Bolt handler. | ||
""" | ||
return await self._handler.handle(request) | ||
from slack_bolt.adapter.socket_mode.async_handler import ( | ||
AsyncSocketModeHandler, | ||
) | ||
|
||
if self._handler is None: | ||
logger.error("Handler is not initialized.") | ||
return responses.Response( | ||
status_code=500, content="Handler not initialized." | ||
) | ||
if isinstance(self._handler, AsyncSocketModeHandler): | ||
logger.info("Skipping processing for AsyncSocketModeHandler.") | ||
return responses.Response( | ||
status_code=200, content="Socket mode request skipped." | ||
) | ||
response = await self._handler.handle(request) | ||
if response is None: | ||
return responses.Response(status_code=400, content="Bad Request") | ||
return response | ||
|
||
async def app_mention( | ||
self, | ||
|
@@ -204,6 +270,9 @@ async def app_mention( | |
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}") | ||
if self.custom_handler: | ||
response = self.custom_handler(event_profile.text) | ||
await say(response) | ||
|
||
async def on_message( | ||
self, | ||
|
@@ -236,6 +305,9 @@ async def on_message( | |
logger.info(f"on_message, say: {say}") | ||
|
||
logger.info(f"Received message: {event_profile.text}") | ||
if self.custom_handler: | ||
response = self.custom_handler(event_profile.text) | ||
await say(response) | ||
|
||
def mention_me( | ||
self, context: "AsyncBoltContext", body: SlackEventBody | ||
|
@@ -253,3 +325,12 @@ def mention_me( | |
bot_user_id = context.bot_user_id | ||
mention = f"<@{bot_user_id}>" | ||
return mention in message | ||
|
||
def set_custom_handler(self, handler: Callable[[str], str]) -> None: | ||
"""Sets a custom message handler for the Slack app. | ||
|
||
Args: | ||
handler (Callable[[str], str]): A custom message handler that | ||
takes a message string as input and returns a response string. | ||
""" | ||
self.custom_handler = handler |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# ========= Copyright 2023-2024 @ 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-2024 @ CAMEL-AI.org. All Rights Reserved. ========= | ||
|
||
import asyncio | ||
|
||
from camel.agents import ChatAgent | ||
from camel.bots.slack.slack_app import SlackApp | ||
from camel.configs import ChatGPTConfig | ||
from camel.models import ModelFactory | ||
from camel.types import ModelPlatformType, ModelType | ||
|
||
slack_bot = SlackApp( | ||
token="please input your slack token", | ||
app_token="please input your slack app token", | ||
socket_mode=True, | ||
) | ||
|
||
o1_model = ModelFactory.create( | ||
model_platform=ModelPlatformType.OPENAI, | ||
model_type=ModelType.GPT_4O, | ||
model_config_dict=ChatGPTConfig(temperature=0.0).as_dict(), | ||
) | ||
agent = ChatAgent( | ||
system_message="you are a helpful assistant", | ||
message_window_size=10, | ||
model=o1_model, | ||
) | ||
|
||
|
||
def custom_handler(message: str) -> str: | ||
response = agent.step(message) | ||
return response.msg.content | ||
|
||
|
||
slack_bot.set_custom_handler(custom_handler) | ||
|
||
asyncio.run(slack_bot.start()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Asher-hss , when we create a new slack app, socket mode is turned off by default. Let's set socket_mode to
False
to maintain consistency with the official configuration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure