Skip to content

Commit

Permalink
Merge pull request #7 from havryliuk/daily-word
Browse files Browse the repository at this point in the history
#1 get daily word
  • Loading branch information
havryliuk committed Jan 3, 2024
2 parents 64c33f6 + 8d0504a commit dc5d83d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 22 deletions.
51 changes: 30 additions & 21 deletions daily_dragon.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import logging
from dotenv import load_dotenv
import os

from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
from openai import OpenAI

logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
import prompts

load_dotenv()
TOKEN = os.getenv('TOKEN')
if TOKEN is None:
raise ValueError('TOKEN not found in .env file')
logger = logging.getLogger(__name__)

LANGUAGES = {'Chinese', 'Japanese'}

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I am 每日龙!\nLauching soon!")

if __name__ == '__main__':
application = ApplicationBuilder().token(TOKEN).build()

start_handler = CommandHandler('start', start)
application.add_handler(start_handler)

application.run_polling()
class DailyDragon:
openai_client: OpenAI
language: str

def __init__(self):
self.openai_client = OpenAI()
self.language = 'Chinese'

def get_daily_word(self):
prompt = prompts.get_daily_word_prompt()
prompt = prompt.format(language=self.language)
logger.info(f"Language: {self.language}")
completion = self.openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"You are a teacher of {self.language}."},
{"role": "user", "content": f"${prompt}"}
]
)
logger.info(completion)
return completion.choices[0].message.content

def set_language(self, language: str):
if language not in LANGUAGES:
raise ValueError(f'Language {language} not supported')
self.language = language
42 changes: 42 additions & 0 deletions daily_dragon_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging
from dotenv import load_dotenv
import os

from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler

from daily_dragon import DailyDragon

logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)

load_dotenv()
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
if TELEGRAM_TOKEN is None or TELEGRAM_TOKEN == '':
raise ValueError('TELEGRAM_TOKEN not found in .env file')

logger = logging.getLogger(__name__)
daily_dragon = DailyDragon()


RAINY_BABE = 'rainy_babe'


async def random_word(update: Update, context: ContextTypes.DEFAULT_TYPE):
username = update.effective_user.username
logger.info(f'User {username} requested daily word')
if username == RAINY_BABE:
daily_dragon.set_language('Japanese')
daily_word_response = daily_dragon.get_daily_word()
await context.bot.send_message(chat_id=update.effective_chat.id, text=daily_word_response)


if __name__ == '__main__':
application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()

start_handler = CommandHandler('random', random_word)
application.add_handler(start_handler)

application.run_polling()
3 changes: 3 additions & 0 deletions prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def get_daily_word_prompt():
with (open('prompts/daily_word', 'r', encoding='utf-8')) as file:
return file.read()
11 changes: 11 additions & 0 deletions prompts/daily_word
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Provide me one random new ${language} word with pronunciation, meanings in English and several sentences to illustrate them.
Respond in the following format:
```
Word: <word> (jiǎo zi)
Meaning: dumplings

Example sentences:
1. <sentence>
(<pronunciation>)
<translation>
```
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python-dotenv==1.0.0
python-dotenv~=1.0.0
python-telegram-bot
openai~=1.6.1

0 comments on commit dc5d83d

Please sign in to comment.