]> git.proxmox.com Git - mirror_acme.sh.git/blame - notify/telegram.sh
Merge pull request #4820 from acmesh-official/dev
[mirror_acme.sh.git] / notify / telegram.sh
CommitLineData
2e5a6e21 1#!/usr/bin/env sh
10de4b6b
MB
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
7c7d61f6 30 _content="$(printf "%s" "$_content" | sed -e 's/\([_*`\[]\)/\\\\\1/g')"
10de4b6b
MB
31 _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
32 _data="{\"text\": \"$_content\", "
33 _data="$_data\"chat_id\": \"$TELEGRAM_BOT_CHATID\", "
34 _data="$_data\"parse_mode\": \"markdown\", "
35 _data="$_data\"disable_web_page_preview\": \"1\"}"
36
53d26e5c
MB
37 _debug "$_data"
38
10de4b6b
MB
39 export _H1="Content-Type: application/json"
40 _telegram_bot_url="https://api.telegram.org/bot${TELEGRAM_BOT_APITOKEN}/sendMessage"
fb079f9e 41 if _post "$_data" "$_telegram_bot_url" >/dev/null; then
10de4b6b 42 # shellcheck disable=SC2154
584cc6de 43 _message=$(printf "%s\n" "$response" | sed -n 's/.*"ok":\([^,]*\).*/\1/p')
10de4b6b
MB
44 if [ "$_message" = "true" ]; then
45 _info "telegram send success."
46 return 0
47 fi
48 fi
49 _err "telegram send error."
50 _err "$response"
51 return 1
52}