Skip to content

Commit

Permalink
Merge pull request #134 from AlexHTW/add-user-id-to-log
Browse files Browse the repository at this point in the history
add user id to all logs
  • Loading branch information
n3d1117 authored Mar 29, 2023
2 parents e110afd + bddf84e commit 4ed59d2
Showing 1 changed file with 29 additions and 15 deletions.
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

0 comments on commit 4ed59d2

Please sign in to comment.