telegram-bot-bash/bin/bashbot_env.inc.sh

89 lines
2.6 KiB
Bash
Raw Normal View History

2020-12-18 13:53:44 +00:00
#!/bin/bash
#===============================================================================
#
2020-12-25 17:43:36 +00:00
# FILE: bashbot_env.inc.sh
2020-12-18 13:53:44 +00:00
#
2020-12-25 17:43:36 +00:00
# USAGE: source bashbot_env.inc.sh [debug]
#
2020-12-18 13:53:44 +00:00
# DESCRIPTION: set bashbot environment for all scripts in this directory
#
2020-12-25 17:43:36 +00:00
# OPTIONS: $1 - will be forwarded ro bashbot, e.g. debug
2020-12-18 13:53:44 +00:00
#
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 18.12.2020 12:27
#
2021-02-01 15:30:04 +00:00
#### $$VERSION$$ v1.40-dev-30-g3a29a9d
2020-12-18 13:53:44 +00:00
#===============================================================================
############
2020-12-18 13:53:44 +00:00
# set where your bashbot lives
2021-02-01 15:30:04 +00:00
export BASHBOT_HOME BASHBOT_ETC BASHBOT_VAR FILE_REGEX ME
2020-12-18 13:53:44 +00:00
# default: one dir up
BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}" >/dev/null 2>&1 && pwd)/../"
[ "${BASHBOT_HOME}" = "/../" ] && BASHBOT_HOME="../"
# set you own BASHBOT_HOME if different, e.g.
# BASHBOT_HOME="/usr/local/telegram-bot-bash"
BASHBOT_VAR="${BASHBOT_HOME}"
BASHBOT_ETC="${BASHBOT_HOME}"
2020-12-18 13:53:44 +00:00
#####
# if files are not readable, eviroment is wrong or bashbot is not initialized
2021-02-01 09:18:42 +00:00
# check for bashbot
if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then
2021-01-02 22:29:30 +00:00
printf "%s\n" "Bashbot.sh not found in \"${BASHBOT_HOME}\""
exit 4
fi
2021-02-01 09:18:42 +00:00
dev=" Are we in dev or did you forget to run init?"
2020-12-18 13:53:44 +00:00
# check for botconfig.jssh readable
if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then
2021-02-01 09:18:42 +00:00
printf "%s\n" "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable. ${dev}"
2020-12-18 13:53:44 +00:00
exit 3
fi
# check for count.jssh readable
if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then
2021-02-01 09:18:42 +00:00
printf "%s\n" "Bashbot count file in \"${BASHBOT_VAR}\" does not exist or is not readable. ${dev}"
2020-12-18 13:53:44 +00:00
exit 3
fi
2020-12-26 19:39:21 +00:00
# shellcheck disable=SC1090
source "${BASHBOT_HOME}/bashbot.sh" source "$1"
# overwrite bot FILE regex to BASHBOT_VAR
2020-12-26 19:39:21 +00:00
# change this to the location you want to allow file uploads from
UPLOADDIR="${BASHBOT_VAR%/bin*}"
FILE_REGEX="${UPLOADDIR}/.*"
2020-12-26 19:39:21 +00:00
# get and check ADMIN and NAME
BOT_ADMIN="$(getConfigKey "botadmin")"
BOT_NAME="$(getConfigKey "botname")"
2021-02-01 15:30:04 +00:00
ME="${BOT_NAME}"
2021-02-01 09:18:42 +00:00
[[ -z "${BOT_ADMIN}" || "${BOT_ADMIN}" == "?" ]] && printf "%s\n" "${ORANGE}Warning: Botadmin not set, send bot command${NC} /start"
[[ -z "${BOT_NAME}" ]] && printf "%s\n" "${ORANGE}Warning: Botname not set, run bashbot.sh botname"
2020-12-25 17:43:36 +00:00
2021-01-21 08:22:14 +00:00
# output command result or Telegram response
print_result() { jssh_printDB "BOTSENT" | sort -r; }
print_response() { jssh_printDB "UPD"; }
# check and output help
print_help() {
case "$1" in
'')
printf "missing arguments\n"
;&
"-h"*)
printf 'usage: %s\n' "${USAGE}"
exit 1
;;
'--h'*)
sed -n '/^#====/,/^#====/p' <"$0"
exit 1
;;
esac
}