Skip to content

Commit e5b661a

Browse files
authored
Merge pull request #11 from LeddaZ/master
Add Telegram support for notifications
2 parents 84659fb + 2b5669f commit e5b661a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ $ crontab -e # set cronjob to run periodically
144144

145145
d2c.sh by default does not use any notification service, but the following are supported and can be enabled:
146146
- [Gotify](https://gotify.net/)
147+
- [Telegram Bot API](https://core.telegram.org/api/bots)
147148
- ...
148149

149150
When DNS records are updated, d2c.sh will send a notification to enabled services. Feel free to submit a pull request to add more notification services.
@@ -158,3 +159,20 @@ enabled = true
158159
endpoint = "http://gotify.example.com"
159160
token = "ccc"
160161
```
162+
163+
#### 2. Telegram
164+
165+
To enable notifications via Telegram bot, add the following configuration to your `toml` file:
166+
167+
```toml
168+
[telegram]
169+
enabled = true
170+
token = "aaabbb" # from BotFather
171+
chat_id = "111234"
172+
```
173+
174+
You can get your chat ID by sending a message to the bot and going to this URL to view the chat_id:
175+
176+
`https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates`
177+
178+

d2c.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ for config_file in $(ls ${config_file_dir}*.toml 2>/dev/null | sort -V); do
8383
gotify_endpoint=$(yq '.gotify.endpoint' ${config_file})
8484
gotify_token=$(yq '.gotify.token' ${config_file})
8585

86+
# read telegram config
87+
telegram_enabled=$(yq '.telegram.enabled' ${config_file})
88+
telegram_token=$(yq '.telegram.token' ${config_file})
89+
telegram_chat_id=$(yq '.telegram.chat_id' ${config_file})
90+
8691
# get records from Cloudflare
8792
existing_records_raw=$(curl --silent --request GET \
8893
--url ${cloudflare_base}/zones/${zone_id}/dns_records \
@@ -141,7 +146,17 @@ for config_file in $(ls ${config_file_dir}*.toml 2>/dev/null | sort -V); do
141146
status_code=$(curl --silent --output /dev/null --write-out "%{http_code}" "${gotify_endpoint}/message?token=${gotify_token}" -F "title=[d2c.sh] ${name} has changed" -F "message=Public IP for ${name} has changed (${public_ip})" -F "priority=5")
142147

143148
if [[ "$status_code" -ne 200 ]]; then
144-
echo "[d2c.sh] Failed to sent Gotify notification"
149+
echo "[d2c.sh] Failed to send Gotify notification"
150+
fi
151+
fi
152+
153+
# check if Telegram is enabled
154+
if [ "$telegram_enabled" = true ]; then
155+
# send changed ip notification
156+
status_code=$(curl --silent --output /dev/null --write-out "%{http_code}" https://api.telegram.org/bot"${telegram_token}"/sendMessage -d chat_id="${telegram_chat_id}" -d disable_web_page_preview=true -d text="[d2c.sh] Public IP for ${name} has changed (${public_ip})")
157+
158+
if [[ "$status_code" -ne 200 ]]; then
159+
echo "[d2c.sh] Failed to send Telegram notification"
145160
fi
146161
fi
147162
else

0 commit comments

Comments
 (0)