From bd31e29441922b068c9737870164cf72ad9515b7 Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Thu, 21 Jul 2016 00:21:44 -0700 Subject: [PATCH] Add Telegram support Add default Telegram variables. Add response. Insert debugging option. Added exit codes. Add Telegram to README.md --- README.md | 8 ++++++++ plexupdate.sh | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 06f5998..3d76ff3 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,14 @@ There are also a few additional options for the more enterprising user. Setting NOTE! If you define this, you MUST define DISTRO and BUILD - DISTRO and BUILD Override which version to download, use -l option to see what you can select. +- TELEGRAM_NOTIFY + Enable Telegram notification support. Requires TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID +- TELEGRAM_BOT_TOKEN + After talking to BotFather and creating a new bot, you can acquire the Bot Token required for the Telegram API. +- TELEGRAM_CHAT_ID + Ask myidbot for your chat ID, or your group's chat ID, required for the Telegram API. +- TELEGRAM_DEBUG + Output API messages for debugging. Most of these options can be specified on the command-line as well, this is just a more convenient way of doing it if you're scripting it. Which brings us to... diff --git a/plexupdate.sh b/plexupdate.sh index 0d5c3ee..9f2f0f8 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -66,6 +66,13 @@ REDHAT_INSTALL="yum -y install" DEBIAN_INSTALL="dpkg -i" DISTRO_INSTALL="" +# Telegram notification options +TELEGRAM_NOTIFY="no" +TELEGRAM_CHAT_ID="no" +TELEGRAM_BOT_TOKEN="no" +TELEGRAM_MESSAGE="" +TELEGRAM_DEBUG=no + # Sanity, make sure wget is in our path... wget >/dev/null 2>/dev/null if [ $? -eq 127 ]; then @@ -140,7 +147,7 @@ cronexit() { } usage() { - echo "Usage: $(basename $0) [-acfhopqsSuU] [config file]" + echo "Usage: $(basename $0) [-acfhopqsStuU] [config file]" echo "" echo " config file overrides the default ~/.plexupdate" echo " If used, it must be the LAST option or it will be ignored" @@ -157,6 +164,7 @@ usage() { echo " -r Print download URL and exit" echo " -s Auto start (needed for some distros)" echo " -S Silent mode. No text output, only exit codes" + echo " -t Send a Telegram notification" echo " -u Auto update plexupdate.sh before running it (experimental)" echo " -U Do not autoupdate plexupdate.sh (experimental, default)" echo @@ -181,6 +189,7 @@ do (-r) PRINT_URL=yes;; (-s) AUTOSTART=yes;; (-S) SILENT=yes;; + (-t) TELEGRAM_NOTIFY=yes;; (-u) AUTOUPDATE=yes;; (-U) AUTOUPDATE=no;; (--) ;; @@ -507,4 +516,36 @@ if [ "${AUTOSTART}" == "yes" ]; then fi fi +if [ "${TELEGRAM_NOTIFY}" == "yes" ]; then + # check to see if there is a BOT_TOKEN provided; if not, error out sending notification. + if [ "${TELEGRAM_BOT_TOKEN}" == "no" ]; then + echo "TELEGRAM_BOT_TOKEN is empty. Get your BOT_TOKEN from @BotFather." + cronexit 1 + fi + # check to see if there is a CHAT_ID in which to send the notification; if not, error out sending notification. + if [ "${TELEGRAM_CHAT_ID}" = "no" ]; then + echo "TELEGRAM_CHAT_ID is empty. Get the Chat ID from @myidbot." + cronexit 1 + fi + # check to see if AUTOINSTALL is on; if so, set a successful update notice. + if [ "${AUTOINSTALL}" == "yes" ]; then + TELEGRAM_MESSAGE="Plex was successfully updated using ${FILENAME}" + else + TELEGRAM_MESSAGE="Plex downloaded ${FILENAME} to ${DOWNLOADDIR}" + fi + # send the notification to Telegram's BOT API, output to TELEGRAM_RESPONSE for potential debugging. + TELEGRAM_RESPONSE=$(curl -s https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage?chat_id="${TELEGRAM_CHAT_ID}"\&text="${TELEGRAM_MESSAGE}") + + if [ "${TELEGRAM_DEBUG}" == "yes" ]; then + echo "######################" + echo "### TELEGRAM DEBUG ###" + echo $TELEGRAM_RESPONSE + echo "######################" + else + echo "Telegram notification sent." + fi + +fi + + cronexit 0