Skip to content

Commit

Permalink
chore: Update MongoDB connection URL handling in bot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rabilrbl committed May 5, 2024
1 parent 416517b commit 8b27559
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Telegram Bot API token
* Groq API Key
* dotenv (for environment variables)

* MongoDB (for storing chat history - optional)

### Docker

Expand Down Expand Up @@ -62,7 +62,7 @@ docker run --env-file .env groq-chatbot
3. Create a `.env` file and add the following environment variables:
* `BOT_TOKEN`: Your Telegram Bot API token. You can get one by talking to [@BotFather](https://t.me/BotFather).
* `GROQ_API_KEY`: Your Groq API key. You can get one by signing up at [Groq Console](https://console.groq.com/keys).
* `MONGODB_URL`: Your MongoDB connection URL. Get one from [MongoDB Atlas](https://www.mongodb.com/cloud/atlas).
* `MONGODB_URL`: Your MongoDB connection URL. Get one from [MongoDB Atlas](https://www.mongodb.com/cloud/atlas). (optional)
* `AUTHORIZED_USERS`: A comma-separated list of Telegram usernames or user IDs that are authorized to access the bot. (optional) Example value: `shonan23,1234567890`
4. Run the bot:
* `python main.py` (if not using pipenv)
Expand Down
39 changes: 22 additions & 17 deletions groq_chat/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,32 @@

logger = logging.getLogger(__name__)

persistence = MongoPersistence(
mongo_url=os.getenv("MONGODB_URL"),
db_name="groq-chatbot",
name_col_user_data="user_data",
name_col_bot_data="bot_data",
name_col_chat_data="chat_data",
name_col_conversations_data="conversations_data",
create_col_if_not_exist=True, # optional
ignore_general_data=["cache"],
update_interval=10,
)
persistence = None
if os.getenv("MONGODB_URL"):
persistence = MongoPersistence(
mongo_url=os.getenv("MONGODB_URL"),
db_name="groq-chatbot",
name_col_user_data="user_data",
name_col_bot_data="bot_data",
name_col_chat_data="chat_data",
name_col_conversations_data="conversations_data",
create_col_if_not_exist=True, # optional
ignore_general_data=["cache"],
update_interval=10,
)


def start_bot():
logger.info("Starting bot")
app = (
Application.builder()
.token(os.getenv("BOT_TOKEN"))
.persistence(persistence)
.build()
)

app_builder = Application.builder().token(os.getenv("BOT_TOKEN"))

# Add persistence if available
if persistence:
app_builder.persistence = persistence

# Build the app
app = app_builder.build()

app.add_handler(CommandHandler("start", start, filters=AuthFilter))
app.add_handler(CommandHandler("help", help_command, filters=AuthFilter))
Expand Down

0 comments on commit 8b27559

Please # to comment.