forked from mem0ai/mem0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: WhatsApp, Slack and Telegram bots (mem0ai#438)
- Loading branch information
1 parent
09c0295
commit e3ae84b
Showing
23 changed files
with
384 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: '💼 Slack Bot' | ||
--- | ||
|
||
### 🖼️ Template Setup | ||
|
||
- Fork [this](https://replit.com/@taranjeetio/EC-Slack-Bot-Template?v=1#README.md) replit template. | ||
- Set your `OPENAI_API_KEY` in Secrets. | ||
- Create a workspace on Slack if you don't have one already by clicking [here](https://slack.com/intl/en-in/). | ||
- Create a new App on your Slack account by going [here](https://api.slack.com/apps). | ||
- Select `From Scratch`, then enter the Bot Name and select your workspace. | ||
- On the `Basic Information` page copy the `Signing Secret` and set it in your secrets as `SLACK_SIGNING_SECRET`. | ||
- On the left Sidebar, go to `OAuth and Permissions` and add the following scopes under `Bot Token Scopes`: | ||
```text | ||
app_mentions:read | ||
channels:history | ||
channels:read | ||
chat:write | ||
``` | ||
- Now select the option `Install to Workspace` and after it's done, copy the `Bot User OAuth Token` and set it in your secrets as `SLACK_BOT_TOKEN`. | ||
- Start your replit container now by clicking on `Run`. | ||
- On the Slack API website go to `Event Subscriptions` on the left Sidebar and turn on `Enable Events`. | ||
- Copy the generated server URL in replit, append `/chat` at its end and paste it in `Request URL` box. | ||
- After it gets verified, click on `Subscribe to bot events`, add `message.channels` Bot User Event and click on `Save Changes`. | ||
- Now go to your workspace, click on the bot name in the Sidebar and then add the bot to any channel you want. | ||
|
||
### 🚀 Usage Instructions | ||
|
||
- Go to the channel where you have added your bot. | ||
- To add data sources to the bot, use the command: | ||
```text | ||
add <data_type> <url_or_text> | ||
``` | ||
- To ask queries from the bot, use the command: | ||
```text | ||
query <question> | ||
``` | ||
|
||
🎉 Happy Chatting! 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: '📱 Telegram Bot' | ||
--- | ||
|
||
### 🖼️ Template Setup | ||
|
||
- Fork [this](https://replit.com/@taranjeetio/EC-Telegram-Bot-Template?v=1#README.md) replit template. | ||
- Set your `OPENAI_API_KEY` in Secrets. | ||
- Open the Telegram app and search for the `BotFather` user. | ||
- Start a chat with BotFather and use the `/newbot` command to create a new bot. | ||
- Follow the instructions to choose a name and username for your bot. | ||
- Once the bot is created, BotFather will provide you with a unique token for your bot. | ||
- Set this token as `TELEGRAM_BOT_TOKEN` in Secrets. | ||
- Click on `Run` in the replit container and a URL will get generated for your bot. | ||
- Now set your webhook by running the following link in your browser: | ||
```url | ||
https://api.telegram.org/bot<Your_Telegram_Bot_Token>/setWebhook?url=<Replit_Generated_URL> | ||
``` | ||
- When you get a successful response in your browser, your bot is ready to be used. | ||
|
||
### 🚀 Usage Instructions | ||
|
||
- Open your bot by searching for it using the bot name or bot username. | ||
- Click on `Start` or type `/start` and follow the on screen instructions. | ||
|
||
🎉 Happy Chatting! 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: '💬 WhatsApp Bot' | ||
--- | ||
|
||
### 🖼️ Template Setup | ||
|
||
- Fork [this](https://replit.com/@taranjeetio/EC-WhatsApp-Bot-Template?v=1#README.md) replit template. | ||
- Set your `OPENAI_API_KEY` in Secrets. | ||
- Register for a free account on [Twilio](https://www.twilio.com/try-twilio) and create a new Whatsapp Sandbox. | ||
- You can find it in the left Sidebar under `Develop` by navigating to `Messaging>Try it out>Send a WhatsApp Message`. | ||
- To connect a phone number and allow it to chat with the bot, follow the on screen instructions. | ||
- Click on `Run` in the replit container and a URL will get generated for your bot. | ||
- Copy the URL, append `/chat` at the end and paste it under `When a message comes in` under the `Sandbox settings` for Whatsapp in Twilio. Save your settings. | ||
|
||
### 🚀 Usage Instructions | ||
|
||
- To allow a new number or reconnect an old number with the Sandbox, follow the instructions in your Twilio account. | ||
- To add data sources to the bot, use the command: | ||
```text | ||
add <data_type> <url_or_text> | ||
``` | ||
- To ask queries from the bot, use the command: | ||
```text | ||
<question> | ||
``` | ||
|
||
🎉 Happy Chatting! 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__pycache__ | ||
db | ||
database | ||
pyenv | ||
venv | ||
.env | ||
trash_files/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
flask==2.3.2 | ||
slackeventsapi==3.0.1 | ||
slacksdk==3.21.3 | ||
python-dotenv==1.0.0 | ||
embedchain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SLACK_SIGNING_SECRET="" | ||
SLACK_BOT_TOKEN="" | ||
OPENAI_API_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__pycache__ | ||
db | ||
database | ||
pyenv | ||
venv | ||
.env | ||
trash_files/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
flask==2.3.2 | ||
requests==2.31.0 | ||
python-dotenv==1.0.0 | ||
embedchain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TELEGRAM_BOT_TOKEN="" | ||
OPENAI_API_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
__pycache__ | ||
db | ||
database | ||
pyenv | ||
venv | ||
.env | ||
trash_files/ | ||
.ideas.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
Oops, something went wrong.