telegram-bot-bash/modules/background.sh

120 lines
4.0 KiB
Bash
Raw Normal View History

2019-04-24 08:07:46 +00:00
#!/bin/bash
# file: modules/background.sh
# do not edit, this file will be overwritten on update
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
2019-04-26 15:19:48 +00:00
#### $$VERSION$$ v0.70-dev3-16-gd6d640a
2019-04-24 08:07:46 +00:00
# source from commands.sh if you want ro use interactive or background jobs
2019-04-24 08:17:29 +00:00
## to statisfy shellcheck
export res
2019-04-24 08:07:46 +00:00
####
# I placed send_message here because main use case is interactive chats and background jobs
send_message() {
2019-04-26 15:19:48 +00:00
local text arg keyboard btext burl no_keyboard file lat long title address sent
[ "$2" = "" ] && return
2019-04-24 08:17:29 +00:00
local mychat="$1"
2019-04-26 15:19:48 +00:00
text="$(sed <<< "${2}" 's/ mynewlinestartshere /\r\n/g; s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
2019-04-24 08:07:46 +00:00
arg="$3"
[ "$arg" != "safe" ] && {
2019-04-26 13:07:07 +00:00
no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')"
2019-04-26 15:19:48 +00:00
keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
btext="$(sed <<< "${2}" '/mybtextstartshere /!d;s/.*mybtextstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
burl="$(sed <<< "${2}" '/myburlstartshere /!d;s/.*myburlstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
file="$(sed <<< "${2}" '/myfilelocationstartshere /!d;s/.*myfilelocationstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
lat="$(sed <<< "${2}" '/mylatstartshere /!d;s/.*mylatstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
long="$(sed <<< "${2}" '/mylongstartshere /!d;s/.*mylongstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
title="$(sed <<< "${2}" '/mytitlestartshere /!d;s/.*mytitlestartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
address="$(sed <<< "${2}" '/myaddressstartshere /!d;s/.*myaddressstartshere //g;s/ my[kfltab][a-z]\{2,13\}startshere.*//g;s/ mykeyboardendshere.*//g')"
2019-04-24 08:07:46 +00:00
}
if [ "$no_keyboard" != "" ]; then
2019-04-24 08:17:29 +00:00
remove_keyboard "$mychat" "$text"
2019-04-24 08:07:46 +00:00
sent=y
fi
if [ "$keyboard" != "" ]; then
if [[ "$keyboard" != *"["* ]]; then # pre 0.60 style
keyboard="[ ${keyboard//\" \"/\" \] , \[ \"} ]"
fi
2019-04-24 08:17:29 +00:00
send_keyboard "$mychat" "$text" "$keyboard"
2019-04-24 08:07:46 +00:00
sent=y
fi
2019-04-26 15:19:48 +00:00
if [ "$btext" != "" ] && [ "$burl" != "" ]; then
send_button "$mychat" "$text" "$btext" "$burl"
sent=y
fi
2019-04-24 08:07:46 +00:00
if [ "$file" != "" ]; then
2019-04-24 08:17:29 +00:00
send_file "$mychat" "$file" "$text"
2019-04-24 08:07:46 +00:00
sent=y
fi
2019-04-26 13:07:07 +00:00
if [ "$lat" != "" ] && [ "$long" != "" ]; then
if [ "$address" != "" ] && [ "$title" != "" ]; then
send_venue "$mychat" "$lat" "$long" "$title" "$address"
else
send_location "$mychat" "$lat" "$long"
fi
2019-04-24 08:07:46 +00:00
sent=y
fi
if [ "$sent" != "y" ];then
2019-04-24 08:17:29 +00:00
send_text "$mychat" "$text"
2019-04-24 08:07:46 +00:00
fi
}
send_text() {
case "$2" in
html_parse_mode*)
send_html_message "$1" "${2//html_parse_mode}"
;;
markdown_parse_mode*)
send_markdown_message "$1" "${2//markdown_parse_mode}"
;;
*)
send_normal_message "$1" "$2"
;;
esac
}
######
# interactive and background functions
background() {
2019-04-24 08:17:29 +00:00
echo "${CHAT[ID]}:$2:$1" >"${TMPDIR:-.}/${copname:--}$2-back.cmd"
2019-04-24 08:07:46 +00:00
startproc "$1" "back-$2-"
}
startproc() {
killproc "$2"
local fifo="$2${copname}"
mkfifo "${TMPDIR:-.}/${fifo}"
tmux new-session -d -s "${fifo}" "$1 &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}"
tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}"
}
checkback() {
checkproc "back-$1-"
}
checkproc() {
tmux ls | grep -q "$1${copname}"; res=$?; return $?
}
killback() {
killproc "back-$1-"
rm -f "${TMPDIR:-.}/${copname}$1-back.cmd"
}
killproc() {
local fifo="$1${copname}"
(tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>"${TMPDIR:-.}/${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}")2>/dev/null
}
inproc() {
tmux send-keys -t "$copname" "${MESSAGE[0]} ${URLS[*]}
"
}