-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
121 additions
and
10 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,5 @@ | ||
TELEGRAM_BOT_TOKEN=XXXX | ||
QUIVR_TOKEN=XXXX | ||
QUIVR_CHAT_ID=1XXXX | ||
QUIVR_BRAIN_ID=XXXX | ||
QUIVR_URL=XXXX |
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,73 @@ | ||
import logging | ||
import os | ||
|
||
import requests | ||
from dotenv import load_dotenv | ||
from telegram import Update | ||
from telegram.ext import ( | ||
ApplicationBuilder, | ||
CommandHandler, | ||
ContextTypes, | ||
MessageHandler, | ||
filters, | ||
) | ||
|
||
load_dotenv() # Load variables from .env file | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO | ||
) | ||
|
||
telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN", "") | ||
quivr_token = os.getenv("QUIVR_TOKEN", "") | ||
quivr_chat_id = os.getenv("QUIVR_CHAT_ID", "") | ||
quivr_brain_id = os.getenv("QUIVR_BRAIN_ID", "") | ||
quivr_url = ( | ||
os.getenv("QUIVR_URL", "https://api.quivr.app") | ||
+ f"/chat/{quivr_chat_id}/question?brain_id={quivr_brain_id}" | ||
) | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer " + quivr_token, | ||
} | ||
|
||
|
||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
await context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text="I'm Quiv's bot and can answer any question. Please ask your question.", | ||
) | ||
|
||
|
||
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
user_message = update.message.text | ||
response = requests.post( | ||
quivr_url, headers=headers, json={"question": user_message} | ||
) | ||
if response.status_code == 200: | ||
quivr_response = response.json().get( | ||
"assistant", "Sorry, I couldn't understand that." | ||
) | ||
await context.bot.send_message( | ||
chat_id=update.effective_chat.id, text=quivr_response | ||
) | ||
else: | ||
# Log or print the response for debugging | ||
print(f"Error: {response.status_code}, {response.text}") | ||
await context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text=f"Sorry, there was an error processing your request. {response.text}", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
application = ApplicationBuilder().token(telegram_bot_token).build() | ||
|
||
start_handler = CommandHandler("start", start) | ||
message_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message) | ||
|
||
application.add_handler(start_handler) | ||
application.add_handler(message_handler) | ||
|
||
application.run_polling() |
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 @@ | ||
{ | ||
"label": "🔗 Connect Quivr to ...", | ||
"position": 3, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "Connect Quivr to anything" | ||
} | ||
} |
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,34 @@ | ||
--- | ||
title: Telegram | ||
--- | ||
|
||
## Load a Telegram chat | ||
|
||
- You can export your Telegram chat history using the [Telegram Desktop](https://desktop.telegram.org/) app. | ||
- Go to Settings > Advanced > Export Telegram data | ||
- Select the chat you want to export | ||
- Select the format `Machine-readable JSON` | ||
- Click `Export` | ||
- Rename the `json` to `<yourname>.telegram` | ||
- Go to [Quivr.app](https://quivr.app/) and upload the file to a brain | ||
- You can now search your Telegram chat history! | ||
|
||
## Create a telegram bot | ||
|
||
- Go to [BotFather](https://t.me/botfather) and create a new bot | ||
- Copy the token | ||
- Go to `/connectors/telegram` and copy-paste the .env.example file | ||
- `TELEGRAM_BOT_TOKEN` The token you copied from BotFather | ||
- `QUIVR_TOKEN` The API Key of Quivr you can find in your profile | ||
- `QUIVR_CHAT_ID` Create a new chat in Quivr and copy the ID from the URL | ||
- `QUIVR_BRAIN_ID` Copy the id of the brain on which you want to ask question to | ||
- `QUIVR_URL` The URL of the **API** of the Quivr instance you want to use | ||
|
||
Enjoy ! 🎉 | ||
|
||
<div style={{ textAlign: 'center' }}> | ||
<video width="640" height="480" controls> | ||
<source src="https://quivr-cms.s3.eu-west-3.amazonaws.com/quivr_telegram_bot_283a935f26.mp4" type="video/mp4"/> | ||
Your browser does not support the video tag. | ||
</video> | ||
</div> |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
title: 📍 Run Quivr fully locally | ||
--- | ||
|
||
|