Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README-telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ The "Copy Message Link" option appears to be available only when you are using a
1. You must add the bot as a subscriber to the channel. You can only do this if you are an Administrator of the Channel
2. For the CHAT_ID, feel free to use the `@ChannelName` value, or follow steps 4 and 5 above to get the numeric ID.

### Sending to a private chat (direct messages)

To receive notifications directly to your personal Telegram account:

1. Start a conversation with your bot by clicking "Start"
2. Use your Telegram user ID as the CHAT_ID
3. Set `PF_TELEGRAM_CHAT_TYPE` and/or `PA_TELEGRAM_CHAT_TYPE` to `private` (see Step 3)

## Step 3: Configure Planefence to send notifications to Telegram

Edit your `planefence.config` file, and add or modify the following parameters:
Expand All @@ -51,6 +59,8 @@ Edit your `planefence.config` file, and add or modify the following parameters:
TELEGRAM_BOT_TOKEN="" # set this parameter to the HTTP API Token you got in Step 1
PF_TELEGRAM_CHAT_ID="" # Planefence Chat (channel) ID - set this parameter to the CHAT_ID you got in Step 2
PA_TELEGRAM_CHAT_ID="" # Plane-Alert Chat (channel) ID - set this parameter to the CHAT_ID you got in Step 2
PF_TELEGRAM_CHAT_TYPE="" # set to "private" for direct messages to a user, leave empty for channels
PA_TELEGRAM_CHAT_TYPE="" # set to "private" for direct messages to a user, leave empty for channels
PF_TELEGRAM_ENABLED=false # Set this to "on"/"enabled"/"1"/"yes"/"true" to start sending Planefence notifications
PA_TELEGRAM_ENABLED=false # Set this to "on"/"enabled"/"1"/"yes"/"true" to start sending Plane-Alert notifications
```
Expand Down
62 changes: 57 additions & 5 deletions rootfs/scripts/post2telegram.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,41 @@ if [[ -z "$TELEGRAM_CHAT_ID" ]]; then
exit 1
fi

if [[ "${TELEGRAM_CHAT_ID:0:4}" != "-100" ]]; then TELEGRAM_CHAT_ID="-100${TELEGRAM_CHAT_ID}"; fi
# Handle different chat ID formats:
# - Starts with @ : public channel username, use as-is (e.g., @mychannel)
# - Positive number: private chat with user (user ID), use as-is (e.g., 37560172)
# - Starts with -100: supergroup/channel ID, use as-is (e.g., -1001234567890)
# - Negative number not starting with -100: regular group, use as-is (e.g., -123456789)
#
# For backward compatibility with existing channel configurations that only stored
# the numeric part without -100 prefix, we add it for IDs that look like channel IDs
# (large positive numbers that don't start with -100 when they should be channels).
# Users can set PF_TELEGRAM_CHAT_TYPE or PA_TELEGRAM_CHAT_TYPE to "private" to
# explicitly indicate a private chat with the bot.

if [[ "${1,,}" == "pf" ]]; then
TELEGRAM_CHAT_TYPE="${PF_TELEGRAM_CHAT_TYPE:-}"
else
TELEGRAM_CHAT_TYPE="${PA_TELEGRAM_CHAT_TYPE:-}"
fi

# Flag to track if this is a private chat (used for link generation)
IS_PRIVATE_CHAT=false

if [[ "${TELEGRAM_CHAT_ID:0:1}" == "@" ]]; then
# Public channel username - use as-is
:
elif [[ "${TELEGRAM_CHAT_TYPE,,}" == "private" ]] || [[ "${TELEGRAM_CHAT_TYPE,,}" == "user" ]] || [[ "${TELEGRAM_CHAT_TYPE,,}" == "dm" ]]; then
# Explicitly marked as private chat - use as-is
IS_PRIVATE_CHAT=true
elif [[ "${TELEGRAM_CHAT_ID:0:1}" == "-" ]]; then
# Already negative - use as-is (either -100... channel or regular group)
:
else
# Positive number without explicit private type - assume it's a channel ID for backward compatibility
# Add -100 prefix as before
TELEGRAM_CHAT_ID="-100${TELEGRAM_CHAT_ID}"
fi

# Extract info from the command line arguments
args=("$@")
Expand Down Expand Up @@ -124,8 +158,17 @@ for image in "${IMAGES[@]}"; do
echo " \"response\": $response }"
} >> /tmp/telegram.json
else
echo "https://t.me/c/${TELEGRAM_CHAT_ID}/${message_id}" > /tmp/telegram.link
"${s6wrap[@]}" echo "Photo message sent successfully to Telegram; link: $(</tmp/telegram.link)"
# Generate link only for channels/groups (private chats don't have public links)
if [[ "$IS_PRIVATE_CHAT" == "true" ]]; then
"${s6wrap[@]}" echo "Photo message sent successfully to Telegram (private chat, message_id: ${message_id})"
echo "" > /tmp/telegram.link
else
# For channels, strip the -100 prefix for the link if present
link_chat_id="${TELEGRAM_CHAT_ID#-100}"
link_chat_id="${link_chat_id#-}"
echo "https://t.me/c/${link_chat_id}/${message_id}" > /tmp/telegram.link
"${s6wrap[@]}" echo "Photo message sent successfully to Telegram; link: $(</tmp/telegram.link)"
fi
fi
fi

Expand All @@ -148,7 +191,16 @@ if (( image_count == 0 )); then
} >> /tmp/telegram.json
exit 1
else
echo "https://t.me/c/${TELEGRAM_CHAT_ID}/${message_id}" > /tmp/telegram.link
"${s6wrap[@]}" echo "Text message sent successfully to Telegram; link: $(</tmp/telegram.link)"
# Generate link only for channels/groups (private chats don't have public links)
if [[ "$IS_PRIVATE_CHAT" == "true" ]]; then
"${s6wrap[@]}" echo "Text message sent successfully to Telegram (private chat, message_id: ${message_id})"
echo "" > /tmp/telegram.link
else
# For channels, strip the -100 prefix for the link if present
link_chat_id="${TELEGRAM_CHAT_ID#-100}"
link_chat_id="${link_chat_id#-}"
echo "https://t.me/c/${link_chat_id}/${message_id}" > /tmp/telegram.link
"${s6wrap[@]}" echo "Text message sent successfully to Telegram; link: $(</tmp/telegram.link)"
fi
fi
fi
18 changes: 15 additions & 3 deletions rootfs/usr/share/planefence/stage/planefence.config
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,29 @@ PA_BLUESKY_ENABLED=""
# how to create a Telegram Bot and a Telegram Channel to post to.
#
# TELEGRAM_BOT_TOKEN should look like "123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ"
# PF_TELEGRAM_CHAT_ID / PA_TELEGRAM_CHAT_ID can be a numeric ID (for private channels) or a public channel name like "@yourchannel"
#
# IMPORTANT: you must add your newly created Bot as an Administrator of your channel. You can only do this if you
# yourself are an Administrator of the channel.
# PF_TELEGRAM_CHAT_ID / PA_TELEGRAM_CHAT_ID can be:
# - A numeric channel/supergroup ID (e.g., "1234567890" - will be prefixed with -100 automatically)
# - A full channel/supergroup ID with -100 prefix (e.g., "-1001234567890")
# - A public channel username (e.g., "@yourchannel")
# - A user ID for private messages to a user (e.g., "37560172")
#
# PF_TELEGRAM_CHAT_TYPE / PA_TELEGRAM_CHAT_TYPE (optional):
# Set to "private", "user", or "dm" if you want to send messages directly to a user (private chat).
# Leave empty or omit for channels/groups (default behavior for backward compatibility).
# When set to "private", the chat ID will be used as-is without adding -100 prefix.
#
# IMPORTANT: For channels, you must add your bot as an Administrator with "Post Messages" permission.
# For private chats, the user must first start a conversation with your bot.
#
# The parameters "PF_TELEGRAM_ENABLED" and "PA_TELEGRAM_ENABLED" must be set to "on"/"enabled"/"1"/"yes"/"true"
# to start notifications about Planefence and Plane-Alert respectively.
#
TELEGRAM_BOT_TOKEN=""
PF_TELEGRAM_CHAT_ID=""
PA_TELEGRAM_CHAT_ID=""
PF_TELEGRAM_CHAT_TYPE=""
PA_TELEGRAM_CHAT_TYPE=""
PF_TELEGRAM_ENABLED=false
PA_TELEGRAM_ENABLED=false
#
Expand Down