Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # POSIX
- show_help() {
- echo "Telegram Sender"
- echo ""
- echo "usage: [FILENAME] [FILETYPE]"
- echo ""
- echo " -t,--token Provide Telegram bot token"
- echo " -c,--chatid Provide ID to Send to"
- echo " -f,--file File to send"
- echo " -a,--audio Send as Audio File"
- echo " -v,--voice Send as Voice File"
- echo " -d,--document Send as Document aka any filetype"
- echo " --text Text to Send"
- echo ""
- echo " Examples:"
- echo " telegram --text \"TEST\" -c 1111111 -t BoTtOkEn"
- echo " telegram -f /somefile -d -c 111111 -t BoTtOkEn"
- echo ""
- }
- URL=https://api.telegram.org/bot
- FILE=
- SETTYPE2=
- TYPE=
- verbose=0
- while :; do
- case $1 in
- -h|-\?|--help)
- show_help; exit ;;
- -f|--file)
- if [ "$2" ]; then FILE=$2; shift
- else echo "ERROR: "--file" requires a non-empty option argument."; exit 1
- fi ;;
- --text)
- if [ "$2" ]; then TEXT=$2; shift
- else echo 'ERROR: "--text" requires a non-empty option argument.'; exit 1
- fi
- SETTYPE=""; SETTYPE2="Message"
- ;;
- -d|--document)
- SETTYPE="document"; SETTYPE2="Document"
- ;;
- -v|--voice)
- SETTYPE="voice"; SETTYPE2="Voice"
- ;;
- -a|--audio)
- SETTYPE="audio"; SETTYPE2="Audio"
- ;;
- -c|--chatid)
- if [ "$2" ]; then CHATID=$2; shift
- else echo 'ERROR: "--chatid" requires a non-empty option argument.'
- fi
- ;;
- -t|--token)
- if [ "$2" ]; then TOKEN=$2; shift
- else echo 'ERROR: "--token" requires a non-empty option argument.'
- fi
- ;;
- --) # End of all options.
- shift
- break ;;
- -?*)
- printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2; exit 1 ;;
- *) # Default case: No more options, so break out of the loop.
- break
- esac
- shift
- done
- if [ "$TOKEN" = "" ]; then
- echo 'ERROR: "--token" needed'
- exit 1
- fi
- if [ "$CHATID" = "" ]; then
- echo "ERROR: "--chatid" needed"
- exit 1
- fi
- if [ "$SETTYPE2" = "" ]; then
- echo 'ERROR: provide a Mediatype'
- exit
- elif [ "$SETTYPE2" = "Message" ]; then
- echo "Sending Text Mesaage"
- elif [ "$FILE" = "" ]; then
- echo 'ERROR: Provide a Filename with option "-f"'
- exit 1
- else
- echo "Sending Media"
- TYPE="-F $SETTYPE=@$FILE"
- fi
- /usr/bin/curl $TYPE "$URL$TOKEN/send$SETTYPE2?chat_id=$CHATID&parse_mode=Markdown&text=$TEXT"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement