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