Skip to content

Commit

Permalink
chore: Add MongoDB connection URL to environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rabilrbl committed May 4, 2024
1 parent f3e6342 commit 2f719fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ AUTHORIZED_USERS=
# Required
GROQ_API_KEY=
BOT_TOKEN=
MONGODB_URL=
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +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).
* `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
47 changes: 37 additions & 10 deletions groq_chat/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CommandHandler,
MessageHandler,
CallbackQueryHandler,
ConversationHandler
ConversationHandler,
)
from groq_chat.handlers import (
start,
Expand All @@ -19,10 +19,11 @@
start_system_prompt,
get_system_prompt,
cancelled_system_prompt,
info_command_handler
info_command_handler,
)
from groq_chat.filters import AuthFilter, MessageFilter
from dotenv import load_dotenv
from mongopersistence import MongoPersistence
import logging

load_dotenv()
Expand All @@ -36,33 +37,59 @@

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"],
)


def start_bot():
logger.info("Starting bot")
app = Application.builder().token(os.getenv("BOT_TOKEN")).build()
app.add_error_handler(lambda _, __: logger.error("Exception while handling an update:", exc_info=True))
app = (
Application.builder()
.token(os.getenv("BOT_TOKEN"))
.persistence(persistence)
.build()
)
app.add_error_handler(
lambda _, __: logger.error("Exception while handling an update:", exc_info=True)
)

app.add_handler(CommandHandler("start", start, filters=AuthFilter))
app.add_handler(CommandHandler("help", help_command, filters=AuthFilter))
app.add_handler(CommandHandler("new", new_command_handler, filters=AuthFilter))
app.add_handler(CommandHandler("model", model_command_handler, filters=AuthFilter))
app.add_handler(CommandHandler("info", info_command_handler, filters=AuthFilter))

app.add_handler(
ConversationHandler(
entry_points=[CommandHandler("system_prompt", start_system_prompt, filters=AuthFilter)],
entry_points=[
CommandHandler("system_prompt", start_system_prompt, filters=AuthFilter)
],
states={
SYSTEM_PROMPT_SP: [MessageHandler(MessageFilter, get_system_prompt)],
CANCEL_SP: [CommandHandler("cancel", cancelled_system_prompt, filters=AuthFilter)],
CANCEL_SP: [
CommandHandler(
"cancel", cancelled_system_prompt, filters=AuthFilter
)
],
},
fallbacks=[CommandHandler("cancel", cancelled_system_prompt, filters=AuthFilter)],
fallbacks=[
CommandHandler("cancel", cancelled_system_prompt, filters=AuthFilter)
],
)
)

app.add_handler(MessageHandler(MessageFilter, message_handler))
app.add_handler(
CallbackQueryHandler(change_model_callback_handler, pattern="^change_model_")
)

# Run the bot until the user presses Ctrl-C
app.run_polling(allowed_updates=Update.ALL_TYPES)
app.run_polling(allowed_updates=Update.ALL_TYPES)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
groq==0.5.0
python-dotenv==1.0.1
python-telegram-bot==21.1.1
mongopersistence==0.3.0

0 comments on commit 2f719fa

Please sign in to comment.