move procname and proclist, replace echo with cat for output to file
This commit is contained in:
parent
a1a823b4b6
commit
a3c0d3165c
45
bashbot.sh
45
bashbot.sh
|
@ -12,7 +12,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.80-dev3-1-gbccd064
|
#### $$VERSION$$ v0.80-dev3-2-ga1a823b
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
|
@ -58,6 +58,7 @@ if [ ! -f "${TOKENFILE}" ]; then
|
||||||
echo "Running headless, run ${SCRIPT} init first!"
|
echo "Running headless, run ${SCRIPT} init first!"
|
||||||
exit 2
|
exit 2
|
||||||
else
|
else
|
||||||
|
${CLEAR}
|
||||||
echo -e "${RED}TOKEN MISSING.${NC}"
|
echo -e "${RED}TOKEN MISSING.${NC}"
|
||||||
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
|
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
|
||||||
read -r token
|
read -r token
|
||||||
|
@ -71,6 +72,7 @@ if [ ! -f "${BOTADMIN}" ]; then
|
||||||
echo "Running headless, set botadmin to AUTO MODE!"
|
echo "Running headless, set botadmin to AUTO MODE!"
|
||||||
echo '?' > "${BOTADMIN}"
|
echo '?' > "${BOTADMIN}"
|
||||||
else
|
else
|
||||||
|
${CLEAR}
|
||||||
echo -e "${RED}BOTADMIN MISSING.${NC}"
|
echo -e "${RED}BOTADMIN MISSING.${NC}"
|
||||||
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
|
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
|
||||||
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
|
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
|
||||||
|
@ -129,6 +131,19 @@ if [ "$1" != "source" ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# internal functions
|
||||||
|
# $1 postfix, e.g. chatid
|
||||||
|
# $2 prefix, back- or startbot-
|
||||||
|
procname(){
|
||||||
|
echo "$2${ME}_$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# $1 proc name
|
||||||
|
proclist() {
|
||||||
|
# shellcheck disable=SC2009
|
||||||
|
ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2
|
||||||
|
}
|
||||||
|
|
||||||
# returns true if command exist
|
# returns true if command exist
|
||||||
_exists()
|
_exists()
|
||||||
{
|
{
|
||||||
|
@ -228,26 +243,26 @@ JsonGetValue() {
|
||||||
################
|
################
|
||||||
# processing of updates starts here
|
# processing of updates starts here
|
||||||
process_updates() {
|
process_updates() {
|
||||||
MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
local max num debug="$1"
|
||||||
for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do
|
max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
||||||
process_client "$1"
|
for ((num=0; num<=max; num++)); do
|
||||||
|
process_client "$num" "${debug}"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
process_client() {
|
process_client() {
|
||||||
iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${PROCESS_NUMBER}"',"inline_query","id"')"
|
local num="$1" debug="$2"
|
||||||
|
iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${num}"',"inline_query","id"')"
|
||||||
if [ "${iQUERY[ID]}" = "" ]; then
|
if [ "${iQUERY[ID]}" = "" ]; then
|
||||||
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"MESSAGE.log"
|
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"MESSAGE.log"
|
||||||
process_message "$PROCESS_NUMBER" "$1"
|
process_message "${num}" "${debug}"
|
||||||
else
|
else
|
||||||
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"INLINE.log"
|
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"INLINE.log"
|
||||||
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "$PROCESS_NUMBER" "$1"
|
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "${num}" "${debug}"
|
||||||
fi
|
fi
|
||||||
# Tmux
|
|
||||||
copname="$ME"_"${CHAT[ID]}"
|
|
||||||
# shellcheck source=./commands.sh
|
# shellcheck source=./commands.sh
|
||||||
source "${COMMANDS}" "$1"
|
source "${COMMANDS}" "${debug}"
|
||||||
tmpcount="COUNT${CHAT[ID]}"
|
tmpcount="COUNT${CHAT[ID]}"
|
||||||
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>"${COUNTFILE}"
|
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || cat <<< "$tmpcount$'\\n'" >>"${COUNTFILE}"
|
||||||
# To get user count execute bash bashbot.sh count
|
# To get user count execute bash bashbot.sh count
|
||||||
}
|
}
|
||||||
process_inline() {
|
process_inline() {
|
||||||
|
@ -261,7 +276,7 @@ process_inline() {
|
||||||
process_message() {
|
process_message() {
|
||||||
local num="$1"
|
local num="$1"
|
||||||
local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE"
|
local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE"
|
||||||
echo "$UPDATE" >"$TMP"
|
cat <<< "$UPDATE$'\\n'" >"$TMP"
|
||||||
# Message
|
# Message
|
||||||
MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')"
|
MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')"
|
||||||
MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )"
|
MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )"
|
||||||
|
@ -333,7 +348,7 @@ process_message() {
|
||||||
# Location
|
# Location
|
||||||
LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")"
|
LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")"
|
||||||
LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")"
|
LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")"
|
||||||
NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')"
|
NAME="$(sed 's/.*\///g' <<< "${URLS[*]}")"
|
||||||
rm "$TMP"
|
rm "$TMP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,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.80-dev3-1-gbccd064
|
#### $$VERSION$$ v0.80-dev3-2-ga1a823b
|
||||||
|
|
||||||
# adjust your language setting here
|
# adjust your language setting here
|
||||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||||
|
|
|
@ -5,7 +5,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.80-dev3-1-gbccd064
|
#### $$VERSION$$ v0.80-dev3-2-ga1a823b
|
||||||
|
|
||||||
# source from commands.sh if you want ro use interactive or background jobs
|
# source from commands.sh if you want ro use interactive or background jobs
|
||||||
|
|
||||||
|
@ -32,25 +32,12 @@ killproc() {
|
||||||
kill_proc "${CHAT[ID]}" "$1"
|
kill_proc "${CHAT[ID]}" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# internal functions
|
|
||||||
# $1 chatid
|
|
||||||
# $2 prefix
|
|
||||||
fifoname(){
|
|
||||||
echo "$2${ME}_$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
# $1 pipename
|
|
||||||
listproc() {
|
|
||||||
# shellcheck disable=SC2009
|
|
||||||
ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2
|
|
||||||
}
|
|
||||||
|
|
||||||
# inline and backgound functions
|
# inline and backgound functions
|
||||||
# $1 chatid
|
# $1 chatid
|
||||||
# $2 program
|
# $2 program
|
||||||
# $3 jobname
|
# $3 jobname
|
||||||
start_back() {
|
start_back() {
|
||||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
|
local fifo; fifo="${TMPDIR:-.}/$(procname "$1")"
|
||||||
echo "$1:$3:$2" >"${fifo}$3-back.cmd"
|
echo "$1:$3:$2" >"${fifo}$3-back.cmd"
|
||||||
start_proc "$1" "$2" "back-$3-"
|
start_proc "$1" "$2" "back-$3-"
|
||||||
}
|
}
|
||||||
|
@ -62,7 +49,7 @@ start_back() {
|
||||||
start_proc() {
|
start_proc() {
|
||||||
[ "$2" = "" ] && return
|
[ "$2" = "" ] && return
|
||||||
kill_proc "$1" "$3"
|
kill_proc "$1" "$3"
|
||||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1" "$3")"
|
local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")"
|
||||||
mkfifo "${fifo}"
|
mkfifo "${fifo}"
|
||||||
{ set -f
|
{ set -f
|
||||||
# shellcheck disable=SC2002
|
# shellcheck disable=SC2002
|
||||||
|
@ -81,7 +68,7 @@ check_back() {
|
||||||
# $1 chatid
|
# $1 chatid
|
||||||
# $2 prefix
|
# $2 prefix
|
||||||
check_proc() {
|
check_proc() {
|
||||||
[ "$(listproc "$(fifoname "$1" "$2")")" != "" ]
|
[ "$(proclist "$(procname "$1" "$2")")" != "" ]
|
||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
res=$?; return $?
|
res=$?; return $?
|
||||||
}
|
}
|
||||||
|
@ -90,15 +77,15 @@ check_proc() {
|
||||||
# $2 jobname
|
# $2 jobname
|
||||||
kill_back() {
|
kill_back() {
|
||||||
kill_proc "$1" "back-$2-"
|
kill_proc "$1" "back-$2-"
|
||||||
rm -f "${TMPDIR:-.}/$(fifoname "$1")$2-back.cmd"
|
rm -f "${TMPDIR:-.}/$(procname "$1")$2-back.cmd"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# $1 chatid
|
# $1 chatid
|
||||||
# $2 prefix
|
# $2 prefix
|
||||||
kill_proc() {
|
kill_proc() {
|
||||||
local fifo; fifo="$(fifoname "$1" "$2")"
|
local fifo; fifo="$(procname "$1" "$2")"
|
||||||
kill -15 "$(listproc "${fifo}")" 2>/dev/null
|
kill -15 "$(proclist "${fifo}")" 2>/dev/null
|
||||||
fifo="${TMPDIR:-.}/${fifo}"
|
fifo="${TMPDIR:-.}/${fifo}"
|
||||||
[ -s "${fifo}.log" ] || rm -f "${fifo}.log"
|
[ -s "${fifo}.log" ] || rm -f "${fifo}.log"
|
||||||
[ -p "${fifo}" ] && rm -f "${fifo}";
|
[ -p "${fifo}" ] && rm -f "${fifo}";
|
||||||
|
@ -107,6 +94,6 @@ kill_proc() {
|
||||||
# $1 chat
|
# $1 chat
|
||||||
# $2 message
|
# $2 message
|
||||||
forward_interactive() {
|
forward_interactive() {
|
||||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
|
local fifo; fifo="${TMPDIR:-.}/$(procname "$1")"
|
||||||
[ -p "${fifo}" ] && echo "$2" >"${fifo}"
|
[ -p "${fifo}" ] && echo "$2" >"${fifo}"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue