Refactor setup script to disable ping and force websocket on mautrix image

This commit is contained in:
Gemini Bot
2025-12-06 18:53:20 +00:00
parent d23968b133
commit bbeac3db44

View File

@@ -1,25 +1,15 @@
#!/bin/bash #!/bin/bash
# Beeper Bridge "Green Field" Setup Script # Setup Script for Mautrix Telegram (Self-Hosted on Beeper)
# This script sets up a Mautrix bridge for Beeper from scratch. # "The Robust Way" - No Bridge Manager, just clean Config
# It overwrites existing configurations to ensure a clean state.
BRIDGE_SERVICE=$1
if [ -z "$BRIDGE_SERVICE" ]; then
echo "Usage: ./setup_beeper_bridge.sh <service>"
echo "Example: ./setup_beeper_bridge.sh telegram"
exit 1
fi
BRIDGE_SERVICE="telegram"
DATA_DIR="data/$BRIDGE_SERVICE" DATA_DIR="data/$BRIDGE_SERVICE"
CONFIG_FILE="$DATA_DIR/config.yaml" CONFIG_FILE="$DATA_DIR/config.yaml"
REG_FILE="$DATA_DIR/registration.yaml" REG_FILE="$DATA_DIR/registration.yaml"
echo "==================================================" echo "=== Beeper Bridge Setup: $BRIDGE_SERVICE ==="
echo " Beeper Bridge Setup: $BRIDGE_SERVICE" echo "WARNING: This overwrites $DATA_DIR"
echo "=================================================="
echo "Warning: This will OVERWRITE contents in $DATA_DIR"
read -p "Continue? (y/N) " confirm read -p "Continue? (y/N) " confirm
if [[ $confirm != [yY] && $confirm != [yY][eE][sS] ]]; then if [[ $confirm != [yY] && $confirm != [yY][eE][sS] ]]; then
exit 0 exit 0
@@ -27,146 +17,78 @@ fi
mkdir -p "$DATA_DIR" mkdir -p "$DATA_DIR"
# --- Step 1: Gather Information ---
echo "" echo ""
echo "--- 1. Beeper Registration ---" echo "--- 1. Gathering Tokens ---"
# Try auto-discovery via bbctl # Try bbctl auto-discovery
if command -v bbctl &> /dev/null; then if command -v bbctl &> /dev/null; then
echo "Found 'bbctl'. Attempting to fetch registration automatically..." echo "Found 'bbctl'. Fetching registration..."
# Run bbctl and capture output. We need to filter out the "You already have..." text.
# Usually the YAML starts with "id:". We try to grep from "id:" down.
BBCTL_OUT=$(bbctl register "sh-$BRIDGE_SERVICE" 2>&1) BBCTL_OUT=$(bbctl register "sh-$BRIDGE_SERVICE" 2>&1)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
# Extract YAML: Find line starting with "id:" and everything after
YAML_CONTENT=$(echo "$BBCTL_OUT" | sed -n '/^id:/,$p') YAML_CONTENT=$(echo "$BBCTL_OUT" | sed -n '/^id:/,$p')
if [ ! -z "$YAML_CONTENT" ]; then if [ ! -z "$YAML_CONTENT" ]; then
echo "$YAML_CONTENT" > "$REG_FILE" echo "$YAML_CONTENT" > "$REG_FILE"
echo "Successfully saved registration to $REG_FILE" echo "Saved $REG_FILE"
fi
# Now we need to parse values from this file to populate config variables fi
# We use grep/sed to extract vars to avoid python dependencies fi
echo "Extracting tokens..."
# Helper function to extract value by key
get_yaml_val() {
grep "^$1:" "$REG_FILE" | sed 's/^[^:]*: *//' | tr -d '\r'
}
# Parsing
if [ -f "$REG_FILE" ]; then
get_yaml_val() { grep "^$1:" "$REG_FILE" | sed 's/^[^:]*: *//' | tr -d '\r'; }
REG_ID=$(get_yaml_val "id") REG_ID=$(get_yaml_val "id")
AS_TOKEN=$(get_yaml_val "as_token") AS_TOKEN=$(get_yaml_val "as_token")
HS_TOKEN=$(get_yaml_val "hs_token") HS_TOKEN=$(get_yaml_val "hs_token")
BOT_USERNAME=$(get_yaml_val "sender_localpart") BOT_USERNAME=$(get_yaml_val "sender_localpart")
if [ -n "$REG_ID" ]; then
echo " -> ID: $REG_ID"
fi
else
echo "Could not parse YAML from bbctl output. Falling back to manual input."
fi
else
echo "bbctl failed. Falling back to manual input."
fi
else
echo "bbctl not found in PATH. Falling back to manual input."
fi fi
# Fallback prompts if variables are still empty # Fallback
if [ -z "$REG_ID" ]; then if [ -z "$REG_ID" ]; then
echo "Manual Input Required:" echo "bbctl failed or not found. Manual input needed."
read -p "ID (e.g., 99ab27...): " REG_ID read -p "ID: " REG_ID
fi read -p "AS Token: " AS_TOKEN
while [[ -z "$REG_ID" ]]; do read -p "ID cannot be empty: " REG_ID; done read -p "HS Token: " HS_TOKEN
if [ -z "$AS_TOKEN" ]; then
read -p "AS Token (e.g., Agqb...): " AS_TOKEN
fi
while [[ -z "$AS_TOKEN" ]]; do read -p "AS Token cannot be empty: " AS_TOKEN; done
if [ -z "$HS_TOKEN" ]; then
read -p "HS Token (e.g., bR5t...): " HS_TOKEN
fi
while [[ -z "$HS_TOKEN" ]]; do read -p "HS Token cannot be empty: " HS_TOKEN; done
# Default Beeper values (can be overridden but we default to what we saw in logs)
DEFAULT_HS_URL="https://matrix.beeper.com/_hungryserv/inswe"
# Try to extract HS URL from bbctl output if possible, otherwise default
read -p "Homeserver URL [$DEFAULT_HS_URL]: " HS_URL
HS_URL=${HS_URL:-$DEFAULT_HS_URL}
DEFAULT_DOMAIN="beeper.local"
read -p "Homeserver Domain [$DEFAULT_DOMAIN]: " HS_DOMAIN
HS_DOMAIN=${HS_DOMAIN:-$DEFAULT_DOMAIN}
DEFAULT_USER="@inswe:beeper.com"
read -p "Your Admin User ID [$DEFAULT_USER]: " ADMIN_USER
ADMIN_USER=${ADMIN_USER:-$DEFAULT_USER}
if [ -z "$BOT_USERNAME" ]; then
DEFAULT_BOT="sh-${BRIDGE_SERVICE}bot" DEFAULT_BOT="sh-${BRIDGE_SERVICE}bot"
read -p "Bot Username (Localpart) [$DEFAULT_BOT]: " BOT_USERNAME read -p "Bot Username [$DEFAULT_BOT]: " BOT_USERNAME
BOT_USERNAME=${BOT_USERNAME:-$DEFAULT_BOT} BOT_USERNAME=${BOT_USERNAME:-$DEFAULT_BOT}
fi fi
echo "" echo ""
echo "--- 2. Bridge Specifics ($BRIDGE_SERVICE) ---" echo "--- 2. Telegram Credentials ---"
if [ "$BRIDGE_SERVICE" == "telegram" ]; then
read -p "Telegram API ID: " API_ID read -p "Telegram API ID: " API_ID
read -p "Telegram API Hash: " API_HASH read -p "Telegram API Hash: " API_HASH
fi
# --- Step 2: Generate registration.yaml ---
echo "" echo ""
echo "Generating registration.yaml..." echo "--- 3. Generating Config ---"
cat <<EOF > "$REG_FILE"
id: $REG_ID
url: websocket
as_token: $AS_TOKEN
hs_token: $HS_TOKEN
sender_localpart: $BOT_USERNAME
namespaces:
users:
- regex: ' @${BOT_USERNAME}_.+:${HS_DOMAIN//.\/\\./}'
exclusive: true
receive_ephemeral: true
EOF
# --- Step 3: Generate config.yaml (Hardcoded Template) ---
echo "Generating config.yaml..."
# Escape domain for regex in config if needed, but standard string is fine for config
# We use a minimal but complete config optimized for Beeper
# Config optimized for Beeper + Websocket + No Pings
cat <<EOF > "$CONFIG_FILE" cat <<EOF > "$CONFIG_FILE"
homeserver: homeserver:
address: $HS_URL address: https://matrix.beeper.com/_hungryserv/inswe
domain: $HS_DOMAIN domain: beeper.local
verify_ssl: true verify_ssl: true
software: hungry software: hungry
http_retry_count: 4 http_retry_count: 4
async_media: true async_media: true
# Force websocket usage (Beeper requirement) # Websocket is critical
websocket: true websocket: true
# Disable Ping to avoid 502 errors
ping_interval_seconds: -1
appservice: appservice:
# Dummy address, bridge uses WS
address: http://localhost:29317 address: http://localhost:29317
id: $REG_ID id: $REG_ID
bot_username: $BOT_USERNAME bot_username: $BOT_USERNAME
as_token: $AS_TOKEN as_token: $AS_TOKEN
hs_token: $HS_TOKEN hs_token: $HS_TOKEN
database: sqlite:////data/bridge.db database: sqlite:////data/bridge.db
# Community advice for Beeper: # Async transactions for Beeper
async_transactions: true async_transactions: true
bridge: bridge:
@@ -175,9 +97,9 @@ bridge:
permissions: permissions:
"*": relay "*": relay
"$HS_DOMAIN": user "beeper.local": user
"beeper.com": user "beeper.com": user
"$ADMIN_USER": admin "@inswe:beeper.com": admin
relay: relay:
enabled: true enabled: true
@@ -207,31 +129,27 @@ telegram:
app_version: "0.1" app_version: "0.1"
EOF EOF
# --- Step 4: Fix Docker Compose (Environment Override) --- echo "--- 4. Updating docker-compose.yml ---"
# We ensure MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET is set in docker-compose.yml
# This is a bit hacky but ensures we win against any config parsing issues.
COMPOSE_FILE="docker-compose.yml" # Simple clean docker-compose rewrite to avoid sed mess
if grep -q "MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET" "$COMPOSE_FILE"; then cat <<EOF > docker-compose.yml
echo "Env var already present in docker-compose.yml" services:
else telegram:
echo "Adding Websocket Environment Variable to docker-compose.yml..." container_name: beeper-telegram
# We use sed to insert environment block if missing, or append to it. image: dock.mau.dev/mautrix/telegram:latest
# Simplified: We just warn the user or append it if we find the service. restart: unless-stopped
# To be safe/lazy: We rely on the config.yaml 'websocket: true' first. volumes:
# But let's append it to the file via sed for the telegram service specifically. - ./data/telegram:/data
environment:
# Using a temporary file to construct the patch - MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET=true
sed -i '/container_name: beeper-telegram/a \ environment:\n - MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET=true' "$COMPOSE_FILE" logging:
fi driver: json-file
options:
# --- Step 5: Start --- max-size: "10m"
echo "" max-file: "3"
echo "Setup Complete." EOF
echo "Starting container..."
docker compose up -d $BRIDGE_SERVICE --force-recreate
echo "" echo ""
echo "Logs:" echo "Setup Done. Starting..."
docker compose logs -f $BRIDGE_SERVICE docker compose up -d --force-recreate telegram
docker compose logs -f telegram