app.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import logging
  2. import os
  3. import re
  4. import requests
  5. from dotenv import load_dotenv
  6. from slack_bolt import App as SlackApp
  7. from slack_bolt.adapter.socket_mode import SocketModeHandler
  8. load_dotenv(".env")
  9. def remove_mentions(message):
  10. mention_pattern = re.compile(r"<@[^>]+>")
  11. cleaned_message = re.sub(mention_pattern, "", message)
  12. cleaned_message.strip()
  13. return cleaned_message
  14. class SlackBotApp:
  15. def __init__(self) -> None:
  16. logging.info("Slack Bot using Embedchain!")
  17. def add(self, _):
  18. raise ValueError("Add is not implemented yet")
  19. def query(self, query, citations: bool = False):
  20. url = os.environ["EC_APP_URL"] + "/query"
  21. payload = {
  22. "question": query,
  23. "citations": citations,
  24. }
  25. try:
  26. response = requests.request("POST", url, json=payload)
  27. try:
  28. response = response.json()
  29. except Exception:
  30. logging.error(f"Failed to parse response: {response}")
  31. response = {}
  32. return response
  33. except Exception:
  34. logging.exception(f"Failed to query {query}.")
  35. response = "An error occurred. Please try again!"
  36. return response
  37. SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"]
  38. SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
  39. slack_app = SlackApp(token=SLACK_BOT_TOKEN)
  40. slack_bot = SlackBotApp()
  41. @slack_app.event("message")
  42. def app_message_handler(message, say):
  43. pass
  44. @slack_app.event("app_mention")
  45. def app_mention_handler(body, say, client):
  46. # Get the timestamp of the original message to reply in the thread
  47. if "thread_ts" in body["event"]:
  48. # thread is already created
  49. thread_ts = body["event"]["thread_ts"]
  50. say(
  51. text="🧵 Currently, we don't support answering questions in threads. Could you please send your message in the channel for a swift response? Appreciate your understanding! 🚀", # noqa: E501
  52. thread_ts=thread_ts,
  53. )
  54. return
  55. thread_ts = body["event"]["ts"]
  56. say(
  57. text="🎭 Putting on my thinking cap, brb with an epic response!",
  58. thread_ts=thread_ts,
  59. )
  60. query = body["event"]["text"]
  61. question = remove_mentions(query)
  62. print("Asking question: ", question)
  63. response = slack_bot.query(question, citations=True)
  64. default_answer = "Sorry, I don't know the answer to that question. Please refer to the documentation.\nhttps://nextjs.org/docs" # noqa: E501
  65. answer = response.get("answer", default_answer)
  66. contexts = response.get("contexts", [])
  67. if contexts:
  68. sources = list(set(map(lambda x: x[1]["url"], contexts)))
  69. answer += "\n\n*Sources*:\n"
  70. for i, source in enumerate(sources):
  71. answer += f"- {source}\n"
  72. print("Sending answer: ", answer)
  73. result = say(text=answer, thread_ts=thread_ts)
  74. if result["ok"]:
  75. channel = result["channel"]
  76. timestamp = result["ts"]
  77. client.reactions_add(
  78. channel=channel,
  79. name="open_mouth",
  80. timestamp=timestamp,
  81. )
  82. client.reactions_add(
  83. channel=channel,
  84. name="thumbsup",
  85. timestamp=timestamp,
  86. )
  87. client.reactions_add(
  88. channel=channel,
  89. name="heart",
  90. timestamp=timestamp,
  91. )
  92. client.reactions_add(
  93. channel=channel,
  94. name="thumbsdown",
  95. timestamp=timestamp,
  96. )
  97. def start_bot():
  98. slack_socket_mode_handler = SocketModeHandler(slack_app, SLACK_APP_TOKEN)
  99. slack_socket_mode_handler.start()
  100. if __name__ == "__main__":
  101. start_bot()