Add Telegram support

Add default Telegram variables.
Add response.
Insert debugging option.
Added exit codes.
Add Telegram to README.md
This commit is contained in:
Cody Cook 2016-07-21 00:21:44 -07:00 committed by Cody Cook
commit bd31e29441
2 changed files with 50 additions and 1 deletions

View file

@ -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...

View file

@ -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