Rename embedchain to mem0 and open sourcing code for long term memory (#1474)
Co-authored-by: Deshraj Yadav <deshrajdry@gmail.com>
This commit is contained in:
2
embedchain/examples/telegram_bot/.env.example
Normal file
2
embedchain/examples/telegram_bot/.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
TELEGRAM_BOT_TOKEN=
|
||||
OPENAI_API_KEY=
|
||||
7
embedchain/examples/telegram_bot/.gitignore
vendored
Normal file
7
embedchain/examples/telegram_bot/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
__pycache__
|
||||
db
|
||||
database
|
||||
pyenv
|
||||
venv
|
||||
.env
|
||||
trash_files/
|
||||
11
embedchain/examples/telegram_bot/Dockerfile
Normal file
11
embedchain/examples/telegram_bot/Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
WORKDIR /usr/src/
|
||||
COPY requirements.txt .
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["python", "telegram_bot.py"]
|
||||
3
embedchain/examples/telegram_bot/README.md
Normal file
3
embedchain/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
embedchain/examples/telegram_bot/requirements.txt
Normal file
4
embedchain/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
embedchain/examples/telegram_bot/telegram_bot.py
Normal file
66
embedchain/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=8000, debug=False)
|
||||
Reference in New Issue
Block a user