From e9d934a3849c27a30237c5120b2a93dc617191aa Mon Sep 17 00:00:00 2001 From: yushen Date: Tue, 10 Feb 2026 17:07:45 +0800 Subject: [PATCH] chore(telegram): add webhook setup script Co-Authored-By: Claude Opus 4.6 --- scripts/set-telegram-webhook.sh | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 scripts/set-telegram-webhook.sh diff --git a/scripts/set-telegram-webhook.sh b/scripts/set-telegram-webhook.sh new file mode 100755 index 00000000..cbea7f14 --- /dev/null +++ b/scripts/set-telegram-webhook.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# +# Set Telegram Bot Webhook +# +# Usage: +# ./scripts/set-telegram-webhook.sh +# +# Example: +# ./scripts/set-telegram-webhook.sh https://your-domain.ngrok-free.dev +# +# Reads TELEGRAM_BOT_TOKEN and TELEGRAM_WEBHOOK_SECRET_TOKEN from .env + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ENV_FILE="$SCRIPT_DIR/../.env" + +if [ ! -f "$ENV_FILE" ]; then + echo "Error: .env file not found at $ENV_FILE" + exit 1 +fi + +source "$ENV_FILE" + +if [ -z "${TELEGRAM_BOT_TOKEN:-}" ]; then + echo "Error: TELEGRAM_BOT_TOKEN not set in .env" + exit 1 +fi + +WEBHOOK_BASE_URL="${1:-}" + +if [ -z "$WEBHOOK_BASE_URL" ]; then + echo "Usage: $0 " + echo "" + echo "Example:" + echo " $0 https://your-domain.ngrok-free.dev" + exit 1 +fi + +# Remove trailing slash +WEBHOOK_BASE_URL="${WEBHOOK_BASE_URL%/}" +WEBHOOK_URL="${WEBHOOK_BASE_URL}/telegram/webhook" + +echo "Bot Token: ${TELEGRAM_BOT_TOKEN:0:10}..." +echo "Secret Token: ${TELEGRAM_WEBHOOK_SECRET_TOKEN:0:8}..." +echo "Webhook URL: $WEBHOOK_URL" +echo "" + +# Set webhook +echo "=> Setting webhook..." +RESPONSE=$(curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setWebhook" \ + -d "url=${WEBHOOK_URL}" \ + -d "secret_token=${TELEGRAM_WEBHOOK_SECRET_TOKEN:-}") + +echo "$RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$RESPONSE" + +echo "" +echo "=> Verifying webhook info..." +INFO=$(curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getWebhookInfo") +echo "$INFO" | python3 -m json.tool 2>/dev/null || echo "$INFO"