Skip to content

Commit

Permalink
Add ALLOWED_USERS, add lint step, refactorig
Browse files Browse the repository at this point in the history
  • Loading branch information
Biunovich committed Apr 6, 2024
1 parent b28ff51 commit aba0e9c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ on:

jobs:

ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build . --file Dockerfile --tag biunovich/simple_openai_telegram_bot

Expand Down
15 changes: 11 additions & 4 deletions Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

telegram_token = os.environ.get("TELEGRAM_TOKEN")
openai_api_key = os.environ.get("OPENAI_API_KEY")
allowed_users = os.environ.get("ALLOWED_USERS")

if allowed_users is None:
logger.warning("Any user can send messages to this bot")
else:
allowed_users = list(map(int, allowed_users.split(",")))

if telegram_token is None or openai_api_key is None:
logger.error("No environment variables were found")
Expand All @@ -43,7 +49,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
append_message(messages, "user", message)
try:
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo",
model="gpt-4-turbo-preview",
messages=messages,
temperature=0.5,
user=user_id,
Expand Down Expand Up @@ -77,11 +83,12 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):

async def clean(update: Update, context: ContextTypes.DEFAULT_TYPE):
context.user_data["messages"] = []
await update.message.reply_text(f"Message history was cleaned!")
await update.message.reply_text("Message history was cleaned!")

application = Application.builder().token(telegram_token).build()
application.add_handler(MessageHandler(
filters.TEXT & ~filters.COMMAND, handle_message))
application.add_handler(
MessageHandler(filters.TEXT & ~filters.COMMAND & filters.User(user_id=allowed_users, allow_empty=True), handle_message)
)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("clean", clean))
application.run_polling()
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Just create `.env` file and setup Telegram bot token and OpenAI token
```
OPENAI_API_KEY=open-ai-token
TELEGRAM_TOKEN=telegram-bot-token
ALLOWED_USERS=id1,id2 // optional
```

0 comments on commit aba0e9c

Please sign in to comment.