Last active
July 16, 2020 03:46
-
-
Save emaniacs/8b41fb4d7731bc4872b7c109745986df to your computer and use it in GitHub Desktop.
sending document image or text into telegram channel with bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## this script sending message into telegram channel | |
OSNAME=$(uname) | |
get_fullpath() { | |
if [[ $OSNAME == "Linux" ]] ; then | |
readlink -m $1 | |
elif [[ $OSNAME == "Darwin" ]] ; then | |
realpath $1 | |
else | |
echo $1 | |
fi | |
} | |
# checking for chatid | |
if [[ -z "$CHAT_ID" ]] ; then | |
echo 'there are no CHAT_ID variable' | |
echo 'invite @RawDataBot into your channel to see your chat id' | |
exit 255 | |
fi | |
# checking for bot token | |
if [[ -z "$BOT_TOKEN" ]] ; then | |
echo 'there are no BOT_TOKEN variable' | |
echo 'ask @botfather about your bot token' | |
exit 255 | |
fi | |
HOST=https://api.telegram.org | |
useArgument=1 | |
# read from pipe | |
# this only work on linux | |
if test -p /proc/self/fd/0 -o -s /dev/stdin ; then | |
useArgument=0 | |
args="text='$(cat -)'" | |
endpoint="$HOST/bot$BOT_TOKEN/sendMessage" | |
elif test $# -eq 0; then | |
echo 'Invalid argument' | |
cat<<EOF | |
$0 - Send message into channel on telegram | |
Usage: | |
$0 <options> content | |
Example: | |
Send image | |
$0 photo /tmp/filename.jpg | |
Send document | |
$0 document /tmp/document.log | |
Send text | |
$0 this text will sending all of it. | |
Sending text from stdout | |
echo this text from stdout | $0 | |
Options: | |
photo, p, i, image -> opions for sending image | |
document, d -> opions for sending document | |
EOF | |
exit | |
fi | |
if [[ $useArgument == 1 ]] ; then | |
case $1 in | |
photo|p|i|image) | |
data=$(get_fullpath $2) | |
args="photo=@$data" | |
endpoint="$HOST/bot$BOT_TOKEN/sendPhoto" | |
;; | |
document|d) | |
data=$(get_fullpath $2) | |
args="document=@$data" | |
endpoint="$HOST/bot$BOT_TOKEN/sendDocument" | |
;; | |
*) | |
args="text='$@'" | |
endpoint="$HOST/bot$BOT_TOKEN/sendMessage" | |
esac | |
fi | |
#echo curl --progress -F chat_id=$CHAT_ID -F "$args" $endpoint | |
curl --progress -F chat_id=$CHAT_ID -F "$args" $endpoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment