repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import logging |
| #2 | import os |
| #3 | import re |
| #4 | |
| #5 | import requests |
| #6 | from dotenv import load_dotenv |
| #7 | from slack_bolt import App as SlackApp |
| #8 | from slack_bolt.adapter.socket_mode import SocketModeHandler |
| #9 | |
| #10 | load_dotenv(".env") |
| #11 | |
| #12 | logger = logging.getLogger(__name__) |
| #13 | |
| #14 | |
| #15 | def remove_mentions(message): |
| #16 | mention_pattern = re.compile(r"<@[^>]+>") |
| #17 | cleaned_message = re.sub(mention_pattern, "", message) |
| #18 | cleaned_message.strip() |
| #19 | return cleaned_message |
| #20 | |
| #21 | |
| #22 | class SlackBotApp: |
| #23 | def __init__(self) -> None: |
| #24 | logger.info("Slack Bot using Embedchain!") |
| #25 | |
| #26 | def add(self, _): |
| #27 | raise ValueError("Add is not implemented yet") |
| #28 | |
| #29 | def query(self, query, citations: bool = False): |
| #30 | url = os.environ["EC_APP_URL"] + "/query" |
| #31 | payload = { |
| #32 | "question": query, |
| #33 | "citations": citations, |
| #34 | } |
| #35 | try: |
| #36 | response = requests.request("POST", url, json=payload) |
| #37 | try: |
| #38 | response = response.json() |
| #39 | except Exception: |
| #40 | logger.error(f"Failed to parse response: {response}") |
| #41 | response = {} |
| #42 | return response |
| #43 | except Exception: |
| #44 | logger.exception(f"Failed to query {query}.") |
| #45 | response = "An error occurred. Please try again!" |
| #46 | return response |
| #47 | |
| #48 | |
| #49 | SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"] |
| #50 | SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"] |
| #51 | |
| #52 | slack_app = SlackApp(token=SLACK_BOT_TOKEN) |
| #53 | slack_bot = SlackBotApp() |
| #54 | |
| #55 | |
| #56 | @slack_app.event("message") |
| #57 | def app_message_handler(message, say): |
| #58 | pass |
| #59 | |
| #60 | |
| #61 | @slack_app.event("app_mention") |
| #62 | def app_mention_handler(body, say, client): |
| #63 | # Get the timestamp of the original message to reply in the thread |
| #64 | if "thread_ts" in body["event"]: |
| #65 | # thread is already created |
| #66 | thread_ts = body["event"]["thread_ts"] |
| #67 | say( |
| #68 | 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 |
| #69 | thread_ts=thread_ts, |
| #70 | ) |
| #71 | return |
| #72 | |
| #73 | thread_ts = body["event"]["ts"] |
| #74 | say( |
| #75 | text="🎭 Putting on my thinking cap, brb with an epic response!", |
| #76 | thread_ts=thread_ts, |
| #77 | ) |
| #78 | query = body["event"]["text"] |
| #79 | question = remove_mentions(query) |
| #80 | print("Asking question: ", question) |
| #81 | response = slack_bot.query(question, citations=True) |
| #82 | default_answer = "Sorry, I don't know the answer to that question. Please refer to the documentation.\nhttps://nextjs.org/docs" # noqa: E501 |
| #83 | answer = response.get("answer", default_answer) |
| #84 | contexts = response.get("contexts", []) |
| #85 | if contexts: |
| #86 | sources = list(set(map(lambda x: x[1]["url"], contexts))) |
| #87 | answer += "\n\n*Sources*:\n" |
| #88 | for i, source in enumerate(sources): |
| #89 | answer += f"- {source}\n" |
| #90 | |
| #91 | print("Sending answer: ", answer) |
| #92 | result = say(text=answer, thread_ts=thread_ts) |
| #93 | if result["ok"]: |
| #94 | channel = result["channel"] |
| #95 | timestamp = result["ts"] |
| #96 | client.reactions_add( |
| #97 | channel=channel, |
| #98 | name="open_mouth", |
| #99 | timestamp=timestamp, |
| #100 | ) |
| #101 | client.reactions_add( |
| #102 | channel=channel, |
| #103 | name="thumbsup", |
| #104 | timestamp=timestamp, |
| #105 | ) |
| #106 | client.reactions_add( |
| #107 | channel=channel, |
| #108 | name="heart", |
| #109 | timestamp=timestamp, |
| #110 | ) |
| #111 | client.reactions_add( |
| #112 | channel=channel, |
| #113 | name="thumbsdown", |
| #114 | timestamp=timestamp, |
| #115 | ) |
| #116 | |
| #117 | |
| #118 | def start_bot(): |
| #119 | slack_socket_mode_handler = SocketModeHandler(slack_app, SLACK_APP_TOKEN) |
| #120 | slack_socket_mode_handler.start() |
| #121 | |
| #122 | |
| #123 | if __name__ == "__main__": |
| #124 | start_bot() |
| #125 |