add: WhatsApp, Slack and Telegram bots (#438)
This commit is contained in:
committed by
GitHub
parent
09c02954ba
commit
e3ae84b80d
8
examples/whatsapp_bot/.gitignore
vendored
Normal file
8
examples/whatsapp_bot/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
__pycache__
|
||||
db
|
||||
database
|
||||
pyenv
|
||||
venv
|
||||
.env
|
||||
trash_files/
|
||||
.ideas.md
|
||||
3
examples/whatsapp_bot/README.md
Normal file
3
examples/whatsapp_bot/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# WhatsApp Bot
|
||||
|
||||
This is a replit template to create your own WhatsApp bot using the embedchain package. To know more about the bot and how to use it, go [here](https://docs.embedchain.ai/examples/whatsapp_bot).
|
||||
3
examples/whatsapp_bot/requirements.txt
Normal file
3
examples/whatsapp_bot/requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Flask==2.3.2
|
||||
twilio==8.5.0
|
||||
embedchain
|
||||
1
examples/whatsapp_bot/variables.env
Normal file
1
examples/whatsapp_bot/variables.env
Normal file
@@ -0,0 +1 @@
|
||||
OPENAI_API_KEY=""
|
||||
51
examples/whatsapp_bot/whatsapp_bot.py
Normal file
51
examples/whatsapp_bot/whatsapp_bot.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from flask import Flask, request
|
||||
from twilio.twiml.messaging_response import MessagingResponse
|
||||
|
||||
from embedchain import App
|
||||
|
||||
app = Flask(__name__)
|
||||
chat_bot = App()
|
||||
|
||||
|
||||
@app.route("/chat", methods=["POST"])
|
||||
def chat():
|
||||
incoming_message = request.values.get("Body", "").lower()
|
||||
response = handle_message(incoming_message)
|
||||
twilio_response = MessagingResponse()
|
||||
twilio_response.message(response)
|
||||
return str(twilio_response)
|
||||
|
||||
|
||||
def handle_message(message):
|
||||
if message.startswith("add "):
|
||||
response = add_sources(message)
|
||||
else:
|
||||
response = query(message)
|
||||
return response
|
||||
|
||||
|
||||
def add_sources(message):
|
||||
message_parts = message.split(" ", 2)
|
||||
if len(message_parts) == 3:
|
||||
data_type = message_parts[1]
|
||||
url_or_text = message_parts[2]
|
||||
try:
|
||||
chat_bot.add(data_type, url_or_text)
|
||||
response = f"Added {data_type}: {url_or_text}"
|
||||
except Exception as e:
|
||||
response = f"Failed to add {data_type}: {url_or_text}.\nError: {str(e)}"
|
||||
else:
|
||||
response = "Invalid 'add' command format.\nUse: add <data_type> <url_or_text>"
|
||||
return response
|
||||
|
||||
|
||||
def query(message):
|
||||
try:
|
||||
response = chat_bot.chat(message)
|
||||
except Exception:
|
||||
response = "An error occurred. Please try again!"
|
||||
return response
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||
Reference in New Issue
Block a user