Adding custom commands #334
Unanswered
xyzelectronic
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Hi, you can add commands in the bot/telegram_bot.py file. self.commands = [
BotCommand(command='help', description=localized_text('help_description', bot_language)),
BotCommand(command='reset', description=localized_text('reset_description', bot_language)),
BotCommand(command='image', description=localized_text('image_description', bot_language)),
BotCommand(command='stats', description=localized_text('stats_description', bot_language)),
BotCommand(command='resend', description=localized_text('resend_description', bot_language)),
BotCommand(command='prompts', description='Helpful tips for your prompts')
] and to the appliocation CommandHandler in the application = ApplicationBuilder() \
.token(self.config['token']) \
.proxy_url(self.config['proxy']) \
.get_updates_proxy_url(self.config['proxy']) \
.post_init(self.post_init) \
.concurrent_updates(True) \
.build()
application.add_handler(CommandHandler('reset', self.reset))
application.add_handler(CommandHandler('help', self.help))
application.add_handler(CommandHandler('image', self.image))
application.add_handler(CommandHandler('start', self.help))
application.add_handler(CommandHandler('stats', self.stats))
application.add_handler(CommandHandler('resend', self.resend))
application.add_handler(CommandHandler('prompts', self.prompts))
application.add_handler(CommandHandler(
'chat', self.prompt, filters=filters.ChatType.GROUP | filters.ChatType.SUPERGROUP)
) Finally you have to add a new function for your command, for example after the help function, after line 71. async def prompts(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
"""
Shows the prompts text.
"""
bot_language = self.config['bot_language']
prompts_text = (
"Context: a simple sentence to help ChatGPT understand the context you want it to be. "
"A simple way is to write “Act as a …”.\n"
"Instruction: what you want to get out of ChatGPT.\n"
"Details: optional, here you can specify the tone of voice or formatting you want.\n"
"Input: the input data you want ChatGPT to respond to, this can be included in the "
"instruction too if it’s short."
)
await update.message.reply_text(prompts_text, disable_web_page_preview=True) Then you restart your bot and it should be there, no botfather interaction required. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I think the addition of more commands would be helpful. For example an input of /prompts could generate a short guide for getting better responses, something along the lines of:
Unfortunately I only have the knowledge to get this program running, not to alter it.
Would the code be inserted into the telegram.py program and then set up through botfather?
Beta Was this translation helpful? Give feedback.
All reactions