Skip to content

Commit

Permalink
add: WhatsApp, Slack and Telegram bots (mem0ai#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilyadav902 committed Aug 14, 2023
1 parent 09c0295 commit e3ae84b
Show file tree
Hide file tree
Showing 23 changed files with 384 additions and 4 deletions.
51 changes: 48 additions & 3 deletions docs/examples/api_server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: '🌍 API Server'
---

The API Server based on Flask integrates the `embedchain` package, offering endpoints to add, query, and chat to engage in conversations with a chatbot using JSON requests.

### 🐳 Docker Setup

- Open variables.env, and edit it to add your 🔑 `OPENAI_API_KEY`.
Expand All @@ -16,8 +18,8 @@ docker-compose up --build
### 🚀 Usage Instructions

- Your api server is running on [http:https://localhost:5000/](http:https://localhost:5000/)
- To use the api server, make an api call to the endpoints `/add` and `/query` using the json formats discussed below.
- To add data sources to the bot:
- To use the api server, make an api call to the endpoints `/add`, `/query` and `/chat` using the json formats discussed below.
- To add data sources to the bot (/add):
```json
// Request
{
Expand All @@ -30,7 +32,19 @@ docker-compose up --build
"data": "Added data_type: url_or_text"
}
```
- To ask questions from the bot:
- To ask queries from the bot (/query):
```json
// Request
{
"question": "your_question_here"
}

// Response
{
"data": "your_answer_here"
}
```
- To chat with the bot (/chat):
```json
// Request
{
Expand All @@ -43,4 +57,35 @@ docker-compose up --build
}
```

### 📡 Curl Call Formats

- To add data sources to the bot (/add):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"data_type": "your_data_type_here",
"url_or_text": "your_url_or_text_here"
}' \
http:https://localhost:5000/add
```
- To ask queries from the bot (/query):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"question": "your_question_here"
}' \
http:https://localhost:5000/query
```
- To chat with the bot (/chat):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"question": "your_question_here"
}' \
http:https://localhost:5000/chat
```

🎉 Happy Chatting! 🎉
39 changes: 39 additions & 0 deletions docs/examples/slack_bot.mdx
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! 🎉
26 changes: 26 additions & 0 deletions docs/examples/telegram_bot.mdx
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! 🎉
27 changes: 27 additions & 0 deletions docs/examples/whatsapp_bot.mdx
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! 🎉
2 changes: 1 addition & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
{
"group": "Examples",
"pages": ["examples/full_stack", "examples/api_server", "examples/discord_bot"]
"pages": ["examples/full_stack", "examples/api_server", "examples/discord_bot", "examples/slack_bot", "examples/telegram_bot", "examples/whatsapp_bot"]
},
{
"group": "Contribution Guidelines",
Expand Down
3 changes: 3 additions & 0 deletions examples/api_server/README.md
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).
13 changes: 13 additions & 0 deletions examples/api_server/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions examples/discord_bot/README.md
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).
7 changes: 7 additions & 0 deletions examples/slack_bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
3 changes: 3 additions & 0 deletions examples/slack_bot/README.md
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).
5 changes: 5 additions & 0 deletions examples/slack_bot/requirements.txt
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
58 changes: 58 additions & 0 deletions examples/slack_bot/slack_bot.py
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)
3 changes: 3 additions & 0 deletions examples/slack_bot/variables.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SLACK_SIGNING_SECRET=""
SLACK_BOT_TOKEN=""
OPENAI_API_KEY=""
7 changes: 7 additions & 0 deletions examples/telegram_bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
3 changes: 3 additions & 0 deletions examples/telegram_bot/README.md
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).
4 changes: 4 additions & 0 deletions examples/telegram_bot/requirements.txt
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
66 changes: 66 additions & 0 deletions examples/telegram_bot/telegram_bot.py
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)
2 changes: 2 additions & 0 deletions examples/telegram_bot/variables.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TELEGRAM_BOT_TOKEN=""
OPENAI_API_KEY=""
8 changes: 8 additions & 0 deletions examples/whatsapp_bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
.ideas.md
3 changes: 3 additions & 0 deletions examples/whatsapp_bot/README.md
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).
Loading

0 comments on commit e3ae84b

Please sign in to comment.