add: WhatsApp, Slack and Telegram bots (#438)

This commit is contained in:
Sahil Kumar Yadav
2023-08-15 03:10:25 +05:30
committed by GitHub
parent 09c02954ba
commit e3ae84b80d
23 changed files with 384 additions and 4 deletions

8
examples/whatsapp_bot/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
.ideas.md

View 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).

View File

@@ -0,0 +1,3 @@
Flask==2.3.2
twilio==8.5.0
embedchain

View File

@@ -0,0 +1 @@
OPENAI_API_KEY=""

View 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)