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 argparse |
| #2 | import logging |
| #3 | import os |
| #4 | import signal |
| #5 | import sys |
| #6 | |
| #7 | from embedchain import App |
| #8 | from embedchain.helpers.json_serializable import register_deserializable |
| #9 | |
| #10 | from .base import BaseBot |
| #11 | |
| #12 | try: |
| #13 | from flask import Flask, request |
| #14 | from slack_sdk import WebClient |
| #15 | except ModuleNotFoundError: |
| #16 | raise ModuleNotFoundError( |
| #17 | "The required dependencies for Slack are not installed." |
| #18 | "Please install with `pip install slack-sdk==3.21.3 flask==2.3.3`" |
| #19 | ) from None |
| #20 | |
| #21 | |
| #22 | logger = logging.getLogger(__name__) |
| #23 | |
| #24 | SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN") |
| #25 | |
| #26 | |
| #27 | @register_deserializable |
| #28 | class SlackBot(BaseBot): |
| #29 | def __init__(self): |
| #30 | self.client = WebClient(token=SLACK_BOT_TOKEN) |
| #31 | self.chat_bot = App() |
| #32 | self.recent_message = {"ts": 0, "channel": ""} |
| #33 | super().__init__() |
| #34 | |
| #35 | def handle_message(self, event_data): |
| #36 | message = event_data.get("event") |
| #37 | if message and "text" in message and message.get("subtype") != "bot_message": |
| #38 | text: str = message["text"] |
| #39 | if float(message.get("ts")) > float(self.recent_message["ts"]): |
| #40 | self.recent_message["ts"] = message["ts"] |
| #41 | self.recent_message["channel"] = message["channel"] |
| #42 | if text.startswith("query"): |
| #43 | _, question = text.split(" ", 1) |
| #44 | try: |
| #45 | response = self.chat_bot.chat(question) |
| #46 | self.send_slack_message(message["channel"], response) |
| #47 | logger.info("Query answered successfully!") |
| #48 | except Exception as e: |
| #49 | self.send_slack_message(message["channel"], "An error occurred. Please try again!") |
| #50 | logger.error("Error occurred during 'query' command:", e) |
| #51 | elif text.startswith("add"): |
| #52 | _, data_type, url_or_text = text.split(" ", 2) |
| #53 | if url_or_text.startswith("<") and url_or_text.endswith(">"): |
| #54 | url_or_text = url_or_text[1:-1] |
| #55 | try: |
| #56 | self.chat_bot.add(url_or_text, data_type) |
| #57 | self.send_slack_message(message["channel"], f"Added {data_type} : {url_or_text}") |
| #58 | except ValueError as e: |
| #59 | self.send_slack_message(message["channel"], f"Error: {str(e)}") |
| #60 | logger.error("Error occurred during 'add' command:", e) |
| #61 | except Exception as e: |
| #62 | self.send_slack_message(message["channel"], f"Failed to add {data_type} : {url_or_text}") |
| #63 | logger.error("Error occurred during 'add' command:", e) |
| #64 | |
| #65 | def send_slack_message(self, channel, message): |
| #66 | response = self.client.chat_postMessage(channel=channel, text=message) |
| #67 | return response |
| #68 | |
| #69 | def start(self, host="0.0.0.0", port=5000, debug=True): |
| #70 | app = Flask(__name__) |
| #71 | |
| #72 | def signal_handler(sig, frame): |
| #73 | logger.info("\nGracefully shutting down the SlackBot...") |
| #74 | sys.exit(0) |
| #75 | |
| #76 | signal.signal(signal.SIGINT, signal_handler) |
| #77 | |
| #78 | @app.route("/", methods=["POST"]) |
| #79 | def chat(): |
| #80 | # Check if the request is a verification request |
| #81 | if request.json.get("challenge"): |
| #82 | return str(request.json.get("challenge")) |
| #83 | |
| #84 | response = self.handle_message(request.json) |
| #85 | return str(response) |
| #86 | |
| #87 | app.run(host=host, port=port, debug=debug) |
| #88 | |
| #89 | |
| #90 | def start_command(): |
| #91 | parser = argparse.ArgumentParser(description="EmbedChain SlackBot command line interface") |
| #92 | parser.add_argument("--host", default="0.0.0.0", help="Host IP to bind") |
| #93 | parser.add_argument("--port", default=5000, type=int, help="Port to bind") |
| #94 | args = parser.parse_args() |
| #95 | |
| #96 | slack_bot = SlackBot() |
| #97 | slack_bot.start(host=args.host, port=args.port) |
| #98 | |
| #99 | |
| #100 | if __name__ == "__main__": |
| #101 | start_command() |
| #102 |