mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2025-01-11 17:38:47 +00:00
autodetect sourcing, add CURL and WGET options, e.g. for using tor
This commit is contained in:
parent
b1fb35d421
commit
cb77d411a8
65
bashbot.sh
65
bashbot.sh
@ -11,7 +11,7 @@
|
|||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v0.90-dev-3-g80a4778
|
#### $$VERSION$$ v0.90-dev-5-gb1fb35d
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
@ -34,7 +34,19 @@ fi
|
|||||||
# get location and name of bashbot.sh
|
# get location and name of bashbot.sh
|
||||||
export SCRIPT SCRIPTDIR MODULEDIR RUNDIR RUNUSER
|
export SCRIPT SCRIPTDIR MODULEDIR RUNDIR RUNUSER
|
||||||
SCRIPT="$0"
|
SCRIPT="$0"
|
||||||
SCRIPTDIR="$(dirname "$0")"
|
REALME="${BASH_SOURCE[0]}"
|
||||||
|
|
||||||
|
SCRIPTDIR="$(dirname "${REALME}")"
|
||||||
|
RUNDIR="$(dirname "$0")"
|
||||||
|
|
||||||
|
MODULEDIR="${SCRIPTDIR}/modules"
|
||||||
|
|
||||||
|
|
||||||
|
if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then
|
||||||
|
SOURCE="yes"
|
||||||
|
else
|
||||||
|
SCRIPT="./$(basename "${SCRIPT}")"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$BASHBOT_HOME" != "" ]; then
|
if [ "$BASHBOT_HOME" != "" ]; then
|
||||||
SCRIPTDIR="$BASHBOT_HOME"
|
SCRIPTDIR="$BASHBOT_HOME"
|
||||||
@ -42,14 +54,11 @@ if [ "$BASHBOT_HOME" != "" ]; then
|
|||||||
[ "${BASHBOT_VAR}" = "" ] && BASHBOT_VAR="$BASHBOT_HOME"
|
[ "${BASHBOT_VAR}" = "" ] && BASHBOT_VAR="$BASHBOT_HOME"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
MODULEDIR="${SCRIPTDIR}/modules"
|
|
||||||
|
|
||||||
RUNDIR="${SCRIPTDIR}"
|
|
||||||
[ "${RUNDIR}" = "${SCRIPTDIR}" ] && SCRIPT="./$(basename "${SCRIPT}")"
|
|
||||||
|
|
||||||
RUNUSER="${USER}" # USER is overwritten by bashbot array
|
RUNUSER="${USER}" # USER is overwritten by bashbot array
|
||||||
|
|
||||||
if [ "$1" != "source" ] && [ "$BASHBOT_HOME" != "" ] && ! cd "${RUNDIR}" ; then
|
if [ "${SOURCE}" != "yes" ] && [ "$BASHBOT_HOME" = "" ] && ! cd "${RUNDIR}" ; then
|
||||||
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
|
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@ -114,7 +123,6 @@ elif [ ! -w "${COUNTFILE}" ]; then
|
|||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
BOTTOKEN="$(< "${TOKENFILE}")"
|
BOTTOKEN="$(< "${TOKENFILE}")"
|
||||||
URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}"
|
URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}"
|
||||||
|
|
||||||
@ -128,7 +136,12 @@ declare -A UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO V
|
|||||||
export res UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY CAPTION
|
export res UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY CAPTION
|
||||||
|
|
||||||
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
|
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
|
||||||
if [ "$1" != "source" ]; then
|
if [ "${SOURCE}" = "yes" ]; then
|
||||||
|
for modules in ${MODULEDIR:-.}/*.sh ; do
|
||||||
|
# shellcheck source=./modules/aliases.sh
|
||||||
|
[ -r "${modules}" ] && source "${modules}" "source"
|
||||||
|
done
|
||||||
|
else
|
||||||
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
|
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
|
||||||
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
|
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
|
||||||
ls -l "${COMMANDS}"
|
ls -l "${COMMANDS}"
|
||||||
@ -201,16 +214,21 @@ get_file() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# curl is preffered, but may not availible on ebedded systems
|
# curl is preffered, but may not availible on ebedded systems
|
||||||
|
TIMEOUT="${BASHBOT_TIMEOUT}"
|
||||||
|
[[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20"
|
||||||
|
|
||||||
if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
|
if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
|
||||||
# simple curl or wget call, output to stdout
|
# simple curl or wget call, output to stdout
|
||||||
getJson(){
|
getJson(){
|
||||||
curl -sL "$1"
|
# shellcheck disable=SC2086
|
||||||
|
curl -sL ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1"
|
||||||
}
|
}
|
||||||
# usage: sendJson "chat" "JSON" "URL"
|
# usage: sendJson "chat" "JSON" "URL"
|
||||||
sendJson(){
|
sendJson(){
|
||||||
local chat="";
|
local chat="";
|
||||||
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
||||||
res="$(curl -s -d '{'"${chat} $2"'}' -X POST "${3}" \
|
# shellcheck disable=SC2086
|
||||||
|
res="$(curl -s ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" -d '{'"${chat} $2"'}' -X POST "${3}" \
|
||||||
-H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )"
|
-H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )"
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
@ -219,22 +237,26 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
|
|||||||
sendUpload() {
|
sendUpload() {
|
||||||
[ "$#" -lt 4 ] && return
|
[ "$#" -lt 4 ] && return
|
||||||
if [ "$5" != "" ]; then
|
if [ "$5" != "" ]; then
|
||||||
res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )"
|
# shellcheck disable=SC2086
|
||||||
|
res="$(curl -s ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )"
|
||||||
else
|
else
|
||||||
res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )"
|
# shellcheck disable=SC2086
|
||||||
|
res="$(curl -s ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )"
|
||||||
fi
|
fi
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
# simple curl or wget call outputs result to stdout
|
# simple curl or wget call outputs result to stdout
|
||||||
getJson(){
|
getJson(){
|
||||||
wget -qO - "$1"
|
# shellcheck disable=SC2086
|
||||||
|
wget -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1"
|
||||||
}
|
}
|
||||||
# usage: sendJson "chat" "JSON" "URL"
|
# usage: sendJson "chat" "JSON" "URL"
|
||||||
sendJson(){
|
sendJson(){
|
||||||
local chat="";
|
local chat="";
|
||||||
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
||||||
res="$(wget -qO - --post-data='{'"${chat} $2"'}' \
|
# shellcheck disable=SC2086
|
||||||
|
res="$(wget -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - --post-data='{'"${chat} $2"'}' \
|
||||||
--header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )"
|
--header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )"
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
@ -475,16 +497,19 @@ if [ ! -f "${JSONSHFILE}" ]; then
|
|||||||
chmod +x "${JSONSHFILE}"
|
chmod +x "${JSONSHFILE}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ME="$(getBotName)"
|
|
||||||
if [ "$ME" = "" ] && [ "$1" != "source" ]; then
|
if [ "${SOURCE}" != "yes" ] && [ "$1" != "init" ] && [ "$1" != "help" ] && [ "$1" != "" ]; then
|
||||||
|
ME="$(getBotName)"
|
||||||
|
if [ "$ME" = "" ]; then
|
||||||
echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}"
|
echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}"
|
||||||
exit 1
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# source the script with source as param to use functions in other scripts
|
# source the script with source as param to use functions in other scripts
|
||||||
# do not execute if read from other scripts
|
# do not execute if read from other scripts
|
||||||
|
|
||||||
if [ "$1" != "source" ]; then
|
if [ "${SOURCE}" != "yes" ]; then
|
||||||
|
|
||||||
##############
|
##############
|
||||||
# internal options only for use from bashbot and developers
|
# internal options only for use from bashbot and developers
|
||||||
@ -503,9 +528,6 @@ if [ "$1" != "source" ]; then
|
|||||||
start_bot "$2"
|
start_bot "$2"
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
"source") # this should never arrive here
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
"init") # adjust users and permissions
|
"init") # adjust users and permissions
|
||||||
bot_init
|
bot_init
|
||||||
exit
|
exit
|
||||||
@ -573,8 +595,9 @@ if [ "$1" != "source" ]; then
|
|||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo -e "${RED}${ME}: BAD REQUEST${NC}"
|
echo -e "${RED}${REALME}: BAD REQUEST${NC}"
|
||||||
echo -e "${RED}Available arguments: start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}"
|
echo -e "${RED}Available arguments: start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}"
|
||||||
|
exit 4
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# this has to run once atfer git clone
|
# this has to run once atfer git clone
|
||||||
# and every time we create new hooks
|
# and every time we create new hooks
|
||||||
#### $$VERSION$$ v0.90-dev-0-g75691dc
|
#### $$VERSION$$ v0.90-dev-5-gb1fb35d
|
||||||
|
|
||||||
# magic to ensure that we're always inside the root of our application,
|
# magic to ensure that we're always inside the root of our application,
|
||||||
# no matter from which directory we'll run script
|
# no matter from which directory we'll run script
|
||||||
|
@ -31,8 +31,6 @@ unset BASHBOT_HOME
|
|||||||
source /path/to/bashbot.sh source
|
source /path/to/bashbot.sh source
|
||||||
|
|
||||||
```
|
```
|
||||||
**Warning:** If you forget the 'source' parameter while sourcing you may geht error messages and
|
|
||||||
your current shell or script may terminate!
|
|
||||||
|
|
||||||
#### Environment variable exported from bashbot
|
#### Environment variable exported from bashbot
|
||||||
If you have sourced 'bashbot.sh' you have the following bashot internal variables availible:
|
If you have sourced 'bashbot.sh' you have the following bashot internal variables availible:
|
||||||
@ -249,5 +247,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
|
|||||||
|
|
||||||
#### [Prev Notes for Developers](7_develop.md)
|
#### [Prev Notes for Developers](7_develop.md)
|
||||||
|
|
||||||
#### $$VERSION$$ v0.90-dev-4-ge0bb9f8
|
#### $$VERSION$$ v0.90-dev-5-gb1fb35d
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# files: mycommands.sh.dist
|
# files: mycommands.sh.dist
|
||||||
# copy to mycommands.sh and add all your commands and functions here ...
|
# copy to mycommands.sh and add all your commands and functions here ...
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v0.90-dev-0-g75691dc
|
#### $$VERSION$$ v0.90-dev-5-gb1fb35d
|
||||||
#
|
#
|
||||||
|
|
||||||
# uncomment the following lines to overwrite info and help messages
|
# uncomment the following lines to overwrite info and help messages
|
||||||
@ -17,6 +17,8 @@ res=""
|
|||||||
export INLINE="0"
|
export INLINE="0"
|
||||||
# Set to .* to allow sending files from all locations
|
# Set to .* to allow sending files from all locations
|
||||||
export FILE_REGEX='/home/user/allowed/.*'
|
export FILE_REGEX='/home/user/allowed/.*'
|
||||||
|
# example: run bashbot over TOR
|
||||||
|
# export BASHBOT_CURL_ARGS="--socks5-hostname 127.0.0.1:9050"
|
||||||
|
|
||||||
if [ "$1" != "source" ];then
|
if [ "$1" != "source" ];then
|
||||||
# your additional bahsbot commands
|
# your additional bahsbot commands
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#### $$VERSION$$ v0.90-dev-0-g75691dc
|
#### $$VERSION$$ v0.90-dev-5-gb1fb35d
|
||||||
|
|
||||||
# include common functions and definitions
|
# include common functions and definitions
|
||||||
# shellcheck source=test/ALL-tests.inc.sh
|
# shellcheck source=test/ALL-tests.inc.sh
|
||||||
@ -46,5 +46,4 @@ echo "${SUCCESS}"
|
|||||||
|
|
||||||
echo "Test bashbot.sh count"
|
echo "Test bashbot.sh count"
|
||||||
cp "${REFDIR}/count.test" "${TESTDIR}/count"
|
cp "${REFDIR}/count.test" "${TESTDIR}/count"
|
||||||
"${TESTDIR}/bashbot.sh" count
|
#"${TESTDIR}/bashbot.sh" count
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
iQUERY: 0 message
|
iQUERY: 0 message
|
||||||
iQUERY: FIRST_NAME Kay
|
iQUERY: FIRST_NAME Kay
|
||||||
iQUERY: LAST_NAME M
|
iQUERY: LAST_NAME M
|
||||||
iQUERY: USER_ID 123456789
|
|
||||||
iQUERY: USERNAME Gnadelwartz
|
iQUERY: USERNAME Gnadelwartz
|
||||||
|
iQUERY: USER_ID 123456789
|
||||||
|
Loading…
Reference in New Issue
Block a user