Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user id to all logs #134

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
Returns token usage statistics for current day and month.
"""
if not await self.is_allowed(update):
logging.warning(f'User {update.message.from_user.name} is not allowed to request their usage statistics')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'is not allowed to request their usage statistics')
await self.send_disallowed_message(update, context)
return

logging.info(f'User {update.message.from_user.name} requested their usage statistics')
logging.info(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'requested their usage statistics')

user_id = update.message.from_user.id
if user_id not in self.usage:
Expand Down Expand Up @@ -127,11 +129,13 @@ async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
Resets the conversation.
"""
if not await self.is_allowed(update):
logging.warning(f'User {update.message.from_user.name} is not allowed to reset the conversation')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})' \
f'is not allowed to reset the conversation')
await self.send_disallowed_message(update, context)
return

logging.info(f'Resetting the conversation for user {update.message.from_user.name}...')
logging.info(f'Resetting the conversation for user {update.message.from_user.name} '\
f'(id: {update.message.from_user.id})...')

chat_id = update.effective_chat.id
reset_content = message_text(update.message)
Expand All @@ -143,12 +147,14 @@ async def image(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
Generates an image for the given prompt using DALL·E APIs
"""
if not await self.is_allowed(update):
logging.warning(f'User {update.message.from_user.name} is not allowed to generate images')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'is not allowed to generate images')
await self.send_disallowed_message(update, context)
return

if not await self.is_within_budget(update):
logging.warning(f'User {update.message.from_user.name} reached their usage limit')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'reached their usage limit')
await self.send_budget_reached_message(update, context)
return

Expand All @@ -158,7 +164,8 @@ async def image(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=chat_id, text='Please provide a prompt! (e.g. /image cat)')
return

logging.info(f'New image generation request received from user {update.message.from_user.name}')
logging.info(f'New image generation request received from user {update.message.from_user.name} '\
f'(id: {update.message.from_user.id})')

async def _generate():
try:
Expand Down Expand Up @@ -191,12 +198,14 @@ async def transcribe(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
Transcribe audio messages.
"""
if not await self.is_allowed(update):
logging.warning(f'User {update.message.from_user.name} is not allowed to transcribe audio messages')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'is not allowed to transcribe audio messages')
await self.send_disallowed_message(update, context)
return

if not await self.is_within_budget(update):
logging.warning(f'User {update.message.from_user.name} reached their usage limit')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'reached their usage limit')
await self.send_budget_reached_message(update, context)
return

Expand Down Expand Up @@ -227,7 +236,8 @@ async def _execute():
try:
audio_track = AudioSegment.from_file(filename)
audio_track.export(filename_mp3, format="mp3")
logging.info(f'New transcribe request received from user {update.message.from_user.name}')
logging.info(f'New transcribe request received from user {update.message.from_user.name} '\
f'(id: {update.message.from_user.id})')

except Exception as e:
logging.exception(e)
Expand Down Expand Up @@ -318,16 +328,18 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
React to incoming messages and respond accordingly.
"""
if not await self.is_allowed(update):
logging.warning(f'User {update.message.from_user.name} is not allowed to use the bot')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'is not allowed to use the bot')
await self.send_disallowed_message(update, context)
return

if not await self.is_within_budget(update):
logging.warning(f'User {update.message.from_user.name} reached their usage limit')
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
f'reached their usage limit')
await self.send_budget_reached_message(update, context)
return

logging.info(f'New message received from user {update.message.from_user.name}')
logging.info(f'New message received from user {update.message.from_user.name} (id: {update.message.from_user.id})')
chat_id = update.effective_chat.id
user_id = update.message.from_user.id
prompt = update.message.text
Expand Down Expand Up @@ -592,7 +604,8 @@ async def is_allowed(self, update: Update) -> bool:
if await self.is_user_in_group(update, user):
logging.info(f'{user} is a member. Allowing group chat message...')
return True
logging.info(f'Group chat messages from user {update.message.from_user.name} are not allowed')
logging.info(f'Group chat messages from user {update.message.from_user.name} '\
f'(id: {update.message.from_user.id}) are not allowed')

return False

Expand Down Expand Up @@ -679,7 +692,8 @@ async def is_within_budget(self, update: Update) -> bool:
return True
logging.warning('Monthly guest budget for group chats used up.')
return False
logging.info(f'Group chat messages from user {update.message.from_user.name} are not allowed')
logging.info(f'Group chat messages from user {update.message.from_user.name} '\
f'(id: {update.message.from_user.id}) are not allowed')
return False

def split_into_chunks(self, text: str, chunk_size: int = 4096) -> list[str]:
Expand Down