Recipe: Custom greetings (time of day, page, language)
Power user
Recipe: Custom greetings
Make the bot feel less robotic by varying its first reply based on context.
Time-of-day greeting
from datetime import datetime
import pytz
def respond(message, context):
msg = message.lower().strip()
# Only trigger on the first message of the session
if len(context.get("history", [])) > 1:
return None
if msg in {"hi", "hello", "hey", "hi there", "hello!"}:
london = pytz.timezone("Europe/London")
hour = datetime.now(london).hour
if hour < 12:
return "Good morning! How can I help you today?"
elif hour < 18:
return "Good afternoon! What can I help you with?"
else:
return "Good evening! How can I help?"
return None
Greeting based on the page they're on
Useful if you want the bot to lead with something relevant to the page context.
def respond(message, context):
if len(context.get("history", [])) > 1:
return None
page = context.get("page_url", "")
msg = message.lower().strip()
if msg not in {"hi", "hello", "hey"}:
return None
if "/pricing" in page:
return "Hi! Looking at our pricing — happy to help you pick the right plan. What size is your team?"
if "/blog" in page:
return "Hi! Hope you're enjoying the article. Anything I can help with?"
if "/contact" in page:
return "Hi! You're in the right place — what brings you here today?"
return "Hi! How can I help you today?"
Language detection
A simple "is this English?" check using common words:
def respond(message, context):
msg = message.lower().strip()
spanish_hints = {"hola", "buenos", "gracias", "ayuda"}
french_hints = {"bonjour", "salut", "merci", "aide"}
words = set(msg.split())
if words & spanish_hints:
return "¡Hola! ¿En qué puedo ayudarte?"
if words & french_hints:
return "Bonjour ! Comment puis-je vous aider ?"
return None
For full multilingual support, see Multi-language bots instead.
Tip
Keep greetings short. Don't lecture. The visitor is here to ask a question — let them ask it.