]> git.proxmox.com Git - mirror_acme.sh.git/blame - notify/telegram.sh
Add Telegram notification script
[mirror_acme.sh.git] / notify / telegram.sh
CommitLineData
10de4b6b
MB
1#!/bin/bash
2
3#Support Telegram Bots
4
5#TELEGRAM_BOT_APITOKEN=""
6#TELEGRAM_BOT_CHATID=""
7
8telegram_send() {
9 _subject="$1"
10 _content="$2"
11 _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
12 _debug "_statusCode" "$_statusCode"
13
14 TELEGRAM_BOT_APITOKEN="${TELEGRAM_BOT_APITOKEN:-$(_readaccountconf_mutable TELEGRAM_BOT_APITOKEN)}"
15 if [ -z "$TELEGRAM_BOT_APITOKEN" ]; then
16 TELEGRAM_BOT_APITOKEN=""
17 _err "You didn't specify a Telegram BOT API Token TELEGRAM_BOT_APITOKEN yet."
18 return 1
19 fi
20 _saveaccountconf_mutable TELEGRAM_BOT_APITOKEN "$TELEGRAM_BOT_APITOKEN"
21
22 TELEGRAM_BOT_CHATID="${TELEGRAM_BOT_CHATID:-$(_readaccountconf_mutable TELEGRAM_BOT_CHATID)}"
23 if [ -z "$TELEGRAM_BOT_CHATID" ]; then
24 TELEGRAM_BOT_CHATID=""
25 _err "You didn't specify a Telegram Chat id TELEGRAM_BOT_CHATID yet."
26 return 1
27 fi
28 _saveaccountconf_mutable TELEGRAM_BOT_CHATID "$TELEGRAM_BOT_CHATID"
29
30 _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
31 _data="{\"text\": \"$_content\", "
32 _data="$_data\"chat_id\": \"$TELEGRAM_BOT_CHATID\", "
33 _data="$_data\"parse_mode\": \"markdown\", "
34 _data="$_data\"disable_web_page_preview\": \"1\"}"
35
36 export _H1="Content-Type: application/json"
37 _telegram_bot_url="https://api.telegram.org/bot${TELEGRAM_BOT_APITOKEN}/sendMessage"
38 if _post "$_data" "$_telegram_bot_url"; then
39 # shellcheck disable=SC2154
40 _message=$(printf "%s\n" "$response" | sed -ne 's/.*"ok":\([^,]*\).*/\1/p')
41 if [ "$_message" = "true" ]; then
42 _info "telegram send success."
43 return 0
44 fi
45 fi
46 _err "telegram send error."
47 _err "$response"
48 return 1
49}