Bot Message Routing

Route messages from different chat contexts (DMs, groups, channels) to specific AI agents.

Quick Start

  1. Define your agents
    python
    from praisonaiagents import Agent
    
    personal_agent = Agent(name="personal", instructions="Handle personal questions.")
    support_agent = Agent(name="support", instructions="Handle support requests.")
    personal_agent.start("Route DMs to me, group messages to support.")
  2. Serve with routing
    bash
    praisonai bot serve

    The user messages in a DM or group; the gateway matches the chat context and forwards the turn to the configured agent.

routes: is the preferred field name for gateway routing configuration — gateway runtime accepts both routing: and routes: interchangeably in YAML.

Diagram

Bot Routing

📋 Incoming Message

🔀 Router

🤖 Matched Agent

✅ Reply

Message routing lets you send different types of messages to different AI agents. For example, direct messages go to your personal assistant, while group messages go to a support agent.

How Routing Works

The user sends a message; the gateway detects the chat context, looks up the matching route, and forwards the turn to that agent.

Diagram
AgentGatewayUserAgentGatewayUserMessage (DM / group / channel)detect_chat_type()Forward to routed agentResponse
  1. Message Arrives

    A user sends a message on Telegram, Discord, or Slack.

  2. Context Detection

    The gateway detects where the message came from — a DM, group, or channel.

  3. Route Lookup

    The gateway checks the routes table in gateway.yaml for that context.

  4. Agent Handles Message

    The matched agent receives the message and responds.

A user messages from a DM or group; the gateway detects the context, picks the matching agent, and returns its reply.

Diagram
AgentRoutesGatewayUserAgentRoutesGatewayUserMessage (DM or group)Detect chat contextLook up contextMatched agentForward messageResponseReply

Route Contexts

ContextDescriptionExample
dmDirect / private messageUser sends a DM to your bot
groupGroup chat messageMessage in a Telegram group or Discord server
channelChannel messageMessage in a Slack channel
defaultFallback for any unmatched contextAny message that doesn’t match above

Configuration

Define routes in the channels section of gateway.yaml:

yaml
channels:
  telegram:
    token: ${TELEGRAM_BOT_TOKEN}
    routes:
      dm: personal        # DMs → personal agent
      group: support      # Groups → support agent
      default: personal   # Everything else → personal agent

  discord:
    token: ${DISCORD_BOT_TOKEN}
    routes:
      dm: personal
      group: support
      default: personal

  slack:
    token: ${SLACK_BOT_TOKEN}
    app_token: ${SLACK_APP_TOKEN}
    routes:
      dm: support
      channel: support
      default: support

  whatsapp:
    mode: web             # or: token: ${WHATSAPP_ACCESS_TOKEN}
    routes:
      dm: personal
      group: support
      default: personal

Routing Examples

Personal + Support

Route DMs to a personal assistant and group messages to support:

yaml
agents:
  personal:
    instructions: "You are a helpful personal assistant"
    model: gpt-4o-mini
  support:
    instructions: "You are a customer support agent"
    model: gpt-4o

channels:
  telegram:
    token: ${TELEGRAM_BOT_TOKEN}
    routes:
      dm: personal
      group: support
      default: personal

Single Agent

Route all messages to one agent:

yaml
agents:
  assistant:
    instructions: "You are a helpful assistant"
    model: gpt-4o-mini

channels:
  discord:
    token: ${DISCORD_BOT_TOKEN}
    routes:
      default: assistant

Multi-Channel

Different agents per platform:

yaml
agents:
  telegram_agent:
    instructions: "Telegram-optimized assistant"
    model: gpt-4o-mini
  slack_agent:
    instructions: "Slack workspace assistant"
    model: gpt-4o

channels:
  telegram:
    token: ${TELEGRAM_BOT_TOKEN}
    routes:
      default: telegram_agent
  slack:
    token: ${SLACK_BOT_TOKEN}
    routes:
      default: slack_agent

Workforce (one platform, many roles)

Multiple specialized bots on the same platform:

yaml
agents:
  cfo:
    instructions: "You are a CFO agent. Help with financial analysis and budget planning."
    model: gpt-4o-mini
  ops:
    instructions: "You are an ops agent. Help with system monitoring and deployment."
    model: gpt-4o-mini
  content:
    instructions: "You are a content agent. Help with writing and content strategy."
    model: gpt-4o-mini

channels:
  telegram_cfo:
    platform: telegram
    token: ${TELEGRAM_CFO_BOT_TOKEN}
    routes:
      default: cfo
  telegram_ops:
    platform: telegram
    token: ${TELEGRAM_OPS_BOT_TOKEN}
    routes:
      default: ops
  telegram_content:
    platform: telegram
    token: ${TELEGRAM_CONTENT_BOT_TOKEN}
    routes:
      default: content

Going further: per-peer, per-role, per-channel, per-thread or per-server bindings

When chat-type routing isn’t specific enough — e.g. one specific user, a Discord role, a specific channel id, a single thread, or a whole server — use the priority-ordered bindings surface.

Gateway Route Bindings

Route by sender, role, channel id, thread, or whole server — with priority ordering.

yaml
channels:
  telegram:
    routes:
      default: general
    bindings:
      - { peer: "12345678", agent: vip }
      - { role: support,    agent: support }
      - { channel_id: "-100999", agent: ops }
      - { thread_id: "77",  agent: escalations }         # one specific thread/forum-topic
      - { guild_id: "9001", agent: support, profile: acme } # a whole server/workspace

Routing Flow Diagram

Diagram

Telegram

Discord

Slack

DM

Group

Other

DM

Guild

Other

DM

Channel

Other

Message Received

Which Platform?

Message Context?

Message Context?

Message Context?

Personal Agent

Support Agent

Default Agent

Best Practices

Always define a default route

Set default on every channel so unexpected contexts (new group types, API changes) still reach an agent instead of failing silently.

Isolate agents by context

Route dm and group to different agents when personal and support workflows must not share memory or tools. See Platform-Aware Agents.

Use bindings for fine-grained routing

When chat-type routing is not enough, add priority-ordered Gateway Route Bindings for specific users, roles, channels, threads, or whole servers.

Scope tools per route

Pair routing with Gateway Tool Policy so public group chats cannot invoke dangerous tools available to trusted DMs.


Related