Skip to content

Instantly share code, notes, and snippets.

@ypchen
Last active March 6, 2022 09:07
Show Gist options
  • Save ypchen/ff36d174f2412f75a3384b7d5aa97b1e to your computer and use it in GitHub Desktop.
Save ypchen/ff36d174f2412f75a3384b7d5aa97b1e to your computer and use it in GitHub Desktop.
Send a message to Telegram
#!/bin/bash
# default
if [ -z ${CURL+x} ]; then
CURL="/usr/bin/curl"
fi
if [ -z ${ECHO+x} ]; then
ECHO="/usr/bin/echo"
fi
if [ -z ${CUT+x} ]; then
CUT="/usr/bin/cut"
fi
CONF_ALTERNATIVE=""
if [ "$#" -eq 2 ]; then
CONF_ALTERNATIVE="${2}"
elif [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
fi
# bot_token, chat_id, MSG_PREFIX, & CURL, ECHO, CUT if necessary
CONF="${HOME}/.Telegram${CONF_ALTERNATIVE}.conf"
source ${CONF}
if [ -z ${MSG_PREFIX+x} ]; then
MSG_PREFIX=""
fi
if [ -z ${MSG_APPENDIX+x} ]; then
MSG_APPENDIX=""
fi
if [ -z ${text+x} ]; then
text="${1}"
fi
prefixMode=$(${ECHO} "${text}" | ${CUT} -c 1-4)
if [ "${prefixMode}" == "YPM0" ]; then
text=$(${ECHO} "${text}" | ${CUT} -c 5-)
elif [ "${prefixMode}" == "YPM1" ]; then
text=$(${ECHO} "${text}" | ${CUT} -c 5-)
msg=$(eval ${ECHO} "${text}")
text=$(${ECHO} -e "${msg}")
fi
text="${MSG_PREFIX}${text}${MSG_APPENDIX}"
#echo ${CURL} --silent --insecure -G --data-urlencode "chat_id=${chat_id}" --data-urlencode "text=${text}" "https://api.telegram.org/bot${bot_token}/sendMessage"
#${CURL} --output - --dump-head - --insecure -G --data-urlencode "chat_id=${chat_id}" --data-urlencode "text=${text}" "https://api.telegram.org/bot${bot_token}/sendMessage"
${CURL} --silent --insecure -G --data-urlencode "chat_id=${chat_id}" --data-urlencode "text=${text}" "https://api.telegram.org/bot${bot_token}/sendMessage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment