Skip to content

About

Out-of-tree Telegram adapter for the Orka generic gateway protocol

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Orka Telegram gateway adapter

orka-gateway-telegram implements Orka's orka.gateway.v1 protocol outside Orka core. It maps private Telegram text messages into gateway events and stores update acknowledgements, delivery outcomes, and Telegram retry cooldowns in SQLite. Long replies are truncated at a Unicode character boundary with a visible suffix. Deleting the original Telegram message does not prevent a response from being sent.

The supported Kubernetes setup routes Telegram messages to a built-in Codex Agent on Orka's orka.harness.v2 execution path. The gateway protocol remains orka.gateway.v1; it is separate from the Agent's harness protocol.

Orka compatibility

This setup targets Orka main at 2064c42. Use an existing Orka installation with:

  • --controller-mode=harness-v2, --gateway-enabled=true, and the current CRDs;
  • --watch-namespace set to one existing namespace labeled orka.ai/controller-mode=harness-v2;
  • a configured, digest-pinned Codex ACP runtime image and a controller-managed provider authentication proxy;
  • a model available through that provider proxy.

Orka requires its Gateway, GatewayBinding, and Agent resources in the controller's watched namespace. These manifests put the adapter in that same namespace and derive its ingress URL from the selected controller API and namespace. They do not create a namespace or change its mode claim. Follow Orka's installation and mode guidance when preparing or upgrading the controller.

The Codex Agent declares contractVersion: orka.harness.v2. Provider credentials belong to Orka's provider proxy, so the Agent has no secretRef and no temperature override. The earlier external harness v1 echo fixture is no longer part of this deployment.

Build and test

make check
make build

# Check against a local checkout of the supported Orka source.
make test-orka-compatibility ORKA_DIR=/path/to/orka

Build a local image with TAG=dev make image. Publishing requires Python 3, an explicit registry repository, and a clean release tag:

export IMAGE=registry.example.com/example/orka-gateway-telegram
export TAG="$(git rev-parse HEAD)"

docker buildx inspect --bootstrap
make image-push
make inspect-image

The active buildx builder is used unless BUILDER names another configured builder. PLATFORMS defaults to linux/amd64; select the architectures used by the target cluster. Published builds include provenance and an SBOM. Enable a write-once tag policy in your registry and record the published digest. A tag's spelling cannot guarantee immutability.

Configure the existing installation

The deployment needs Bash, kubectl, jq, Python 3, curl, and openssl. The manifest tests also need PyYAML. Run the following examples in one Bash shell, replacing the example values with your installation's settings:

set -euo pipefail
export KUBE_CONTEXT=my-cluster-context
export NAMESPACE=my-orka-v2-namespace
export ORKA_API_URL="http://orka-api.${NAMESPACE}.svc.cluster.local:8080"
export AGENT_MODEL=your-provider-supported-codex-model
export IMAGE=registry.example.com/example/orka-gateway-telegram
export TAG="$(git rev-parse HEAD)"

# New PVCs only: omit this to use the cluster's default StorageClass.
# For an existing PVC, read and preserve its class below before deploying.
# export STORAGE_CLASS=managed-csi

# Every command uses the selected context and namespace, including cluster-scoped reads.
k() {
  : "${KUBE_CONTEXT:?Set KUBE_CONTEXT}" "${NAMESPACE:?Set NAMESPACE}"
  kubectl --context "${KUBE_CONTEXT}" --namespace "${NAMESPACE}" "$@"
}

namespace_mode="$(k get namespace "${NAMESPACE}" \
  -o 'jsonpath={.metadata.labels.orka\.ai/controller-mode}')"
[[ "${namespace_mode}" == harness-v2 ]] || {
  printf 'Choose an existing Orka harness-v2 watched namespace.\n' >&2
  exit 1
}
k get crd gateways.gateway.orka.ai gatewaybindings.gateway.orka.ai \
  gatewayclasses.gateway.orka.ai agents.core.orka.ai

ORKA_API_URL must name the API of the controller that watches NAMESPACE. It is a base URL without credentials or a path. Rendered deployments allow HTTP only for Kubernetes Service DNS ending in .svc or .svc.cluster.local; other hosts require HTTPS. The adapter uses ${ORKA_API_URL}/api/v1/gateways/${NAMESPACE}/telegram/events. A controller watching another namespace rejects that request.

Create the adapter Secrets

Keep mode-0600 source files outside the repository. For a first installation, save the BotFather token and generate three independent secrets. Reuse the existing files when upgrading:

export SECRET_DIR="${HOME}/.config/orka-gateway-telegram"
umask 077
mkdir -p "${SECRET_DIR}"
${EDITOR:-vi} "${SECRET_DIR}/telegram-bot-token"
for name in telegram-webhook-secret orka-gateway-inbound-token orka-gateway-outbound-token; do
  if [[ ! -e "${SECRET_DIR}/${name}" ]]; then
    openssl rand -hex 32 >"${SECRET_DIR}/${name}"
  fi
done
chmod 0600 "${SECRET_DIR}"/*

k create secret generic telegram-adapter-secrets \
  --from-file=telegram-bot-token="${SECRET_DIR}/telegram-bot-token" \
  --from-file=telegram-webhook-secret="${SECRET_DIR}/telegram-webhook-secret" \
  --dry-run=client --output=yaml | k apply -f -

for direction in inbound outbound; do
  k create secret generic "telegram-gateway-${direction}" \
    --from-file=token="${SECRET_DIR}/orka-gateway-${direction}-token" \
    --dry-run=client --output=yaml | k apply -f -
  k label secret "telegram-gateway-${direction}" \
    "gateway.orka.ai/${direction}-auth=true" \
    gateway.orka.ai/gateway-name=telegram --overwrite
  k annotate secret "telegram-gateway-${direction}" \
    gateway.orka.ai/gateway-name=telegram --overwrite
done

The generated Secret manifests go directly to Kubernetes. Do not print or commit them. The adapter mounts these four files through TELEGRAM_BOT_TOKEN_FILE, TELEGRAM_WEBHOOK_SECRET_FILE, ORKA_GATEWAY_INBOUND_TOKEN_FILE, and ORKA_GATEWAY_OUTBOUND_TOKEN_FILE. A secret value and its corresponding *_FILE variable are mutually exclusive.

Deploy the adapter

The adapter uses one replica, a Recreate rollout, and one ReadWriteOnce PVC. It runs as a non-root user with a read-only root filesystem, no ServiceAccount token, and read-only Secret mounts. Keep the replica count at one while SQLite owns durable state. A new PVC uses the cluster's default StorageClass unless STORAGE_CLASS is set.

For an in-place upgrade, preserve the existing PVC's actual StorageClass before rendering or deploying. The original base explicitly set storageClassName: managed-csi. Omitting STORAGE_CLASS on that upgrade makes kubectl apply try to remove an immutable field, so the PVC update fails. Read the existing value and keep it exported for subsequent deployments:

STORAGE_CLASS="$(k get pvc/orka-gateway-telegram-data -o 'jsonpath={.spec.storageClassName}')"
: "${STORAGE_CLASS:?Preserve the named StorageClass of the existing PVC}"
export STORAGE_CLASS

For an existing bot, preserve its SQLite history before deploying. Moving from the old adapter namespace to Orka's watched namespace creates a different PVC, even when its name is unchanged. Stop incoming traffic and the old adapter, take a consistent SQLite backup, and restore it into a pre-created orka-gateway-telegram-data PVC in the target namespace before starting the new adapter. Preserve the database and any required WAL state, file ownership for UID/GID 65532, and the existing bot and gateway credentials. Retain the original PVC for recovery. Starting with an empty database loses duplicate-delivery protection; never run the old and new adapters for the same bot at the same time.

For an in-place upgrade, also preserve the public adapter URL before rendering. Read the current webhook URL from the existing ConfigMap:

webhook_url="$(k get configmap/orka-gateway-telegram -o 'jsonpath={.data.TELEGRAM_WEBHOOK_URL}')"
[[ "${webhook_url}" == https://*/telegram/webhook ]] || {
  printf 'Set ADAPTER_URL to the existing public HTTPS adapter base URL before upgrading.\n' >&2
  exit 1
}
export ADAPTER_URL="${webhook_url%/telegram/webhook}"

If the old ConfigMap has no webhook URL, set and export ADAPTER_URL to the bot's existing public endpoint instead. This lets startup re-register the webhook with max_connections: 1 for ordered updates. Leave ADAPTER_URL empty only during a first installation's bootstrap.

Render the complete non-secret manifests for inspection, then deploy:

export MANIFEST_DIR="$(mktemp -d)"
make render-manifests >"${MANIFEST_DIR}/adapter.yaml"
make deploy

Rendering is local and does not contact Kubernetes. Deployment verifies the existing namespace's mode before applying resources. The base exposes a ClusterIP Service on port 8080. Its /healthz and /readyz probes do not need the bearer token required by the gateway protocol routes.

For a stable endpoint, configure an HTTPS ingress or tunnel forwarding to http://orka-gateway-telegram:8080 in this namespace, then set ADAPTER_URL to its public HTTPS base URL. For temporary validation, the optional tunnel below supplies that URL.

Optional temporary Quick Tunnel

The existing pinned cloudflared image can expose the adapter without a local port-forward:

./scripts/render-manifests.sh tunnel >"${MANIFEST_DIR}/tunnel.yaml"
k apply -f "${MANIFEST_DIR}/tunnel.yaml"
k rollout status deployment/telegram-quick-tunnel --timeout=5m
k logs deployment/telegram-quick-tunnel --tail=100

Copy the generated public URL, without a path or trailing slash:

export ADAPTER_URL=https://replace-with-generated-host.trycloudflare.com

A Quick Tunnel hostname changes when its Pod is recreated. Use a stable HTTPS ingress or named tunnel for persistent operation. See the Cloudflare Quick Tunnel documentation for its limits.

Connect the Codex Agent

ADAPTER_URL must be a public HTTPS base URL. Rendering and live validation reject local and Kubernetes Service names, private addresses, and special-purpose IP ranges that Orka disallows for direct endpoints. Rendering does not resolve DNS; Orka checks every resolved address when connecting. The optional public check in live validation checks every DNS address and pins curl to those addresses, with proxies and curl's default config disabled.

Set the exact numeric bot, private chat, and sender IDs. The bot ID is the non-secret numeric prefix before the colon in a valid BotFather token. In a private conversation, the chat and sender IDs normally match:

export TELEGRAM_ACCOUNT_ID="$(cut -d: -f1 <"${SECRET_DIR}/telegram-bot-token")"
export TELEGRAM_CHAT_ID=123456789
export TELEGRAM_SENDER_ID=123456789
: "${ADAPTER_URL:?Set the public HTTPS adapter base URL}"

# GatewayClass is shared and cluster-scoped. Reuse this class if already installed.
k apply -k deploy/cluster
k annotate secret telegram-gateway-outbound \
  gateway.orka.ai/adapter-endpoint="${ADAPTER_URL%/}" --overwrite

./scripts/render-manifests.sh routing >"${MANIFEST_DIR}/routing.yaml"
k apply -f "${MANIFEST_DIR}/routing.yaml"

config_patch="$(jq -cn --arg url "${ADAPTER_URL%/}/telegram/webhook" \
  '{data:{TELEGRAM_WEBHOOK_URL:$url}}')"
k patch configmap orka-gateway-telegram --type=merge --patch "${config_patch}"
unset config_patch
k rollout restart deployment/orka-gateway-telegram
k rollout status deployment/orka-gateway-telegram --timeout=5m

The routing manifests create Agent/telegram-ai, Gateway/telegram, and GatewayBinding/telegram-ai. The binding permits only the selected bot, chat, and sender, and queues messages while a turn is active. The class is GatewayClass/telegram-chat-live-c65ebad8; its existing name is retained for installations that already share it.

Keep ADAPTER_URL exported for later make deploy commands so rendering preserves the webhook URL. After a Quick Tunnel URL changes, update the existing route, Secret endpoint annotation, and webhook together:

ADAPTER_URL="$(./scripts/reconcile-quick-tunnel.sh)"
export ADAPTER_URL

The reconciler requires KUBE_CONTEXT, NAMESPACE, an existing harness-v2 mode claim, and the routing objects from the previous step. It prints only the new public URL.

Verify the route

ORKA_GATEWAY_OUTBOUND_TOKEN_FILE="${SECRET_DIR}/orka-gateway-outbound-token" \
  make live-validate

Validation checks the adapter Deployment, PVC, Service, and Secret object presence; authenticated protocol health and capabilities through a temporary local port-forward; and current-generation GatewayClass, Agent, Gateway, and GatewayBinding readiness. It checks the Codex v2 Agent configuration and that the ingress URL names the selected namespace. It does not read Kubernetes Secret data or send Telegram messages.

Then send a private message from the configured Telegram account. Verify that it produces a reply and advances the binding's activity timestamps:

k get gatewaybinding/telegram-ai \
  -o 'jsonpath={.status.lastInboundActivity}{" -> "}{.status.lastOutboundActivity}{"\n"}'
k get tasks

Inspect session transcripts through Orka's API or dashboard.

Configuration and upgrades

For local execution, copy .env.example and provide the full Orka ingress URL plus the four secret file paths. The file is an example; the adapter reads its configuration from the process environment.

Variable Deployment value Purpose
LISTEN_ADDRESS :8080 Adapter listener
DATABASE_PATH /data/telegram-adapter.db Durable SQLite database
TELEGRAM_API_BASE_URL https://api.telegram.org Telegram API, overridable for trusted tests
TELEGRAM_WEBHOOK_URL Derived from ADAPTER_URL, or empty during bootstrap Full Telegram callback URL
TELEGRAM_DROP_PENDING_UPDATES false Webhook registration behavior
ORKA_GATEWAY_INGRESS_URL Derived from ORKA_API_URL and NAMESPACE Selected Orka Gateway events endpoint
TELEGRAM_CONFORMANCE_CHAT_ID 0 Chat for explicitly requested delivery conformance checks
REQUEST_TIMEOUT 15s Outbound request timeout
SHUTDOWN_TIMEOUT 20s Graceful shutdown deadline

Schema version 2 adds stable idempotency indexes; version 3 adds durable Telegram cooldown timestamps. Current Orka uses equal deliveryId and idempotencyId values, which the upgrade maps to existing delivery records. Older callers using distinct values must retry the original deliveryId once after upgrading before rotating that ID, because schema version 1 did not store their distinct idempotency ID. Delivery records remain durable without time-based pruning.

Back up SQLite consistently with its WAL before upgrading. The schema migrations are forward-only. To run an older adapter after a migration, restore its matching pre-upgrade database snapshot first. Keep the PVC during maintenance and cleanup.

Rotate Telegram and Orka tokens independently, update their Secret files, and restart the adapter so it reloads them. When changing a public endpoint, update the webhook, Gateway endpoint, and outbound Secret endpoint annotation together. Provider credentials continue to be managed by Orka's provider proxy.

Retire the legacy echo fixture

Applying the new manifests does not remove objects installed from the old deploy/fixtures/ directory. Keep the echo resources listed below, their previous manifests and image, the protected runtime token file and Secret, and the pre-upgrade SQLite/WAL backup while rollback is still needed. A rollback to the v1 echo fixture also needs its compatible Orka controller version.

After the v2 route checks confirm a Codex reply and activity on GatewayBinding/telegram-ai, and rollback is no longer needed, select the namespace containing the old fixture. The original manifests used orka-gateway-telegram, which may differ from the new NAMESPACE. Remove the old binding first to stop new echo work:

export LEGACY_NAMESPACE=orka-gateway-telegram
legacy_k() {
  kubectl --context "${KUBE_CONTEXT:?Set KUBE_CONTEXT}" \
    --namespace "${LEGACY_NAMESPACE:?Set the old fixture namespace}" "$@"
}
legacy_k delete gatewaybinding/telegram-echo --ignore-not-found

Let existing echo tasks finish or cancel them before removing their runtime. Confirm that no other Agents use telegram-echo-runtime, then remove the legacy Agent and runtime registration, followed by the Deployment, Service, and token Secret:

legacy_k delete agent/telegram-echo --ignore-not-found
legacy_k delete agentruntime/telegram-echo-runtime --ignore-not-found
legacy_k delete deployment/telegram-echo-runtime service/telegram-echo-runtime --ignore-not-found
legacy_k delete secret/telegram-echo-runtime-token --ignore-not-found

The old fixture kustomization also contains Deployment/telegram-quick-tunnel, which the current route may reuse. Use the named deletions above. Preserve Gateway/telegram, the shared GatewayClass, the current Agent and binding, adapter credentials, both namespaces, and every orka-gateway-telegram-data PVC.

Cleanup

Remove the bot's webhook before retiring its public endpoint. Keep the bot token out of command arguments by using a protected temporary curl config:

delete_webhook_config="$(mktemp)"
chmod 0600 "${delete_webhook_config}"
bot_token="$(tr -d '\r\n' <"${SECRET_DIR}/telegram-bot-token")"
printf 'url = "https://api.telegram.org/bot%s/deleteWebhook"\nrequest = "POST"\nsilent\nshow-error\nfail\n' \
  "${bot_token}" >"${delete_webhook_config}"
unset bot_token
curl --config "${delete_webhook_config}"
rm -f "${delete_webhook_config}"

k delete gatewaybinding/telegram-ai gateway/telegram agent/telegram-ai --ignore-not-found
k delete deployment/telegram-quick-tunnel --ignore-not-found
k delete deployment/orka-gateway-telegram service/orka-gateway-telegram \
  configmap/orka-gateway-telegram serviceaccount/orka-gateway-telegram --ignore-not-found
k delete secret/telegram-adapter-secrets secret/telegram-gateway-inbound \
  secret/telegram-gateway-outbound --ignore-not-found

This retains the adapter PVC, the shared GatewayClass, and the Orka namespace. Remove shared resources or persistent data only as a separate, deliberate operation.

Manifest layout

Path Purpose
deploy/ Adapter Deployment, Service, identity, configuration, and PVC
deploy/routing/ One Codex v2 Agent, Gateway, and private-chat binding
deploy/cluster/ Shared, cluster-scoped GatewayClass
deploy/tunnel/ Optional temporary Quick Tunnel
scripts/render-manifests.sh Local namespace, image, endpoint, and model rendering

The renderer's default adapter mode requires NAMESPACE, ORKA_API_URL, IMAGE, and TAG, with optional ADAPTER_URL and STORAGE_CLASS. routing requires NAMESPACE, ADAPTER_URL, AGENT_MODEL, and the three Telegram identity variables. tunnel requires only NAMESPACE. KUBECTL selects the kubectl binary. Apply rendered files; the raw bases contain placeholders.

About

Out-of-tree Telegram adapter for the Orka generic gateway protocol

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages