Rewrite setup_final.sh to use template replacement

This commit is contained in:
Gemini Bot
2025-12-06 19:08:47 +00:00
parent 87aabaae34
commit b9acaadc83

View File

@@ -1,316 +1,76 @@
#!/bin/bash #!/bin/bash
# FINAL FINAL ATTEMPT # FINAL FINAL ATTEMPT (Template Version)
# Uses user's known-good config structure, adapted for Docker + Websocket. # Uses config.template.yaml to ensure YAML syntax is perfect.
BRIDGE_SERVICE="telegram" 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"
# ... (Token extraction same as before) ... 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
fi
mkdir -p "$DATA_DIR"
# --- Token Gathering ---
if command -v bbctl &> /dev/null; then if command -v bbctl &> /dev/null; then
echo "Found 'bbctl'. Fetching registration..."
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
YAML_CONTENT=$(echo "$BBCTL_OUT" | sed -n '/^id:/,$p') YAML_CONTENT=$(echo "$BBCTL_OUT" | sed -n '/^id:/,$p')
echo "$YAML_CONTENT" > "$REG_FILE" if [ ! -z "$YAML_CONTENT" ]; then
echo "$YAML_CONTENT" > "$REG_FILE"
fi
fi fi
fi fi
get_yaml_val() { grep "^$1:" "$REG_FILE" | sed 's/^[^:]*: *//' | tr -d '\r'; } if [ -f "$REG_FILE" ]; then
REG_ID=$(get_yaml_val "id") get_yaml_val() { grep "^$1:" "$REG_FILE" | sed 's/^[^:]*: *//' | tr -d '\r'; }
AS_TOKEN=$(get_yaml_val "as_token") REG_ID=$(get_yaml_val "id")
HS_TOKEN=$(get_yaml_val "hs_token") AS_TOKEN=$(get_yaml_val "as_token")
BOT_USERNAME=$(get_yaml_val "sender_localpart") HS_TOKEN=$(get_yaml_val "hs_token")
BOT_USERNAME=$(get_yaml_val "sender_localpart")
fi
if [ -z "$REG_ID" ]; then
echo "Manual input needed."
read -p "ID: " REG_ID
read -p "AS Token: " AS_TOKEN
read -p "HS Token: " HS_TOKEN
read -p "Bot Username: " BOT_USERNAME
fi
if [ -z "$API_ID" ]; then if [ -z "$API_ID" ]; 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 fi
# USER'S CONFIG (Adapted) # --- Config Generation via Template ---
cat <<EOF > "$CONFIG_FILE" if [ ! -f "config.template.yaml" ]; then
homeserver: echo "Error: config.template.yaml not found! Please git pull."
address: https://matrix.beeper.com/_hungryserv/inswe exit 1
domain: beeper.local fi
verify_ssl: true
software: hungry
http_retry_count: 4
async_media: true
# ADDED FOR DOCKER:
websocket: true
ping_interval_seconds: -1
appservice: echo "Generating config.yaml from template..."
address: http://localhost:29317 cp config.template.yaml "$CONFIG_FILE"
hostname: 0.0.0.0
port: 29317
max_body_size: 1
database: sqlite:////data/bridge.db
id: $REG_ID
bot_username: $BOT_USERNAME
as_token: $AS_TOKEN
hs_token: $HS_TOKEN
ephemeral_events: true
# Beeper usually needs this for WS
async_transactions: true
metrics: # Replace placeholders using sed
enabled: false # We use | as delimiter to avoid issues with slashes in tokens
listen_port: 8000 sed -i "s|REPLACE_REG_ID|$REG_ID|g" "$CONFIG_FILE"
sed -i "s|REPLACE_BOT_USERNAME|$BOT_USERNAME|g" "$CONFIG_FILE"
sed -i "s|REPLACE_AS_TOKEN|$AS_TOKEN|g" "$CONFIG_FILE"
sed -i "s|REPLACE_HS_TOKEN|$HS_TOKEN|g" "$CONFIG_FILE"
sed -i "s|REPLACE_API_ID|$API_ID|g" "$CONFIG_FILE"
sed -i "s|REPLACE_API_HASH|$API_HASH|g" "$CONFIG_FILE"
manhole: echo "Config generated."
enabled: false
bridge:
username_template: ${BRIDGE_SERVICE}_{userid}
alias_template: ${BRIDGE_SERVICE}_{groupname}
displayname_template: '{displayname}'
displayname_preference:
- full name
- username
- phone number
displayname_max_length: 100
allow_avatar_remove: true
allow_contact_info: true
max_initial_member_sync: 20
max_member_count: 10000
sync_channel_members: false
skip_deleted_members: true
startup_sync: false
sync_update_limit: 0
sync_create_limit: 10
sync_deferred_create_all: true
sync_direct_chats: true
max_telegram_delete: 10
sync_matrix_state: true
allow_matrix_login: true
public_portals: false
sync_with_custom_puppets: false
sync_direct_chat_list: false
double_puppet_server_map:
beeper.com: https://matrix.beeper.com/_hungryserv/inswe
double_puppet_allow_discovery: false
# Shared secrets for https://github.com/devture/matrix-synapse-shared-secret-auth
#
# If set, custom puppets will be enabled automatically for local users
# instead of users having to find an access token and run `login-matrix`
# manually.
# If using this for other servers than the bridge's server,
# you must also set the URL in the double_puppet_server_map.
login_shared_secret_map:
"beeper.com": "as_token:hua_GgntyU8meanud1GBjx4kh8sWf2XZJrXtnrQzHJZ8Fa1mKB52mWQpA_OdwUC3"
telegram_link_preview: true
# Whether or not the !tg join command should do a HTTP request
# to resolve redirects in invite links.
invite_link_resolve: false
caption_in_message: true
image_as_file_size: 10
image_as_file_pixels: 5000000
parallel_file_transfer: false
federate_rooms: false
always_custom_emoji_reaction: false
animated_sticker:
target: webp
convert_from_webm: false
args:
width: 256
height: 256
fps: 25
animated_emoji:
target: webp
args:
width: 64
height: 64
fps: 25
encryption:
allow: true
default: true
appservice: true
msc4190: false
require: true
allow_key_sharing: true
delete_keys:
delete_outbound_on_ack: true
dont_store_outbound: false
ratchet_on_decrypt: true
delete_fully_used_on_decrypt: true
delete_prev_on_new_session: true
delete_on_device_delete: true
periodically_delete_expired: true
delete_outdated_inbound: true
verification_levels:
receive: cross-signed-tofu
send: cross-signed-tofu
share: cross-signed-tofu
rotation:
enable_custom: true
milliseconds: 2592000000
messages: 10000
disable_device_change_key_rotation: true
private_chat_portal_meta: default
disable_reply_fallbacks: true
cross_room_replies: false
delivery_receipts: false
delivery_error_reports: false
incoming_bridge_error_reports: true
message_status_events: true
resend_bridge_info: false
mute_bridging: true
pinned_tag: m.favourite
archive_tag: m.lowpriority
tag_only_on_create: true
bridge_matrix_leave: false
kick_on_logout: false
always_read_joined_telegram_notice: true
create_group_on_invite: false
backfill:
enable: true
normal_groups: true
unread_hours_threshold: 720
forward_limits:
initial:
user: 50
normal_group: 100
supergroup: 50
channel: 50
sync:
user: 0
normal_group: 0
supergroup: 0
channel: 0
forward_timeout: 900
incremental:
messages_per_batch: 100
post_batch_delay: 20
max_batches:
user: -1
normal_group: -1
supergroup: 10
channel: -1
initial_power_level_overrides:
user: {}
group: {}
bot_messages_as_notices: true
bridge_notices:
default: false
exceptions: []
relay_user_distinguishers: ["🟦", "🟣", "🟩", "⭕️", "🔶", "⬛️", "🔵", "🟢"]
# The formats to use when sending messages to Telegram via the relay bot.
# Text msgtypes (m.text, m.notice and m.emote) support HTML, media msgtypes don't.
#
# Available variables:
# \$sender_displayname - The display name of the sender (e.g. Example User)
# \$sender_username - The username (Matrix ID localpart) of the sender (e.g. exampleuser)
# \$sender_mxid - The Matrix ID of the sender (e.g. @exampleuser:example.com)
# \$distinguisher - A random string from the options in the relay_user_distinguishers array.
# \$message - The message content
message_formats:
m.text: '\$distinguisher <b>\$sender_displayname</b>: \$message'
m.notice: '\$distinguisher <b>\$sender_displayname</b>: \$message'
m.emote: '* \$distinguisher <b>\$sender_displayname</b> \$message'
m.file: '\$distinguisher <b>\$sender_displayname</b> sent a file: \$message'
m.image: '\$distinguisher <b>\$sender_displayname</b> sent an image: \$message'
m.audio: '\$distinguisher <b>\$sender_displayname</b> sent an audio file: \$message'
m.video: '\$distinguisher <b>\$sender_displayname</b> sent a video: \$message'
m.location: '\$distinguisher <b>\$sender_displayname</b> sent a location: \$message'
# Telegram doesn't have built-in emotes, this field specifies how m.emote's from authenticated
# users are sent to telegram. All fields in message_formats are supported. Additionally, the
# Telegram user info is available in the following variables:
# \$displayname - Telegram displayname
# \$username - Telegram username (may not exist)
# \$mention - Telegram @username or displayname mention (depending on which exists)
emote_format: '* \$mention \$formatted_body'
# The formats to use when sending state events to Telegram via the relay bot.
#
# Variables from `message_formats` that have the `sender_` prefix are available without the prefix.
# In name_change events, `\$prev_displayname` is the previous displayname.
#
# Set format to an empty string to disable the messages for that event.
state_event_formats:
join: \$distinguisher <b>\$displayname</b> joined the room.
leave: \$distinguisher <b>\$displayname</b> left the room.
name_change: \$distinguisher <b>\$prev_displayname</b> changed their name to \$distinguisher <b>\$displayname</b>
filter:
mode: blacklist
list: []
users: true
command_prefix: '!tg'
management_room_text:
welcome: Hello, I'm a Telegram bridge bot.
welcome_connected: Use `help` for help.
welcome_unconnected: Use `help` for help or `login` to log in.
additional_help: ''
management_room_multiple_messages: false
permissions:
"beeper.local": user
"beeper.com": user
"@inswe:beeper.com": admin
relaybot:
private_chat:
invite: []
state_changes: true
message: This is a Matrix bridge relaybot and does not support direct chats
group_chat_invite: []
ignore_unbridged_group_chat: true
authless_portals: true
whitelist_group_admins: true
ignore_own_incoming_events: true
whitelist: []
telegram:
api_id: $API_ID
api_hash: $API_HASH
bot_token: disabled
catch_up: true
sequential_updates: true
exit_on_update_error: true
force_refresh_interval_seconds: 0
connection:
timeout: 120
retries: 5
retry_delay: 1
flood_sleep_threshold: 60
request_retries: 5
use_ipv6: false
device_info:
device_model: Beeper (self-hosted)
system_version: auto
app_version: auto
lang_code: en
system_lang_code: en
server:
enabled: false
dc: 2
ip: 149.154.167.40
port: 80
proxy:
type: disabled
logging:
version: 1
formatters:
colored:
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
datefmt: "%b %d %H:%M:%S"
handlers:
console:
class: logging.StreamHandler
formatter: colored
stream: ext://sys.stdout
root:
level: INFO
handlers: [console]
EOF
# --- Docker Compose ---
cat <<EOF > docker-compose.yml cat <<EOF > docker-compose.yml
services: services:
telegram: telegram:
@@ -321,7 +81,6 @@ services:
volumes: volumes:
- ./data/telegram:/data - ./data/telegram:/data
environment: environment:
# Explicitly force websocket via env var just in case
- MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET=true - MAUTRIX_TELEGRAM_HOMESERVER_WEBSOCKET=true
logging: logging:
driver: json-file driver: json-file
@@ -330,6 +89,6 @@ services:
max-file: "3" max-file: "3"
EOF EOF
echo "Starting FINAL ATTEMPT..." echo "Starting..."
docker compose up -d --force-recreate telegram docker compose up -d --force-recreate telegram
docker compose logs -f telegram docker compose logs -f telegram