mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 06:05:12 +01:00
refactored budget_type to budget_period
added descriptions to README
This commit is contained in:
@@ -55,8 +55,9 @@ Customize the configuration by copying `.env.example` and renaming it to `.env`,
|
|||||||
| `ENABLE_QUOTING` | Whether to enable message quoting in private chats | true |
|
| `ENABLE_QUOTING` | Whether to enable message quoting in private chats | true |
|
||||||
| `ENABLE_IMAGE_GENERATION` | Whether to enable image generation via the `/image` command | true |
|
| `ENABLE_IMAGE_GENERATION` | Whether to enable image generation via the `/image` command | true |
|
||||||
| `ENABLE_TRANSCRIPTION` | Whether to enable transcriptions of audio and video messages | true |
|
| `ENABLE_TRANSCRIPTION` | Whether to enable transcriptions of audio and video messages | true |
|
||||||
| `MONTHLY_USER_BUDGETS` | A comma-separated list of $-amounts per user from list `ALLOWED_TELEGRAM_USER_IDS` to set custom usage limit of OpenAI API costs for each. **Note**: by default, *no limits* for anyone (`*`) | `*` |
|
| `BUDGET_PERIOD` | Determines the time frame all budgets are applied to. Available periods: `daily` *(resets budget every day)*, `monthly` *(resets budgets on the first of each month)*, `all-time` *(never resets budget)* | `monthly` |
|
||||||
| `MONTHLY_GUEST_BUDGET` | $-amount as usage limit for all guest users. Guest users are users in group chats that are not in the `ALLOWED_TELEGRAM_USER_IDS` list. Value is ignored if no usage limits are set in user budgets (`MONTHLY_USER_BUDGETS`="*") | `100.0` |
|
| `USER_BUDGETS` | A comma-separated list of $-amounts per user from list `ALLOWED_TELEGRAM_USER_IDS` to set custom usage limit of OpenAI API costs for each. For `*`- user lists the first `USER_BUDGETS` value is given to every user. **Note**: by default, *no limits* for any user (`*`) | `*` |
|
||||||
|
| `GUEST_BUDGET` | $-amount as usage limit for all guest users. Guest users are users in group chats that are not in the `ALLOWED_TELEGRAM_USER_IDS` list. Value is ignored if no usage limits are set in user budgets (`USER_BUDGETS`="*") | `100.0` |
|
||||||
| `PROXY` | Proxy to be used for OpenAI and Telegram bot (e.g. `http://localhost:8080`) | - |
|
| `PROXY` | Proxy to be used for OpenAI and Telegram bot (e.g. `http://localhost:8080`) | - |
|
||||||
| `OPENAI_MODEL` | The OpenAI model to use for generating responses | `gpt-3.5-turbo` |
|
| `OPENAI_MODEL` | The OpenAI model to use for generating responses | `gpt-3.5-turbo` |
|
||||||
| `ASSISTANT_PROMPT` | A system message that sets the tone and controls the behavior of the assistant | `You are a helpful assistant.` |
|
| `ASSISTANT_PROMPT` | A system message that sets the tone and controls the behavior of the assistant | `You are a helpful assistant.` |
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ def main():
|
|||||||
# remove support for old budget names at some point in the future
|
# remove support for old budget names at some point in the future
|
||||||
if os.environ.get('MONTHLY_USER_BUDGETS') is not None:
|
if os.environ.get('MONTHLY_USER_BUDGETS') is not None:
|
||||||
logging.warning('The environment variable MONTHLY_USER_BUDGETS is deprecated. '
|
logging.warning('The environment variable MONTHLY_USER_BUDGETS is deprecated. '
|
||||||
'Please use USER_BUDGETS with BUDGET_TYPE instead.')
|
'Please use USER_BUDGETS with BUDGET_PERIOD instead.')
|
||||||
if os.environ.get('MONTHLY_GUEST_BUDGET') is not None:
|
if os.environ.get('MONTHLY_GUEST_BUDGET') is not None:
|
||||||
logging.warning('The environment variable MONTHLY_GUEST_BUDGET is deprecated. '
|
logging.warning('The environment variable MONTHLY_GUEST_BUDGET is deprecated. '
|
||||||
'Please use GUEST_BUDGET with BUDGET_TYPE instead.')
|
'Please use GUEST_BUDGET with BUDGET_PERIOD instead.')
|
||||||
|
|
||||||
telegram_config = {
|
telegram_config = {
|
||||||
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||||
@@ -61,7 +61,7 @@ def main():
|
|||||||
'enable_quoting': os.environ.get('ENABLE_QUOTING', 'true').lower() == 'true',
|
'enable_quoting': os.environ.get('ENABLE_QUOTING', 'true').lower() == 'true',
|
||||||
'enable_image_generation': os.environ.get('ENABLE_IMAGE_GENERATION', 'true').lower() == 'true',
|
'enable_image_generation': os.environ.get('ENABLE_IMAGE_GENERATION', 'true').lower() == 'true',
|
||||||
'enable_transcription': os.environ.get('ENABLE_TRANSCRIPTION', 'true').lower() == 'true',
|
'enable_transcription': os.environ.get('ENABLE_TRANSCRIPTION', 'true').lower() == 'true',
|
||||||
'budget_type': os.environ.get('BUDGET_TYPE', 'monthly').lower(),
|
'budget_period': os.environ.get('BUDGET_PERIOD', 'monthly').lower(),
|
||||||
'user_budgets': os.environ.get('USER_BUDGETS', os.environ.get('MONTHLY_USER_BUDGETS', '*')),
|
'user_budgets': os.environ.get('USER_BUDGETS', os.environ.get('MONTHLY_USER_BUDGETS', '*')),
|
||||||
'guest_budget': float(os.environ.get('GUEST_BUDGET', os.environ.get('MONTHLY_GUEST_BUDGET', '100.0'))),
|
'guest_budget': float(os.environ.get('GUEST_BUDGET', os.environ.get('MONTHLY_GUEST_BUDGET', '100.0'))),
|
||||||
'stream': os.environ.get('STREAM', 'true').lower() == 'true',
|
'stream': os.environ.get('STREAM', 'true').lower() == 'true',
|
||||||
|
|||||||
@@ -34,13 +34,13 @@ class ChatGPTTelegramBot:
|
|||||||
"""
|
"""
|
||||||
Class representing a ChatGPT Telegram Bot.
|
Class representing a ChatGPT Telegram Bot.
|
||||||
"""
|
"""
|
||||||
# Mapping of budget type to cost type
|
# Mapping of budget period to cost period
|
||||||
budget_cost_map = {
|
budget_cost_map = {
|
||||||
"monthly":"cost_month",
|
"monthly":"cost_month",
|
||||||
"daily":"cost_today",
|
"daily":"cost_today",
|
||||||
"all-time":"cost_all_time"
|
"all-time":"cost_all_time"
|
||||||
}
|
}
|
||||||
# Mapping of budget type to a print output
|
# Mapping of budget period to a print output
|
||||||
budget_print_map = {
|
budget_print_map = {
|
||||||
"monthly": " this month",
|
"monthly": " this month",
|
||||||
"daily": " today",
|
"daily": " today",
|
||||||
@@ -65,7 +65,7 @@ class ChatGPTTelegramBot:
|
|||||||
]
|
]
|
||||||
self.disallowed_message = "Sorry, you are not allowed to use this bot. You can check out the source code at " \
|
self.disallowed_message = "Sorry, you are not allowed to use this bot. You can check out the source code at " \
|
||||||
"https://github.com/n3d1117/chatgpt-telegram-bot"
|
"https://github.com/n3d1117/chatgpt-telegram-bot"
|
||||||
self.budget_limit_message = f"Sorry, you have reached your usage limit{self.budget_print_map[config['budget_type']]}."
|
self.budget_limit_message = f"Sorry, you have reached your usage limit{self.budget_print_map[config['budget_period']]}."
|
||||||
self.usage = {}
|
self.usage = {}
|
||||||
self.last_message = {}
|
self.last_message = {}
|
||||||
|
|
||||||
@@ -128,9 +128,9 @@ class ChatGPTTelegramBot:
|
|||||||
f"💰 For a total amount of ${current_cost['cost_month']:.2f}"
|
f"💰 For a total amount of ${current_cost['cost_month']:.2f}"
|
||||||
# text_budget filled with conditional content
|
# text_budget filled with conditional content
|
||||||
text_budget = "\n\n"
|
text_budget = "\n\n"
|
||||||
budget_type =self.config['budget_type']
|
budget_period =self.config['budget_period']
|
||||||
if remaining_budget < float('inf'):
|
if remaining_budget < float('inf'):
|
||||||
text_budget += f"You have a remaining budget of ${remaining_budget:.2f}{self.budget_print_map[budget_type]}.\n"
|
text_budget += f"You have a remaining budget of ${remaining_budget:.2f}{self.budget_print_map[budget_period]}.\n"
|
||||||
# add OpenAI account information for admin request
|
# add OpenAI account information for admin request
|
||||||
if self.is_admin(update):
|
if self.is_admin(update):
|
||||||
text_budget += f"Your OpenAI account was billed ${self.openai.get_billing_current_month():.2f} this month."
|
text_budget += f"Your OpenAI account was billed ${self.openai.get_billing_current_month():.2f} this month."
|
||||||
@@ -698,15 +698,15 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
# Get budget for users
|
# Get budget for users
|
||||||
user_budget = self.get_user_budget(update)
|
user_budget = self.get_user_budget(update)
|
||||||
budget_type = self.config['budget_type']
|
budget_period = self.config['budget_period']
|
||||||
if user_budget is not None:
|
if user_budget is not None:
|
||||||
cost = self.usage[user_id].get_current_cost()[self.budget_cost_map[budget_type]]
|
cost = self.usage[user_id].get_current_cost()[self.budget_cost_map[budget_period]]
|
||||||
return user_budget - cost
|
return user_budget - cost
|
||||||
|
|
||||||
# Get budget for guests
|
# Get budget for guests
|
||||||
if 'guests' not in self.usage:
|
if 'guests' not in self.usage:
|
||||||
self.usage['guests'] = UsageTracker('guests', 'all guest users in group chats')
|
self.usage['guests'] = UsageTracker('guests', 'all guest users in group chats')
|
||||||
cost = self.usage['guests'].get_current_cost()[self.budget_cost_map[budget_type]]
|
cost = self.usage['guests'].get_current_cost()[self.budget_cost_map[budget_period]]
|
||||||
return self.config['guest_budget'] - cost
|
return self.config['guest_budget'] - cost
|
||||||
|
|
||||||
def is_within_budget(self, update: Update) -> bool:
|
def is_within_budget(self, update: Update) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user