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 importlib |
| #3 | import logging |
| #4 | import signal |
| #5 | import sys |
| #6 | |
| #7 | from embedchain.helpers.json_serializable import register_deserializable |
| #8 | |
| #9 | from .base import BaseBot |
| #10 | |
| #11 | logger = logging.getLogger(__name__) |
| #12 | |
| #13 | |
| #14 | @register_deserializable |
| #15 | class WhatsAppBot(BaseBot): |
| #16 | def __init__(self): |
| #17 | try: |
| #18 | self.flask = importlib.import_module("flask") |
| #19 | self.twilio = importlib.import_module("twilio") |
| #20 | except ModuleNotFoundError: |
| #21 | raise ModuleNotFoundError( |
| #22 | "The required dependencies for WhatsApp are not installed. " |
| #23 | "Please install with `pip install twilio==8.5.0 flask==2.3.3`" |
| #24 | ) from None |
| #25 | super().__init__() |
| #26 | |
| #27 | def handle_message(self, message): |
| #28 | if message.startswith("add "): |
| #29 | response = self.add_data(message) |
| #30 | else: |
| #31 | response = self.ask_bot(message) |
| #32 | return response |
| #33 | |
| #34 | def add_data(self, message): |
| #35 | data = message.split(" ")[-1] |
| #36 | try: |
| #37 | self.add(data) |
| #38 | response = f"Added data from: {data}" |
| #39 | except Exception: |
| #40 | logger.exception(f"Failed to add data {data}.") |
| #41 | response = "Some error occurred while adding data." |
| #42 | return response |
| #43 | |
| #44 | def ask_bot(self, message): |
| #45 | try: |
| #46 | response = self.query(message) |
| #47 | except Exception: |
| #48 | logger.exception(f"Failed to query {message}.") |
| #49 | response = "An error occurred. Please try again!" |
| #50 | return response |
| #51 | |
| #52 | def start(self, host="0.0.0.0", port=5000, debug=True): |
| #53 | app = self.flask.Flask(__name__) |
| #54 | |
| #55 | def signal_handler(sig, frame): |
| #56 | logger.info("\nGracefully shutting down the WhatsAppBot...") |
| #57 | sys.exit(0) |
| #58 | |
| #59 | signal.signal(signal.SIGINT, signal_handler) |
| #60 | |
| #61 | @app.route("/chat", methods=["POST"]) |
| #62 | def chat(): |
| #63 | incoming_message = self.flask.request.values.get("Body", "").lower() |
| #64 | response = self.handle_message(incoming_message) |
| #65 | twilio_response = self.twilio.twiml.messaging_response.MessagingResponse() |
| #66 | twilio_response.message(response) |
| #67 | return str(twilio_response) |
| #68 | |
| #69 | app.run(host=host, port=port, debug=debug) |
| #70 | |
| #71 | |
| #72 | def start_command(): |
| #73 | parser = argparse.ArgumentParser(description="EmbedChain WhatsAppBot command line interface") |
| #74 | parser.add_argument("--host", default="0.0.0.0", help="Host IP to bind") |
| #75 | parser.add_argument("--port", default=5000, type=int, help="Port to bind") |
| #76 | args = parser.parse_args() |
| #77 | |
| #78 | whatsapp_bot = WhatsAppBot() |
| #79 | whatsapp_bot.start(host=args.host, port=args.port) |
| #80 | |
| #81 | |
| #82 | if __name__ == "__main__": |
| #83 | start_command() |
| #84 |