From 1824dd8eb8ea04f4c582e44ef4e80e56ce74c9b2 Mon Sep 17 00:00:00 2001 From: Rosie Wood Date: Wed, 20 Sep 2023 16:30:31 +0100 Subject: [PATCH 1/2] add ability to clear chat history --- slack_bot/slack_bot/bot/bot.py | 125 ++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 40 deletions(-) diff --git a/slack_bot/slack_bot/bot/bot.py b/slack_bot/slack_bot/bot/bot.py index e4556e41..a7de810f 100644 --- a/slack_bot/slack_bot/bot/bot.py +++ b/slack_bot/slack_bot/bot/bot.py @@ -18,49 +18,94 @@ def __init__(self, model: ResponseModel) -> None: task = asyncio.create_task(self.worker(self.queue)) async def __call__(self, client: SocketModeClient, req: SocketModeRequest) -> None: - if req.type != "events_api": + if req.type == "events_api": + # Acknowledge the request + logging.info("Received an events_api request") + response = SocketModeResponse(envelope_id=req.envelope_id) + await client.send_socket_mode_response(response) + + try: + # Extract event from payload + event = req.payload["event"] + + # Ignore messages from bots + if event.get("bot_id") is not None: + logging.info("Ignoring an event triggered by a bot.") + return + if event.get("hidden") is not None: + logging.info("Ignoring hidden message.") + return + + # add clock emoji + logging.info("Reacting with clock emoji.") + await client.web_client.reactions_add( + name="clock2", + channel=event["channel"], + timestamp=event["ts"], + ) + + self.queue.put_nowait((client, event)) + logging.info( + f"There are currently {self.queue.qsize()} items in the queue." + ) + + except KeyError as exc: + logging.warning( + f"Attempted to access key that does not exist.\n{str(exc)}" + ) + + except Exception as exc: + logging.error( + f"Something went wrong in processing a Slack request.\nPayload: {req.payload}.\n{str(exc)}" + ) + raise + + elif req.type == "slash_commands": + # Acknowledge the request + logging.info("Received an slash_commands request") + response = SocketModeResponse(envelope_id=req.envelope_id) + await client.send_socket_mode_response(response) + + try: + # Extract command, user, etc from payload + command = req.payload["command"] + user_id = req.payload["user_id"] + + if command == "/clear_history": + if self.model.mode == "chat": + logging.info(f"Clearing {user_id}'s history") + if self.model.chat_engine.get(user_id) is not None: + self.model.chat_engine[user_id].reset() + message = "All done! Chat history is cleared." + logging.info(f"Done clearing {user_id}'s history") + else: + logging.info(f"{user_id} has no history to be cleared.") + message = "No history to clear" + else: + logging.info("Using query engine, no history to be cleared.") + message = "No history to clear" + + logging.info("Posting clear_history message.") + await client.web_client.chat_postMessage( + channel=req.payload["channel_id"], + text=message, + ) + + except KeyError as exc: + logging.warning( + f"Attempted to access key that does not exist.\n{str(exc)}" + ) + + except Exception as exc: + logging.error( + f"Something went wrong in processing a Slack request.\nPayload: {req.payload}.\n{str(exc)}" + ) + raise + + else: logging.info(f"Received unexpected request of type '{req.type}'") return - # Acknowledge the request - logging.info("Received an events_api request") - response = SocketModeResponse(envelope_id=req.envelope_id) - await client.send_socket_mode_response(response) - - try: - # Extract event from payload - event = req.payload["event"] - - # Ignore messages from bots - if event.get("bot_id") is not None: - logging.info("Ignoring an event triggered by a bot.") - return - if event.get("hidden") is not None: - logging.info("Ignoring hidden message.") - return - - # add clock emoji - logging.info("Reacting with clock emoji.") - await client.web_client.reactions_add( - name="clock2", - channel=event["channel"], - timestamp=event["ts"], - ) - - self.queue.put_nowait((client, event)) - logging.info( - f"There are currently {self.queue.qsize()} items in the queue." - ) - - except KeyError as exc: - logging.warning(f"Attempted to access key that does not exist.\n{str(exc)}") - - except Exception as exc: - logging.error( - f"Something went wrong in processing a Slack request.\nPayload: {req.payload}.\n{str(exc)}" - ) - raise - async def worker(self, queue): while True: (client, event) = await queue.get() From 9d28210e0c31e9c69d169960afdb01a6c9d5855e Mon Sep 17 00:00:00 2001 From: rchan Date: Thu, 21 Sep 2023 09:10:00 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20need=20to=20have=20different=20?= =?UTF-8?q?clear=5Fhistory=20commands=20for=20different=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slack_bot/slack_bot/bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack_bot/slack_bot/bot/bot.py b/slack_bot/slack_bot/bot/bot.py index a7de810f..127b28c7 100644 --- a/slack_bot/slack_bot/bot/bot.py +++ b/slack_bot/slack_bot/bot/bot.py @@ -71,7 +71,7 @@ async def __call__(self, client: SocketModeClient, req: SocketModeRequest) -> No command = req.payload["command"] user_id = req.payload["user_id"] - if command == "/clear_history": + if command.startswith("/clear_history"): if self.model.mode == "chat": logging.info(f"Clearing {user_id}'s history") if self.model.chat_engine.get(user_id) is not None: