Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Gateway Plugins

Gateway plugins enable Duragent to communicate with messaging platforms. They run as separate processes and communicate via the Gateway Protocol (JSON Lines over stdio).

Configuration

Gateway plugins are configured in duragent.yaml:

gateways:
  # Built-in Telegram gateway
  telegram:
    enabled: true
    bot_token: ${TELEGRAM_BOT_TOKEN}

  # External gateways (subprocess plugins)
  external:
    - name: discord
      command: /usr/local/bin/duragent-discord
      args: ["--verbose"]
      env:
        DISCORD_BOT_TOKEN: ${DISCORD_BOT_TOKEN}
      restart: on_failure

    - name: custom-gateway
      command: ./my-gateway
      restart: always

External Gateway Fields

FieldTypeDefaultDescription
namestringrequiredGateway identifier
commandstringrequiredPath to gateway binary
argsarray[]Command arguments
envmap{}Environment variables
restartenumon_failurealways, on_failure, or never

Agent Routing

Routing rules determine which agent handles messages from each gateway. Rules are global and evaluated in order:

routes:
  - match:
      gateway: telegram
      sender_id: "123456789"
    agent: personal-assistant

  - match:
      gateway: telegram
      chat_type: group
    agent: group-moderator

  - match: {}    # Catch-all
    agent: default-assistant

Match Conditions

FieldDescriptionExamples
gatewayGateway nametelegram, discord
chat_typeConversation typedm, group, channel
chat_idSpecific chat ID-1001234567890
sender_idSpecific user ID123456789

All conditions in a rule must match (AND logic). First match wins. An empty match: {} acts as a catch-all.

Process Management

Duragent manages plugin lifecycle:

  • Startup — Plugins are spawned when the server starts
  • Health checks — Periodic pings to verify plugin is responsive
  • Restart — Configurable restart policy on crash
  • Shutdown — Graceful shutdown signal, then force kill
  • Orphan prevention — On Linux, plugins die when Duragent dies (via prctl)

Writing a Custom Gateway

Custom gateways implement the Gateway Protocol — JSON Lines over stdin/stdout. You can write them in any language.

Protocol Messages

Events (gateway to Duragent):

EventPurpose
readyGateway initialized
message_receivedIncoming user message
callback_queryInline keyboard button pressed
command_okCommand succeeded
command_errorCommand failed
pongResponse to health check ping
errorGateway-level error
auth_requiredAuthentication needed (e.g. QR code)
auth_successAuthentication succeeded
shutdownGateway terminating

Commands (Duragent to gateway):

CommandPurpose
send_messageSend text to a chat
send_mediaSend media (image, video, audio, document)
send_typingShow typing indicator
edit_messageEdit a previously sent message
delete_messageDelete a message
answer_callback_queryRespond to inline keyboard button press
pingHealth check
shutdownGraceful termination request

Message Metadata

Gateways provide metadata with incoming messages:

  • chat_id — Platform-specific chat identifier
  • chat_typedm, group, or channel
  • sender_id — User identifier
  • mentions_bot — Whether the bot was @mentioned
  • reply_to_bot — Whether this is a reply to the bot’s message

See the duragent-gateway-protocol crate for the full type definitions.