Skip to content

Plugins info #683

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def main():
# Setup and run ChatGPT and Telegram bot
plugin_manager = PluginManager(config=plugin_config)
openai_helper = OpenAIHelper(config=openai_config, plugin_manager=plugin_manager)
telegram_bot = ChatGPTTelegramBot(config=telegram_config, openai=openai_helper)
telegram_bot = ChatGPTTelegramBot(config=telegram_config, openai=openai_helper,
plugin_manager=plugin_manager)
telegram_bot.run()


Expand Down
1 change: 1 addition & 0 deletions bot/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, config):
'iplocation': IpLocationPlugin,
}
self.plugins = [plugin_mapping[plugin]() for plugin in enabled_plugins if plugin in plugin_mapping]
self.active_plugins_names = [plugin for plugin in enabled_plugins if plugin in plugin_mapping]

def get_functions_specs(self):
"""
Expand Down
48 changes: 35 additions & 13 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pydub import AudioSegment
from PIL import Image

from plugin_manager import PluginManager
from utils import is_group_chat, get_thread_id, message_text, wrap_with_indicator, split_into_chunks, \
edit_message_with_retry, get_stream_cutoff_values, is_allowed, get_remaining_budget, is_admin, is_within_budget, \
get_reply_to_message_id, add_chat_request_to_usage_tracker, error_handler, is_direct_result, handle_direct_result, \
Expand All @@ -29,17 +30,19 @@ class ChatGPTTelegramBot:
Class representing a ChatGPT Telegram Bot.
"""

def __init__(self, config: dict, openai: OpenAIHelper):
def __init__(self, config: dict, openai: OpenAIHelper, plugin_manager: PluginManager):
"""
Initializes the bot with the given configuration and GPT bot object.
:param config: A dictionary containing the bot configuration
:param openai: OpenAIHelper object
"""
self.config = config
self.openai = openai
self.plugin_manager = plugin_manager
bot_language = self.config['bot_language']
self.commands = [
BotCommand(command='help', description=localized_text('help_description', bot_language)),
BotCommand(command='plugins', description=localized_text('get_plugins', bot_language)),
BotCommand(command='reset', description=localized_text('reset_description', bot_language)),
BotCommand(command='stats', description=localized_text('stats_description', bot_language)),
BotCommand(command='resend', description=localized_text('resend_description', bot_language))
Expand Down Expand Up @@ -78,6 +81,24 @@ async def help(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
)
await update.message.reply_text(help_text, disable_web_page_preview=True)

async def plugins(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
"""
Shows the active plugins menu.
"""
bot_language = self.config['bot_language']
help_text = (
localized_text('Activate plugins:', bot_language) +
'\n\n' +
" ".join(self.plugin_manager.active_plugins_names) +
"\n\n" +
localized_text("How to use?", bot_language) +
'\n' +
localized_text(
'plugin_name + text (e.g. youtube_audio_extractor https://www.youtube.com/watch?v=lYBUbBu4W08)',
bot_language)
)
await update.message.reply_text(help_text, disable_web_page_preview=True)

async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""
Returns token usage statistics for current day and month.
Expand Down Expand Up @@ -107,14 +128,14 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_messages, chat_token_length = self.openai.get_conversation_stats(chat_id)
remaining_budget = get_remaining_budget(self.config, self.usage, update)
bot_language = self.config['bot_language']

text_current_conversation = (
f"*{localized_text('stats_conversation', bot_language)[0]}*:\n"
f"{chat_messages} {localized_text('stats_conversation', bot_language)[1]}\n"
f"{chat_token_length} {localized_text('stats_conversation', bot_language)[2]}\n"
"----------------------------\n"
)

# Check if image generation is enabled and, if so, generate the image statistics for today
text_today_images = ""
if self.config.get('enable_image_generation', False):
Expand All @@ -127,7 +148,7 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
text_today_tts = ""
if self.config.get('enable_tts_generation', False):
text_today_tts = f"{characters_today} {localized_text('stats_tts', bot_language)}\n"

text_today = (
f"*{localized_text('usage_today', bot_language)}:*\n"
f"{tokens_today} {localized_text('stats_tokens', bot_language)}\n"
Expand All @@ -139,7 +160,7 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
f"{localized_text('stats_total', bot_language)}{current_cost['cost_today']:.2f}\n"
"----------------------------\n"
)

text_month_images = ""
if self.config.get('enable_image_generation', False):
text_month_images = f"{images_month} {localized_text('stats_images', bot_language)}\n"
Expand All @@ -151,7 +172,7 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
text_month_tts = ""
if self.config.get('enable_tts_generation', False):
text_month_tts = f"{characters_month} {localized_text('stats_tts', bot_language)}\n"

# Check if image generation is enabled and, if so, generate the image statistics for the month
text_month = (
f"*{localized_text('usage_month', bot_language)}:*\n"
Expand Down Expand Up @@ -471,9 +492,9 @@ async def vision(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
(prompt is not None and not prompt.lower().startswith(trigger_keyword.lower())):
logging.info('Vision coming from group chat with wrong keyword, ignoring...')
return

image = update.message.effective_attachment[-1]


async def _execute():
bot_language = self.config['bot_language']
Expand All @@ -492,14 +513,14 @@ async def _execute():
parse_mode=constants.ParseMode.MARKDOWN
)
return

# convert jpg from telegram to png as understood by openai

temp_file_png = io.BytesIO()

try:
original_image = Image.open(temp_file)

original_image.save(temp_file_png, format='PNG')
logging.info(f'New vision request received from user {update.message.from_user.name} '
f'(id: {update.message.from_user.id})')
Expand All @@ -511,8 +532,8 @@ async def _execute():
reply_to_message_id=get_reply_to_message_id(self.config, update),
text=localized_text('media_type_fail', bot_language)
)



user_id = update.message.from_user.id
if user_id not in self.usage:
Expand Down Expand Up @@ -597,7 +618,7 @@ async def _execute():
if tokens != 'not_finished':
total_tokens = int(tokens)


else:

try:
Expand Down Expand Up @@ -1061,6 +1082,7 @@ def run(self):
application.add_handler(CommandHandler('tts', self.tts))
application.add_handler(CommandHandler('start', self.help))
application.add_handler(CommandHandler('stats', self.stats))
application.add_handler(CommandHandler('plugins', self.plugins))
application.add_handler(CommandHandler('resend', self.resend))
application.add_handler(CommandHandler(
'chat', self.prompt, filters=filters.ChatType.GROUP | filters.ChatType.SUPERGROUP)
Expand Down
3 changes: 2 additions & 1 deletion translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"answer_with_chatgpt":"Answer with ChatGPT",
"ask_chatgpt":"Ask ChatGPT",
"loading":"Loading...",
"function_unavailable_in_inline_mode": "This function is unavailable in inline mode"
"function_unavailable_in_inline_mode": "This function is unavailable in inline mode",
"get_plugins": "Get active plugins and see how to use them"
},
"ar": {
"help_description":"عرض رسالة المساعدة",
Expand Down