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.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v0.80-dev3-1-gbccd064
|
||||
#### $$VERSION$$ v0.80-dev3-2-ga1a823b
|
||||
#
|
||||
# Exit Codes:
|
||||
# - 0 sucess (hopefully)
|
||||
|
@ -58,6 +58,7 @@ if [ ! -f "${TOKENFILE}" ]; then
|
|||
echo "Running headless, run ${SCRIPT} init first!"
|
||||
exit 2
|
||||
else
|
||||
${CLEAR}
|
||||
echo -e "${RED}TOKEN MISSING.${NC}"
|
||||
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
|
||||
read -r token
|
||||
|
@ -71,6 +72,7 @@ if [ ! -f "${BOTADMIN}" ]; then
|
|||
echo "Running headless, set botadmin to AUTO MODE!"
|
||||
echo '?' > "${BOTADMIN}"
|
||||
else
|
||||
${CLEAR}
|
||||
echo -e "${RED}BOTADMIN MISSING.${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}"
|
||||
|
@ -129,6 +131,19 @@ if [ "$1" != "source" ]; then
|
|||
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
|
||||
_exists()
|
||||
{
|
||||
|
@ -228,26 +243,26 @@ JsonGetValue() {
|
|||
################
|
||||
# processing of updates starts here
|
||||
process_updates() {
|
||||
MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
||||
for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do
|
||||
process_client "$1"
|
||||
local max num debug="$1"
|
||||
max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
||||
for ((num=0; num<=max; num++)); do
|
||||
process_client "$num" "${debug}"
|
||||
done
|
||||
}
|
||||
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
|
||||
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"MESSAGE.log"
|
||||
process_message "$PROCESS_NUMBER" "$1"
|
||||
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"MESSAGE.log"
|
||||
process_message "${num}" "${debug}"
|
||||
else
|
||||
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"INLINE.log"
|
||||
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "$PROCESS_NUMBER" "$1"
|
||||
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"INLINE.log"
|
||||
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "${num}" "${debug}"
|
||||
fi
|
||||
# Tmux
|
||||
copname="$ME"_"${CHAT[ID]}"
|
||||
# shellcheck source=./commands.sh
|
||||
source "${COMMANDS}" "$1"
|
||||
source "${COMMANDS}" "${debug}"
|
||||
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
|
||||
}
|
||||
process_inline() {
|
||||
|
@ -261,7 +276,7 @@ process_inline() {
|
|||
process_message() {
|
||||
local num="$1"
|
||||
local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE"
|
||||
echo "$UPDATE" >"$TMP"
|
||||
cat <<< "$UPDATE$'\\n'" >"$TMP"
|
||||
# Message
|
||||
MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')"
|
||||
MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )"
|
||||
|
@ -333,7 +348,7 @@ process_message() {
|
|||
# Location
|
||||
LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")"
|
||||
LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")"
|
||||
NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')"
|
||||
NAME="$(sed 's/.*\///g' <<< "${URLS[*]}")"
|
||||
rm "$TMP"
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
# This file is public domain in the USA and all free countries.
|
||||
# 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
|
||||
# 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.
|
||||
# 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
|
||||
|
||||
|
@ -32,25 +32,12 @@ killproc() {
|
|||
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
|
||||
# $1 chatid
|
||||
# $2 program
|
||||
# $3 jobname
|
||||
start_back() {
|
||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
|
||||
local fifo; fifo="${TMPDIR:-.}/$(procname "$1")"
|
||||
echo "$1:$3:$2" >"${fifo}$3-back.cmd"
|
||||
start_proc "$1" "$2" "back-$3-"
|
||||
}
|
||||
|
@ -62,7 +49,7 @@ start_back() {
|
|||
start_proc() {
|
||||
[ "$2" = "" ] && return
|
||||
kill_proc "$1" "$3"
|
||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1" "$3")"
|
||||
local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")"
|
||||
mkfifo "${fifo}"
|
||||
{ set -f
|
||||
# shellcheck disable=SC2002
|
||||
|
@ -81,7 +68,7 @@ check_back() {
|
|||
# $1 chatid
|
||||
# $2 prefix
|
||||
check_proc() {
|
||||
[ "$(listproc "$(fifoname "$1" "$2")")" != "" ]
|
||||
[ "$(proclist "$(procname "$1" "$2")")" != "" ]
|
||||
# shellcheck disable=SC2034
|
||||
res=$?; return $?
|
||||
}
|
||||
|
@ -90,15 +77,15 @@ check_proc() {
|
|||
# $2 jobname
|
||||
kill_back() {
|
||||
kill_proc "$1" "back-$2-"
|
||||
rm -f "${TMPDIR:-.}/$(fifoname "$1")$2-back.cmd"
|
||||
rm -f "${TMPDIR:-.}/$(procname "$1")$2-back.cmd"
|
||||
}
|
||||
|
||||
|
||||
# $1 chatid
|
||||
# $2 prefix
|
||||
kill_proc() {
|
||||
local fifo; fifo="$(fifoname "$1" "$2")"
|
||||
kill -15 "$(listproc "${fifo}")" 2>/dev/null
|
||||
local fifo; fifo="$(procname "$1" "$2")"
|
||||
kill -15 "$(proclist "${fifo}")" 2>/dev/null
|
||||
fifo="${TMPDIR:-.}/${fifo}"
|
||||
[ -s "${fifo}.log" ] || rm -f "${fifo}.log"
|
||||
[ -p "${fifo}" ] && rm -f "${fifo}";
|
||||
|
@ -107,6 +94,6 @@ kill_proc() {
|
|||
# $1 chat
|
||||
# $2 message
|
||||
forward_interactive() {
|
||||
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
|
||||
local fifo; fifo="${TMPDIR:-.}/$(procname "$1")"
|
||||
[ -p "${fifo}" ] && echo "$2" >"${fifo}"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue