Skip to content

Commit c10f466

Browse files
Copilothcastc00
andcommitted
Add Telegram notification system for releases
Co-authored-by: hcastc00 <[email protected]>
1 parent 788e38a commit c10f466

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

.github/workflows/release.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,13 @@ jobs:
187187
source: SHASUM_UBUNTU_UNATTENDED.txt,./images/Dappnode-*-ubuntu-*-unattended.iso
188188
target: ${{ secrets.ISO_SSH_PATH }}
189189
overwrite: true
190+
191+
- name: Send Telegram notification
192+
if: success()
193+
run: |
194+
chmod +x ./scripts/telegram_notify.sh
195+
RELEASE_URL="https://github.com/dappnode/DAppNode/releases/tag/${{ github.event.inputs.core }}"
196+
./scripts/telegram_notify.sh "${{ github.event.inputs.core }}" "${RELEASE_URL}"
197+
env:
198+
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
199+
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ The release will contain:
203203
- Changes section
204204
- SHASUMs for unattended and attended ISOs
205205
- Default credentials
206+
- Notifications:
207+
- Automatic Telegram notification to the configured chat (requires `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID` repository secrets)
206208

207209
## Testing with artifacts
208210

installer_instructions/INSTALLER.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,33 @@ The release action will generate all the assets required as well as the release-
4444
2. Set inputs for the release
4545

4646
![](inputs-release.png)
47+
48+
### Telegram Release Notifications
49+
50+
DAppNode can automatically send Telegram notifications when new releases are created. This requires setting up the following repository secrets:
51+
52+
#### Setup Instructions
53+
54+
1. **Create a Telegram Bot**:
55+
- Message `@BotFather` on Telegram
56+
- Use `/newbot` command and follow the instructions
57+
- Save the bot token (format: `123456789:ABCdefGHIjklMNOpqrsTUVwxyz`)
58+
59+
2. **Get the Chat ID**:
60+
- Add your bot to the desired chat/channel
61+
- Send a message to the chat
62+
- Visit `https://api.telegram.org/bot<YourBotToken>/getUpdates`
63+
- Find the `chat.id` in the response
64+
65+
3. **Configure Repository Secrets**:
66+
- Go to repository Settings → Secrets and variables → Actions
67+
- Add the following secrets:
68+
- `TELEGRAM_BOT_TOKEN`: Your bot token from BotFather
69+
- `TELEGRAM_CHAT_ID`: The chat ID where notifications should be sent
70+
71+
#### Message Format
72+
73+
The notification message includes:
74+
- Release version
75+
- Direct link to the GitHub release page
76+
- Formatted message with emojis and hashtags for easy identification

scripts/telegram_notify.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
3+
# Telegram notification script for DAppNode releases
4+
# This script sends a notification to a Telegram chat when a new release is created
5+
6+
set -e
7+
8+
# Function to display usage
9+
show_usage() {
10+
echo "Usage: $0 <release_version> <release_url>"
11+
echo "Example: $0 v0.2.51 https://github.com/dappnode/DAppNode/releases/tag/v0.2.51"
12+
exit 1
13+
}
14+
15+
# Function to send telegram message
16+
send_telegram_message() {
17+
local message="$1"
18+
local bot_token="$TELEGRAM_BOT_TOKEN"
19+
local chat_id="$TELEGRAM_CHAT_ID"
20+
21+
if [ -z "$bot_token" ]; then
22+
echo "Error: TELEGRAM_BOT_TOKEN environment variable is not set"
23+
exit 1
24+
fi
25+
26+
if [ -z "$chat_id" ]; then
27+
echo "Error: TELEGRAM_CHAT_ID environment variable is not set"
28+
exit 1
29+
fi
30+
31+
# Send the message using curl
32+
curl -s -X POST "https://api.telegram.org/bot${bot_token}/sendMessage" \
33+
-H "Content-Type: application/json" \
34+
-d "{
35+
\"chat_id\": \"${chat_id}\",
36+
\"text\": \"${message}\",
37+
\"parse_mode\": \"Markdown\",
38+
\"disable_web_page_preview\": false
39+
}" > /dev/null
40+
41+
local exit_code=$?
42+
if [ $exit_code -eq 0 ]; then
43+
echo "Telegram notification sent successfully"
44+
else
45+
echo "Failed to send Telegram notification (exit code: $exit_code)"
46+
exit 1
47+
fi
48+
}
49+
50+
# Check if required arguments are provided
51+
if [ $# -ne 2 ]; then
52+
echo "Error: Missing required arguments"
53+
show_usage
54+
fi
55+
56+
RELEASE_VERSION="$1"
57+
RELEASE_URL="$2"
58+
59+
# Validate release version format
60+
if [[ ! $RELEASE_VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
61+
echo "Error: Release version must be in format vX.Y.Z (e.g., v0.2.51)"
62+
exit 1
63+
fi
64+
65+
# Validate release URL
66+
if [[ ! $RELEASE_URL =~ ^https://github\.com/dappnode/DAppNode/releases/tag/.+ ]]; then
67+
echo "Error: Invalid release URL format"
68+
exit 1
69+
fi
70+
71+
# Create the message
72+
MESSAGE="🚀 *New DAppNode Release Available!*
73+
74+
📦 **Version:** \`${RELEASE_VERSION}\`
75+
🔗 **Download:** [${RELEASE_VERSION}](${RELEASE_URL})
76+
77+
The latest DAppNode release is now available with updated core packages and improvements. Visit the release page to download the installation ISOs and view the complete changelog.
78+
79+
#DAppNode #Release #Decentralized"
80+
81+
# Send the notification
82+
echo "Sending Telegram notification for release ${RELEASE_VERSION}..."
83+
send_telegram_message "$MESSAGE"

test/test_telegram_notify.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
# Test script for telegram_notify.sh
4+
# Tests the validation logic and message formatting
5+
6+
set -e
7+
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
TELEGRAM_SCRIPT="${SCRIPT_DIR}/../scripts/telegram_notify.sh"
10+
11+
echo "Testing Telegram notification script..."
12+
13+
# Test 1: No arguments
14+
echo "Test 1: No arguments"
15+
if $TELEGRAM_SCRIPT 2>/dev/null; then
16+
echo "❌ FAILED: Should have failed with no arguments"
17+
exit 1
18+
else
19+
echo "✅ PASSED: Correctly failed with no arguments"
20+
fi
21+
22+
# Test 2: Invalid version format
23+
echo "Test 2: Invalid version format"
24+
if $TELEGRAM_SCRIPT "0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then
25+
echo "❌ FAILED: Should have failed with invalid version format"
26+
exit 1
27+
else
28+
echo "✅ PASSED: Correctly failed with invalid version format"
29+
fi
30+
31+
# Test 3: Invalid URL format
32+
echo "Test 3: Invalid URL format"
33+
if $TELEGRAM_SCRIPT "v0.2.51" "https://invalid-url.com" 2>/dev/null; then
34+
echo "❌ FAILED: Should have failed with invalid URL format"
35+
exit 1
36+
else
37+
echo "✅ PASSED: Correctly failed with invalid URL format"
38+
fi
39+
40+
# Test 4: Valid arguments but missing environment variables
41+
echo "Test 4: Valid arguments but missing environment variables"
42+
if $TELEGRAM_SCRIPT "v0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then
43+
echo "❌ FAILED: Should have failed with missing environment variables"
44+
exit 1
45+
else
46+
echo "✅ PASSED: Correctly failed with missing environment variables"
47+
fi
48+
49+
# Test 5: Valid arguments with mock environment variables (dry run)
50+
echo "Test 5: Valid arguments with test environment variables"
51+
export TELEGRAM_BOT_TOKEN="test_token_123456"
52+
export TELEGRAM_CHAT_ID="test_chat_id"
53+
54+
# Create a mock curl command that doesn't actually send anything
55+
create_mock_curl() {
56+
cat > /tmp/mock_curl.sh << 'EOF'
57+
#!/bin/bash
58+
# Mock curl command for testing
59+
if [[ "$*" == *"sendMessage"* ]]; then
60+
echo "Mock: Telegram message would be sent"
61+
exit 0
62+
fi
63+
exec /usr/bin/curl "$@"
64+
EOF
65+
chmod +x /tmp/mock_curl.sh
66+
}
67+
68+
# Test the script with a dry run by temporarily replacing curl
69+
create_mock_curl
70+
export PATH="/tmp:$PATH"
71+
72+
# Temporarily modify the telegram script to use our mock curl
73+
sed 's|curl -s|/tmp/mock_curl.sh -s|g' $TELEGRAM_SCRIPT > /tmp/telegram_notify_test.sh
74+
chmod +x /tmp/telegram_notify_test.sh
75+
76+
if /tmp/telegram_notify_test.sh "v0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then
77+
echo "✅ PASSED: Script executed successfully with valid arguments"
78+
else
79+
echo "❌ FAILED: Script should have succeeded with valid arguments and environment variables"
80+
exit 1
81+
fi
82+
83+
# Clean up
84+
rm -f /tmp/mock_curl.sh /tmp/telegram_notify_test.sh
85+
unset TELEGRAM_BOT_TOKEN TELEGRAM_CHAT_ID
86+
87+
echo ""
88+
echo "🎉 All tests passed! The Telegram notification script is working correctly."

0 commit comments

Comments
 (0)