-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from havryliuk/daily-word
#1 get daily word
- Loading branch information
Showing
5 changed files
with
88 additions
and
22 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
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 |
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,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() |
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,3 @@ | ||
def get_daily_word_prompt(): | ||
with (open('prompts/daily_word', 'r', encoding='utf-8')) as file: | ||
return file.read() |
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,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> | ||
``` |
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,2 +1,3 @@ | ||
python-dotenv==1.0.0 | ||
python-dotenv~=1.0.0 | ||
python-telegram-bot | ||
openai~=1.6.1 |