mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-22 15:14:54 +01:00
minor improvements
This commit is contained in:
@@ -76,12 +76,12 @@ class ChatGPTTelegramBot:
|
|||||||
Returns token usage statistics for current day and month.
|
Returns token usage statistics for current day and month.
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'is not allowed to request their usage statistics')
|
f'is not allowed to request their usage statistics')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
return
|
||||||
|
|
||||||
logging.info(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.info(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'requested their usage statistics')
|
f'requested their usage statistics')
|
||||||
|
|
||||||
user_id = update.message.from_user.id
|
user_id = update.message.from_user.id
|
||||||
@@ -131,34 +131,37 @@ class ChatGPTTelegramBot:
|
|||||||
Resend the last request
|
Resend the last request
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} is not allowed to resend the message')
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})'
|
||||||
|
f' is not allowed to resend the message')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
return
|
||||||
|
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
if chat_id not in self.last_message:
|
if chat_id not in self.last_message:
|
||||||
logging.warning(f'User {update.message.from_user.name} does not have anything to resend')
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})'
|
||||||
|
f' does not have anything to resend')
|
||||||
await context.bot.send_message(chat_id=chat_id, text="You have nothing to resend")
|
await context.bot.send_message(chat_id=chat_id, text="You have nothing to resend")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Update message text, clear self.last_message and send the request to promt
|
# Update message text, clear self.last_message and send the request to prompt
|
||||||
logging.info(f'Resending the last prompt from user: {update.message.from_user.name}')
|
logging.info(f'Resending the last prompt from user: {update.message.from_user.name} '
|
||||||
|
f'(id: {update.message.from_user.id})')
|
||||||
with update.message._unfrozen() as message:
|
with update.message._unfrozen() as message:
|
||||||
message.text = self.last_message.pop(chat_id)
|
message.text = self.last_message.pop(chat_id)
|
||||||
|
|
||||||
await self.prompt(update=update, context=context)
|
await self.prompt(update=update, context=context)
|
||||||
|
|
||||||
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
"""
|
"""
|
||||||
Resets the conversation.
|
Resets the conversation.
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})' \
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'is not allowed to reset the conversation')
|
f'is not allowed to reset the conversation')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
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})...')
|
f'(id: {update.message.from_user.id})...')
|
||||||
|
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
@@ -171,13 +174,13 @@ class ChatGPTTelegramBot:
|
|||||||
Generates an image for the given prompt using DALL·E APIs
|
Generates an image for the given prompt using DALL·E APIs
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'is not allowed to generate images')
|
f'is not allowed to generate images')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not await self.is_within_budget(update):
|
if not await self.is_within_budget(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'reached their usage limit')
|
f'reached their usage limit')
|
||||||
await self.send_budget_reached_message(update, context)
|
await self.send_budget_reached_message(update, context)
|
||||||
return
|
return
|
||||||
@@ -188,7 +191,7 @@ class ChatGPTTelegramBot:
|
|||||||
await context.bot.send_message(chat_id=chat_id, text='Please provide a prompt! (e.g. /image cat)')
|
await context.bot.send_message(chat_id=chat_id, text='Please provide a prompt! (e.g. /image cat)')
|
||||||
return
|
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})')
|
f'(id: {update.message.from_user.id})')
|
||||||
|
|
||||||
async def _generate():
|
async def _generate():
|
||||||
@@ -222,13 +225,13 @@ class ChatGPTTelegramBot:
|
|||||||
Transcribe audio messages.
|
Transcribe audio messages.
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'is not allowed to transcribe audio messages')
|
f'is not allowed to transcribe audio messages')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not await self.is_within_budget(update):
|
if not await self.is_within_budget(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'reached their usage limit')
|
f'reached their usage limit')
|
||||||
await self.send_budget_reached_message(update, context)
|
await self.send_budget_reached_message(update, context)
|
||||||
return
|
return
|
||||||
@@ -260,7 +263,7 @@ class ChatGPTTelegramBot:
|
|||||||
try:
|
try:
|
||||||
audio_track = AudioSegment.from_file(filename)
|
audio_track = AudioSegment.from_file(filename)
|
||||||
audio_track.export(filename_mp3, format="mp3")
|
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})')
|
f'(id: {update.message.from_user.id})')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -352,13 +355,13 @@ class ChatGPTTelegramBot:
|
|||||||
React to incoming messages and respond accordingly.
|
React to incoming messages and respond accordingly.
|
||||||
"""
|
"""
|
||||||
if not await self.is_allowed(update):
|
if not await self.is_allowed(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'is not allowed to use the bot')
|
f'is not allowed to use the bot')
|
||||||
await self.send_disallowed_message(update, context)
|
await self.send_disallowed_message(update, context)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not await self.is_within_budget(update):
|
if not await self.is_within_budget(update):
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '\
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id}) '
|
||||||
f'reached their usage limit')
|
f'reached their usage limit')
|
||||||
await self.send_budget_reached_message(update, context)
|
await self.send_budget_reached_message(update, context)
|
||||||
return
|
return
|
||||||
@@ -629,7 +632,7 @@ class ChatGPTTelegramBot:
|
|||||||
if await self.is_user_in_group(update, user):
|
if await self.is_user_in_group(update, user):
|
||||||
logging.info(f'{user} is a member. Allowing group chat message...')
|
logging.info(f'{user} is a member. Allowing group chat message...')
|
||||||
return True
|
return True
|
||||||
logging.info(f'Group chat messages from user {update.message.from_user.name} '\
|
logging.info(f'Group chat messages from user {update.message.from_user.name} '
|
||||||
f'(id: {update.message.from_user.id}) are not allowed')
|
f'(id: {update.message.from_user.id}) are not allowed')
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@@ -717,7 +720,7 @@ class ChatGPTTelegramBot:
|
|||||||
return True
|
return True
|
||||||
logging.warning('Monthly guest budget for group chats used up.')
|
logging.warning('Monthly guest budget for group chats used up.')
|
||||||
return False
|
return False
|
||||||
logging.info(f'Group chat messages from user {update.message.from_user.name} '\
|
logging.info(f'Group chat messages from user {update.message.from_user.name} '
|
||||||
f'(id: {update.message.from_user.id}) are not allowed')
|
f'(id: {update.message.from_user.id}) are not allowed')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user