mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2025-01-28 08:08:25 +00:00
bin: extend broadcast to use any jsonDB user db
This commit is contained in:
parent
168584d693
commit
2a3ab2a145
@ -1,15 +1,18 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#===============================================================================
|
#===============================================================================
|
||||||
|
# shellcheck disable=SC2059
|
||||||
#
|
#
|
||||||
# FILE: bin/broadcast_message.sh
|
# FILE: bin/broadcast_message.sh
|
||||||
#
|
#
|
||||||
# USAGE: broadcast_message.sh [-h|--help] [--doit] [--groups|--both] [format] "message ...." [debug]
|
USAGE='broadcast_message.sh [-h|--help] [--doit] [--groups|--both|--db=file] [format] "message ...." [debug]'
|
||||||
#
|
#
|
||||||
# DESCRIPTION: send a message to all users the bot have seen (listet in count.jssh)
|
# DESCRIPTION: send a message to all users listed in a jsonDB (default count db)
|
||||||
#
|
#
|
||||||
# OPTIONS: --doit - broadcast is dangerous, simulate run without --doit
|
# OPTIONS: --doit - broadcast is dangerous, simulate run without --doit
|
||||||
# --groups - send to groups instead of users
|
# --groups - send to groups instead of users
|
||||||
# --both - send to users and groups
|
# --both - send to users and groups
|
||||||
|
# --db name - send to all user/groups in jsonDB database (e.g. blocked)
|
||||||
|
# db file: name.jssh, db keys are user/chat id, values are ignored
|
||||||
#
|
#
|
||||||
# format - normal, markdown, html (optional)
|
# format - normal, markdown, html (optional)
|
||||||
# message - message to send in specified format
|
# message - message to send in specified format
|
||||||
@ -24,13 +27,13 @@
|
|||||||
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
|
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
|
||||||
# CREATED: 16.12.2020 16:14
|
# CREATED: 16.12.2020 16:14
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v1.25-dev-34-gda214ab
|
#### $$VERSION$$ v1.25-dev-44-g168584d
|
||||||
#===============================================================================
|
#===============================================================================
|
||||||
# shellcheck disable=SC2059
|
|
||||||
|
|
||||||
####
|
####
|
||||||
# minimum messages seen in a chat before send a broadcast to it
|
# minimum messages seen in a chat before send a broadcast to it
|
||||||
MINCOUNT=2
|
MINCOUNT=2
|
||||||
|
USERDB=""
|
||||||
|
|
||||||
####
|
####
|
||||||
# broadcast is dangerous, without --doit we do a dry run ...
|
# broadcast is dangerous, without --doit we do a dry run ...
|
||||||
@ -49,6 +52,11 @@ elif [ "$1" = "--groups" ]; then
|
|||||||
SENDTO="groups"
|
SENDTO="groups"
|
||||||
GROUPSALSO=" only"
|
GROUPSALSO=" only"
|
||||||
shift
|
shift
|
||||||
|
elif [ "$1" = "--db" ]; then
|
||||||
|
USERDB="${2%.jssh}"
|
||||||
|
MINCOUNT=""
|
||||||
|
GROUPSALSO=" and groups"
|
||||||
|
shift 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
####
|
####
|
||||||
@ -71,11 +79,11 @@ case "$1" in
|
|||||||
printf "missing missing arguments\n"
|
printf "missing missing arguments\n"
|
||||||
;&
|
;&
|
||||||
"-h"*)
|
"-h"*)
|
||||||
printf 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]\n'
|
printf 'usage: %s\n' "${USAGE}"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
'--h'*)
|
'--h'*)
|
||||||
sed -n '3,/###/p' <"$0"
|
sed -n -e '/# shellcheck /d' -e '3,/###/p' <"$0"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
@ -85,15 +93,16 @@ esac
|
|||||||
source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug
|
source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug
|
||||||
|
|
||||||
|
|
||||||
# read in users
|
# read in users from given DB or count.jssh
|
||||||
|
database="${USERDB:-${COUNTFILE}}"
|
||||||
declare -A SENDALL
|
declare -A SENDALL
|
||||||
jssh_readDB_async "SENDALL" "${COUNTFILE}"
|
jssh_readDB_async "SENDALL" "${database}"
|
||||||
if [ -z "${SENDALL[*]}" ]; then
|
if [ -z "${SENDALL[*]}" ]; then
|
||||||
printf "${ORANGE}Countfile not found or empty,${NC}\n"
|
printf "${ORANGE}User database not found or empty: ${NC}${database}\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# loop over users
|
# loop over users
|
||||||
printf "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}"
|
printf "${GREEN}Sending broadcast message to user of ${BOT_NAME} using database:${NC}${GREY} ${database##*/}"
|
||||||
|
|
||||||
{ # dry run
|
{ # dry run
|
||||||
[ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n"
|
[ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n"
|
||||||
@ -106,7 +115,7 @@ fi
|
|||||||
# ignore everything not a user or group
|
# ignore everything not a user or group
|
||||||
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
|
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
|
||||||
# ignore chats with no count or lower MINCOUNT
|
# ignore chats with no count or lower MINCOUNT
|
||||||
[[ ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ]] && continue
|
[[ -n "${MINCOUNT}" && ( ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ) ]] && continue
|
||||||
(( COUNT++ ))
|
(( COUNT++ ))
|
||||||
if [ -z "${DOIT}" ]; then
|
if [ -z "${DOIT}" ]; then
|
||||||
printf "${SEND} ${USER} $1 $2\n"
|
printf "${SEND} ${USER} $1 $2\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user