The gateway pattern
Today the agent is glued to Telegram. Tomorrow you might want it on a web page, on Slack, or as a CLI. The gateway pattern keeps the agent logic in one function and lets any delivery channel call into it.
# Telegram side
async def on_telegram_message(update, context):
user_id = str(update.effective_user.id)
reply = run_agent_turn(user_id, update.message.text)
await update.message.reply_text(reply)
# Flask HTTP side
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.post('/chat')
def on_http_message():
body = request.get_json()
user_id = body['user_id']
reply = run_agent_turn(user_id, body['text'])
return jsonify({'reply': reply})Both channels do the exact same thing. They extract a user id and a text, call run_agent_turn, and hand the reply back in their own format.
Yes, as long as you decide to key sessions by user id instead of by channel. That gives you one continuous assistant that follows the user wherever they are. If you want separation per channel, combine the two values into the session key. The choice is yours.
Quiz: Quiz
Loading practice…