add: WhatsApp, Slack and Telegram bots (#438)
This commit is contained in:
committed by
GitHub
parent
09c02954ba
commit
e3ae84b80d
3
examples/api_server/README.md
Normal file
3
examples/api_server/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# API Server
|
||||
|
||||
This is a docker template to create your own API Server using the embedchain package. To know more about the API Server and how to use it, go [here](https://docs.embedchain.ai/examples/api_server).
|
||||
@@ -26,6 +26,19 @@ def add():
|
||||
|
||||
@app.route("/query", methods=["POST"])
|
||||
def query():
|
||||
data = request.get_json()
|
||||
question = data.get("question")
|
||||
if question:
|
||||
try:
|
||||
response = chat_bot.query(question)
|
||||
return jsonify({"data": response}), 200
|
||||
except Exception:
|
||||
return jsonify({"error": "An error occurred. Please try again!"}), 500
|
||||
return jsonify({"error": "Invalid request. Please provide 'question' in JSON format."}), 400
|
||||
|
||||
|
||||
@app.route("/chat", methods=["POST"])
|
||||
def chat():
|
||||
data = request.get_json()
|
||||
question = data.get("question")
|
||||
if question:
|
||||
|
||||
3
examples/discord_bot/README.md
Normal file
3
examples/discord_bot/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Discord Bot
|
||||
|
||||
This is a docker template to create your own Discord bot using the embedchain package. To know more about the bot and how to use it, go [here](https://docs.embedchain.ai/examples/discord_bot).
|
||||
7
examples/slack_bot/.gitignore
vendored
Normal file
7
examples/slack_bot/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
__pycache__
|
||||
db
|
||||
database
|
||||
pyenv
|
||||
venv
|
||||
.env
|
||||
trash_files/
|
||||
3
examples/slack_bot/README.md
Normal file
3
examples/slack_bot/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Slack Bot
|
||||
|
||||
This is a replit template to create your own Slack bot using the embedchain package. To know more about the bot and how to use it, go [here](https://docs.embedchain.ai/examples/slack_bot).
|
||||
5
examples/slack_bot/requirements.txt
Normal file
5
examples/slack_bot/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
flask==2.3.2
|
||||
slackeventsapi==3.0.1
|
||||
slacksdk==3.21.3
|
||||
python-dotenv==1.0.0
|
||||
embedchain
|
||||
58
examples/slack_bot/slack_bot.py
Normal file
58
examples/slack_bot/slack_bot.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from flask import Flask
|
||||
from slack_sdk import WebClient
|
||||
from slackeventsapi import SlackEventAdapter
|
||||
|
||||
from embedchain import App
|
||||
|
||||
load_dotenv()
|
||||
app = Flask(__name__)
|
||||
|
||||
slack_signing_secret = os.environ.get("SLACK_SIGNING_SECRET")
|
||||
slack_events_adapter = SlackEventAdapter(slack_signing_secret, "/chat", app)
|
||||
|
||||
slack_bot_token = os.environ.get("SLACK_BOT_TOKEN")
|
||||
client = WebClient(token=slack_bot_token)
|
||||
|
||||
chat_bot = App()
|
||||
recent_message = {"ts": 0, "channel": ""}
|
||||
|
||||
|
||||
@slack_events_adapter.on("message")
|
||||
def handle_message(event_data):
|
||||
message = event_data["event"]
|
||||
if "text" in message and message.get("subtype") != "bot_message":
|
||||
text = message["text"]
|
||||
if float(message.get("ts")) > float(recent_message["ts"]):
|
||||
recent_message["ts"] = message["ts"]
|
||||
recent_message["channel"] = message["channel"]
|
||||
if text.startswith("query"):
|
||||
_, question = text.split(" ", 1)
|
||||
try:
|
||||
response = chat_bot.chat(question)
|
||||
send_slack_message(message["channel"], response)
|
||||
print("Query answered successfully!")
|
||||
except Exception as e:
|
||||
send_slack_message(message["channel"], "An error occurred. Please try again!")
|
||||
print("Error occurred during 'query' command:", e)
|
||||
elif text.startswith("add"):
|
||||
_, data_type, url_or_text = text.split(" ", 2)
|
||||
if url_or_text.startswith("<") and url_or_text.endswith(">"):
|
||||
url_or_text = url_or_text[1:-1]
|
||||
try:
|
||||
chat_bot.add(data_type, url_or_text)
|
||||
send_slack_message(message["channel"], f"Added {data_type} : {url_or_text}")
|
||||
except Exception as e:
|
||||
send_slack_message(message["channel"], f"Failed to add {data_type} : {url_or_text}")
|
||||
print("Error occurred during 'add' command:", e)
|
||||
|
||||
|
||||
def send_slack_message(channel, message):
|
||||
response = client.chat_postMessage(channel=channel, text=message)
|
||||
return response
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||
3
examples/slack_bot/variables.env
Normal file
3
examples/slack_bot/variables.env
Normal file
@@ -0,0 +1,3 @@
|
||||
SLACK_SIGNING_SECRET=""
|
||||
SLACK_BOT_TOKEN=""
|
||||
OPENAI_API_KEY=""
|
||||
7
examples/telegram_bot/.gitignore
vendored
Normal file
7
examples/telegram_bot/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
__pycache__
|
||||
db
|
||||
database
|
||||
pyenv
|
||||
venv
|
||||
.env
|
||||
trash_files/
|
||||
3
examples/telegram_bot/README.md
Normal file
3
examples/telegram_bot/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Telegram Bot
|
||||
|
||||
This is a replit template to create your own Telegram bot using the embedchain package. To know more about the bot and how to use it, go [here](https://docs.embedchain.ai/examples/telegram_bot).
|
||||
4
examples/telegram_bot/requirements.txt
Normal file
4
examples/telegram_bot/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
flask==2.3.2
|
||||
requests==2.31.0
|
||||
python-dotenv==1.0.0
|
||||
embedchain
|
||||
66
examples/telegram_bot/telegram_bot.py
Normal file
66
examples/telegram_bot/telegram_bot.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
from flask import Flask, request
|
||||
|
||||
from embedchain import App
|
||||
|
||||
app = Flask(__name__)
|
||||
load_dotenv()
|
||||
bot_token = os.environ["TELEGRAM_BOT_TOKEN"]
|
||||
chat_bot = App()
|
||||
|
||||
|
||||
@app.route("/", methods=["POST"])
|
||||
def telegram_webhook():
|
||||
data = request.json
|
||||
message = data["message"]
|
||||
chat_id = message["chat"]["id"]
|
||||
text = message["text"]
|
||||
if text.startswith("/start"):
|
||||
response_text = (
|
||||
"Welcome to Embedchain Bot! Try the following commands to use the bot:\n"
|
||||
"For adding data sources:\n /add <data_type> <url_or_text>\n"
|
||||
"For asking queries:\n /query <question>"
|
||||
)
|
||||
elif text.startswith("/add"):
|
||||
_, data_type, url_or_text = text.split(maxsplit=2)
|
||||
response_text = add_to_chat_bot(data_type, url_or_text)
|
||||
elif text.startswith("/query"):
|
||||
_, question = text.split(maxsplit=1)
|
||||
response_text = query_chat_bot(question)
|
||||
else:
|
||||
response_text = "Invalid command. Please refer to the documentation for correct syntax."
|
||||
send_message(chat_id, response_text)
|
||||
return "OK"
|
||||
|
||||
|
||||
def add_to_chat_bot(data_type, url_or_text):
|
||||
try:
|
||||
chat_bot.add(data_type, url_or_text)
|
||||
response_text = f"Added {data_type} : {url_or_text}"
|
||||
except Exception as e:
|
||||
response_text = f"Failed to add {data_type} : {url_or_text}"
|
||||
print("Error occurred during 'add' command:", e)
|
||||
return response_text
|
||||
|
||||
|
||||
def query_chat_bot(question):
|
||||
try:
|
||||
response = chat_bot.chat(question)
|
||||
response_text = response
|
||||
except Exception as e:
|
||||
response_text = "An error occurred. Please try again!"
|
||||
print("Error occurred during 'query' command:", e)
|
||||
return response_text
|
||||
|
||||
|
||||
def send_message(chat_id, text):
|
||||
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
||||
data = {"chat_id": chat_id, "text": text}
|
||||
requests.post(url, json=data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||
2
examples/telegram_bot/variables.env
Normal file
2
examples/telegram_bot/variables.env
Normal file
@@ -0,0 +1,2 @@
|
||||
TELEGRAM_BOT_TOKEN=""
|
||||
OPENAI_API_KEY=""
|
||||
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