Skip to content

Commit

Permalink
Merge pull request #97 from alan-turing-institute/chat_history
Browse files Browse the repository at this point in the history
Add ability to clear chat history
  • Loading branch information
rchan26 authored Sep 22, 2023
2 parents fc5ca67 + 9d28210 commit a359f88
Showing 1 changed file with 85 additions and 40 deletions.
125 changes: 85 additions & 40 deletions slack_bot/slack_bot/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.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:
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()
Expand Down

0 comments on commit a359f88

Please sign in to comment.