Skip to content

Advanced Configuration#

Custom Command Handling#

Since the library is built on whatsapp-chatbot-python, you can use all the commands/filters features of the base library:

@bot.router.message(command="help")
def help_handler(notification):
help_text = (
"🤖 *WhatsApp GPT Bot* 🤖\n\n"
"Available commands:\n"
"• */help* - Show this help message\n"
"• */clear* - Clear conversation history\n"
"• */info* - Show bot info"
)
notification.answer(help_text)

# Command to clear chat history
@bot.router.message(command="clear")
def clear_history_handler(notification):
chat_id = notification.chat

# Get session data
session_data = bot.get_session_data(chat_id)

# Find system message, if it exists
system_message = None
for msg in session_data.messages:
if msg.get("role") == "system":
system_message = msg
break

# Clear messages, but keep system message
if system_message:
session_data.messages = [system_message]
else:
session_data.messages = []

# Update session
bot.update_session_data(chat_id, session_data)

notification.answer("🗑️ Chat history cleared! Let's start over.")

Bypassing GPT for specific commands#

You can create handlers that bypass GPT processing:

@bot.router.message(command="weather")
def weather_handler(notification):
notification.answer(
"🌤️ This is a stub weather response from a custom handler.\n\n"
"In a real bot, this would be a call to the weather API.\n\n"
"This handler demonstrates bypassing GPT processing."
)

Explicit GPT processing#

You can explicitly request GPT processing after processing a message:

@bot.router.message(text_message="recommend")
def recommend_handler(notification):
# Add a prefix message
notification.answer("I'll give you a recommendation. Let me think about it...")
# Request GPT processing
notification.process_with_gpt()

You can also modify the message before sending it to GPT using the custom_message parameter:

# An echo handler that forwards the modified message to GPT
@bot.router.message(command="echo")
def echo_handler(notification):
# Receive the message after the command
message_text = notification.message_text
command_parts = message_text.split(maxsplit=1)

if len(command_parts) > 1:
echo_text = command_parts[1]
notification.answer(f"You said: {echo_text}\n\nI'll ask GPT for more information...")

# Process with GPT, but only pass the actual message (without the command)
notification.process_with_gpt(custom_message=echo_text)
else:
notification.answer("Please provide text after the /echo command.")

This is useful when you want to preprocess the message before sending it to GPT, such as removing command prefixes, formatting the text, or adding context.

Advanced message processing#

# Check if the current model supports images
if bot.supports_images():
# Processing image-based workflow
pass