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 hashlib |
| #2 | import logging |
| #3 | import os |
| #4 | import ssl |
| #5 | from typing import Any, Optional |
| #6 | |
| #7 | import certifi |
| #8 | |
| #9 | from embedchain.loaders.base_loader import BaseLoader |
| #10 | from embedchain.utils.misc import clean_string |
| #11 | |
| #12 | SLACK_API_BASE_URL = "https://www.slack.com/api/" |
| #13 | |
| #14 | logger = logging.getLogger(__name__) |
| #15 | |
| #16 | |
| #17 | class SlackLoader(BaseLoader): |
| #18 | def __init__(self, config: Optional[dict[str, Any]] = None): |
| #19 | super().__init__() |
| #20 | |
| #21 | self.config = config if config else {} |
| #22 | |
| #23 | if "base_url" not in self.config: |
| #24 | self.config["base_url"] = SLACK_API_BASE_URL |
| #25 | |
| #26 | self.client = None |
| #27 | self._setup_loader(self.config) |
| #28 | |
| #29 | def _setup_loader(self, config: dict[str, Any]): |
| #30 | try: |
| #31 | from slack_sdk import WebClient |
| #32 | except ImportError as e: |
| #33 | raise ImportError( |
| #34 | "Slack loader requires extra dependencies. \ |
| #35 | Install with `pip install --upgrade embedchain[slack]`" |
| #36 | ) from e |
| #37 | |
| #38 | if os.getenv("SLACK_USER_TOKEN") is None: |
| #39 | raise ValueError( |
| #40 | "SLACK_USER_TOKEN environment variables not provided. Check `https://docs.embedchain.ai/data-sources/slack` to learn more." # noqa:E501 |
| #41 | ) |
| #42 | |
| #43 | logger.info(f"Creating Slack Loader with config: {config}") |
| #44 | # get slack client config params |
| #45 | slack_bot_token = os.getenv("SLACK_USER_TOKEN") |
| #46 | ssl_cert = ssl.create_default_context(cafile=certifi.where()) |
| #47 | base_url = config.get("base_url", SLACK_API_BASE_URL) |
| #48 | headers = config.get("headers") |
| #49 | # for Org-Wide App |
| #50 | team_id = config.get("team_id") |
| #51 | |
| #52 | self.client = WebClient( |
| #53 | token=slack_bot_token, |
| #54 | base_url=base_url, |
| #55 | ssl=ssl_cert, |
| #56 | headers=headers, |
| #57 | team_id=team_id, |
| #58 | ) |
| #59 | logger.info("Slack Loader setup successful!") |
| #60 | |
| #61 | @staticmethod |
| #62 | def _check_query(query): |
| #63 | if not isinstance(query, str): |
| #64 | raise ValueError( |
| #65 | f"Invalid query passed to Slack loader, found: {query}. Check `https://docs.embedchain.ai/data-sources/slack` to learn more." # noqa:E501 |
| #66 | ) |
| #67 | |
| #68 | def load_data(self, query): |
| #69 | self._check_query(query) |
| #70 | try: |
| #71 | data = [] |
| #72 | data_content = [] |
| #73 | |
| #74 | logger.info(f"Searching slack conversations for query: {query}") |
| #75 | results = self.client.search_messages( |
| #76 | query=query, |
| #77 | sort="timestamp", |
| #78 | sort_dir="desc", |
| #79 | count=self.config.get("count", 100), |
| #80 | ) |
| #81 | |
| #82 | messages = results.get("messages") |
| #83 | num_message = len(messages) |
| #84 | logger.info(f"Found {num_message} messages for query: {query}") |
| #85 | |
| #86 | matches = messages.get("matches", []) |
| #87 | for message in matches: |
| #88 | url = message.get("permalink") |
| #89 | text = message.get("text") |
| #90 | content = clean_string(text) |
| #91 | |
| #92 | message_meta_data_keys = ["iid", "team", "ts", "type", "user", "username"] |
| #93 | metadata = {} |
| #94 | for key in message.keys(): |
| #95 | if key in message_meta_data_keys: |
| #96 | metadata[key] = message.get(key) |
| #97 | metadata.update({"url": url}) |
| #98 | |
| #99 | data.append( |
| #100 | { |
| #101 | "content": content, |
| #102 | "meta_data": metadata, |
| #103 | } |
| #104 | ) |
| #105 | data_content.append(content) |
| #106 | doc_id = hashlib.md5((query + ", ".join(data_content)).encode()).hexdigest() |
| #107 | return { |
| #108 | "doc_id": doc_id, |
| #109 | "data": data, |
| #110 | } |
| #111 | except Exception as e: |
| #112 | logger.warning(f"Error in loading slack data: {e}") |
| #113 | raise ValueError( |
| #114 | f"Error in loading slack data: {e}. Check `https://docs.embedchain.ai/data-sources/slack` to learn more." # noqa:E501 |
| #115 | ) from e |
| #116 |