mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-14 11:04:29 +05:00
refactor(telegram-bot): update telegram-bot installer (#251)
This commit is contained in:
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
This document covers possible important changes to KIAUH.
|
This document covers possible important changes to KIAUH.
|
||||||
|
|
||||||
|
### 2022-10-30
|
||||||
|
Some functions got updated, though not all of them.
|
||||||
|
|
||||||
|
The following functions are still currently unavailable:
|
||||||
|
- Installation of: Obico, MJPG-Streamer
|
||||||
|
- All backup functions and the Log-Upload
|
||||||
|
|
||||||
### 2022-10-20
|
### 2022-10-20
|
||||||
KIAUH has now reached major version 5 !
|
KIAUH has now reached major version 5 !
|
||||||
|
|
||||||
|
|||||||
1
resources/moonraker-telegram-bot.env
Normal file
1
resources/moonraker-telegram-bot.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
TELEGRAM_BOT_ARGS="/home/%USER%/moonraker-telegram-bot/bot/main.py -c %CFG% -l %LOG%"
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
#Systemd service file for Moonraker Telegram Bot
|
|
||||||
|
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Starts Moonraker Telegram Bot instance %INST% on startup
|
Description=Moonraker Telegram Bot SV1 %INST%
|
||||||
Documentation=https://github.com/nlef/moonraker-telegram-bot/wiki
|
Documentation=https://github.com/nlef/moonraker-telegram-bot/wiki
|
||||||
After=network.target
|
After=network-online.target
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Environment=TELEGRAM_CONF=%CFG%
|
|
||||||
Environment=TELEGRAM_LOG=%LOG%
|
|
||||||
Type=simple
|
Type=simple
|
||||||
User=%USER%
|
User=%USER%
|
||||||
RemainAfterExit=yes
|
RemainAfterExit=yes
|
||||||
ExecStart=%ENV%/bin/python %DIR%/bot/main.py -c ${TELEGRAM_CONF} -l ${TELEGRAM_LOG}
|
WorkingDirectory=/home/%USER%/moonraker-telegram-bot
|
||||||
|
EnvironmentFile=%ENV_FILE%
|
||||||
|
ExecStart=%ENV%/bin/python $TELEGRAM_BOT_ARGS
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=10
|
RestartSec=10
|
||||||
@@ -203,29 +203,40 @@ function create_telegram_conf() {
|
|||||||
local input=("${@}")
|
local input=("${@}")
|
||||||
local telegram_bot_count=${input[0]} && unset "input[0]"
|
local telegram_bot_count=${input[0]} && unset "input[0]"
|
||||||
local names=("${input[@]}") && unset "input[@]"
|
local names=("${input[@]}") && unset "input[@]"
|
||||||
local log="${KLIPPER_LOGS}"
|
local printer_data log_dir cfg cfg_dir
|
||||||
local cfg cfg_dir
|
|
||||||
|
|
||||||
if (( telegram_bot_count == 1 )); then
|
if (( telegram_bot_count == 1 )); then
|
||||||
cfg_dir="${KLIPPER_CONFIG}"
|
printer_data="${HOME}/printer_data"
|
||||||
|
log_dir="${printer_data}/logs"
|
||||||
|
cfg_dir="${printer_data}/config"
|
||||||
cfg="${cfg_dir}/telegram.conf"
|
cfg="${cfg_dir}/telegram.conf"
|
||||||
|
|
||||||
|
### create required folder structure
|
||||||
|
create_required_folders "${printer_data}"
|
||||||
|
|
||||||
### write single instance config
|
### write single instance config
|
||||||
write_telegram_conf "${cfg_dir}" "${cfg}" "${log}"
|
write_telegram_conf "${cfg_dir}" "${cfg}"
|
||||||
|
|
||||||
elif (( telegram_bot_count > 1 )); then
|
elif (( telegram_bot_count > 1 )); then
|
||||||
local j=0 re="^[1-9][0-9]*$"
|
local j=0 re="^[1-9][0-9]*$"
|
||||||
|
|
||||||
for (( i=1; i <= telegram_bot_count; i++ )); do
|
for (( i=1; i <= telegram_bot_count; i++ )); do
|
||||||
### overwrite config folder if name is only a number
|
|
||||||
if [[ ${names[j]} =~ ${re} ]]; then
|
printer_data="${HOME}/${names[${j}]}_data"
|
||||||
cfg_dir="${KLIPPER_CONFIG}/printer_${names[${j}]}"
|
### prefix instance name with "printer_" if it is only a number
|
||||||
else
|
[[ ${names[j]} =~ ${re} ]] && printer_data="${HOME}/printer_${names[${j}]}_data"
|
||||||
cfg_dir="${KLIPPER_CONFIG}/${names[${j}]}"
|
|
||||||
fi
|
|
||||||
|
cfg_dir="${printer_data}/config"
|
||||||
cfg="${cfg_dir}/telegram.conf"
|
cfg="${cfg_dir}/telegram.conf"
|
||||||
|
log_dir="${printer_data}/logs"
|
||||||
|
|
||||||
|
### create required folder structure
|
||||||
|
create_required_folders "${printer_data}"
|
||||||
|
|
||||||
### write multi instance config
|
### write multi instance config
|
||||||
write_telegram_conf "${cfg_dir}" "${cfg}" "${log}"
|
write_telegram_conf "${cfg_dir}" "${cfg}"
|
||||||
|
|
||||||
j=$(( j + 1 ))
|
j=$(( j + 1 ))
|
||||||
done && unset j
|
done && unset j
|
||||||
|
|
||||||
@@ -235,17 +246,15 @@ function create_telegram_conf() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function write_telegram_conf() {
|
function write_telegram_conf() {
|
||||||
local cfg_dir=${1} cfg=${2} log=${3}
|
local cfg_dir=${1} cfg=${2}
|
||||||
local conf_template="${TELEGRAM_BOT_DIR}/scripts/base_install_template"
|
local conf_template="${TELEGRAM_BOT_DIR}/scripts/base_install_template"
|
||||||
[[ ! -d ${cfg_dir} ]] && mkdir -p "${cfg_dir}"
|
|
||||||
|
|
||||||
if [[ ! -f ${cfg} ]]; then
|
if [[ ! -f ${cfg} ]]; then
|
||||||
status_msg "Creating telegram.conf in ${cfg_dir} ..."
|
status_msg "Creating telegram.conf in ${cfg_dir} ..."
|
||||||
cp "${conf_template}" "${cfg}"
|
cp "${conf_template}" "${cfg}"
|
||||||
sed -i "s|some_log_path|${log}|g" "${cfg}"
|
|
||||||
ok_msg "telegram.conf created!"
|
ok_msg "telegram.conf created!"
|
||||||
else
|
else
|
||||||
status_msg "File '${cfg}' already exists!\nSkipping..."
|
ok_msg "File '${cfg}' already exists! Skipping..."
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,16 +262,22 @@ function create_telegram_bot_service() {
|
|||||||
local input=("${@}")
|
local input=("${@}")
|
||||||
local instances=${input[0]} && unset "input[0]"
|
local instances=${input[0]} && unset "input[0]"
|
||||||
local names=("${input[@]}") && unset "input[@]"
|
local names=("${input[@]}") && unset "input[@]"
|
||||||
local cfg_dir cfg log service
|
local printer_data cfg_dir cfg log service env_file
|
||||||
|
|
||||||
if (( instances == 1 )); then
|
if (( instances == 1 )); then
|
||||||
cfg_dir="${KLIPPER_CONFIG}"
|
printer_data="${HOME}/printer_data"
|
||||||
|
cfg_dir="${printer_data}/config"
|
||||||
cfg="${cfg_dir}/telegram.conf"
|
cfg="${cfg_dir}/telegram.conf"
|
||||||
log="${KLIPPER_LOGS}/telegram.log"
|
log="${printer_data}/logs/telegram.log"
|
||||||
service="${SYSTEMD}/moonraker-telegram-bot.service"
|
service="${SYSTEMD}/moonraker-telegram-bot.service"
|
||||||
|
env_file="${printer_data}/systemd/moonraker-telegram-bot.env"
|
||||||
|
|
||||||
|
### create required folder structure
|
||||||
|
create_required_folders "${printer_data}"
|
||||||
|
|
||||||
### write single instance service
|
### write single instance service
|
||||||
write_telegram_bot_service "" "${cfg}" "${log}" "${service}"
|
write_telegram_bot_service "" "${cfg}" "${log}" "${service}" "${env_file}"
|
||||||
ok_msg "Single Telegram Bot instance created!"
|
ok_msg "Telegram Bot instance created!"
|
||||||
|
|
||||||
elif (( instances > 1 )); then
|
elif (( instances > 1 )); then
|
||||||
local j=0 re="^[1-9][0-9]*$"
|
local j=0 re="^[1-9][0-9]*$"
|
||||||
@@ -270,17 +285,27 @@ function create_telegram_bot_service() {
|
|||||||
for (( i=1; i <= instances; i++ )); do
|
for (( i=1; i <= instances; i++ )); do
|
||||||
### overwrite config folder if name is only a number
|
### overwrite config folder if name is only a number
|
||||||
if [[ ${names[j]} =~ ${re} ]]; then
|
if [[ ${names[j]} =~ ${re} ]]; then
|
||||||
cfg_dir="${KLIPPER_CONFIG}/printer_${names[${j}]}"
|
printer_data="${HOME}/printer_${names[${j}]}_data"
|
||||||
else
|
else
|
||||||
cfg_dir="${KLIPPER_CONFIG}/${names[${j}]}"
|
printer_data="${HOME}/${names[${j}]}_data"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
cfg_dir="${printer_data}/config"
|
||||||
cfg="${cfg_dir}/telegram.conf"
|
cfg="${cfg_dir}/telegram.conf"
|
||||||
log="${KLIPPER_LOGS}/telegram-${names[${j}]}.log"
|
log="${printer_data}/logs/telegram.log"
|
||||||
service="${SYSTEMD}/moonraker-telegram-bot-${names[${j}]}.service"
|
service="${SYSTEMD}/moonraker-telegram-bot-${names[${j}]}.service"
|
||||||
|
env_file="${printer_data}/systemd/moonraker-telegram-bot.env"
|
||||||
|
|
||||||
|
### create required folder structure
|
||||||
|
create_required_folders "${printer_data}"
|
||||||
|
|
||||||
### write multi instance service
|
### write multi instance service
|
||||||
write_telegram_bot_service "${names[${j}]}" "${cfg}" "${log}" "${service}"
|
if write_telegram_bot_service "${names[${j}]}" "${cfg}" "${log}" "${service}" "${env_file}"; then
|
||||||
ok_msg "Telegram Bot instance moonraker-telegram-bot-${names[${j}]} created!"
|
ok_msg "Telegram Bot instance moonraker-telegram-bot-${names[${j}]} created!"
|
||||||
|
else
|
||||||
|
error_msg "An error occured during creation of instance moonraker-telegram-bot-${names[${j}]}!"
|
||||||
|
fi
|
||||||
|
|
||||||
j=$(( j + 1 ))
|
j=$(( j + 1 ))
|
||||||
done && unset j
|
done && unset j
|
||||||
|
|
||||||
@@ -290,17 +315,24 @@ function create_telegram_bot_service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function write_telegram_bot_service() {
|
function write_telegram_bot_service() {
|
||||||
local i=${1} cfg=${2} log=${3} service=${4}
|
local i=${1} cfg=${2} log=${3} service=${4} env_file=${5}
|
||||||
local service_template="${KIAUH_SRCDIR}/resources/moonraker-telegram-bot.service"
|
local service_template="${KIAUH_SRCDIR}/resources/moonraker-telegram-bot.service"
|
||||||
|
local env_template="${KIAUH_SRCDIR}/resources/moonraker-telegram-bot.env"
|
||||||
|
|
||||||
### replace all placeholders
|
### replace all placeholders
|
||||||
if [[ ! -f ${service} ]]; then
|
if [[ ! -f ${service} ]]; then
|
||||||
status_msg "Creating Telegram Bot Service ${i} ..."
|
status_msg "Creating service file for instance ${i} ..."
|
||||||
sudo cp "${service_template}" "${service}"
|
sudo cp "${service_template}" "${service}"
|
||||||
[[ -z ${i} ]] && sudo sed -i "s|instance %INST% ||" "${service}"
|
if [[ -z ${i} ]]; then
|
||||||
[[ -n ${i} ]] && sudo sed -i "s|%INST%|${i}|" "${service}"
|
sudo sed -i "s| %INST%||" "${service}"
|
||||||
sudo sed -i "s|%USER%|${USER}|; s|%ENV%|${TELEGRAM_BOT_ENV}|; s|%DIR%|${TELEGRAM_BOT_DIR}|" "${service}"
|
else
|
||||||
sudo sed -i "s|%CFG%|${cfg}|; s|%LOG%|${log}|" "${service}"
|
sudo sed -i "s|%INST%|${i}|" "${service}"
|
||||||
|
fi
|
||||||
|
sudo sed -i "s|%USER%|${USER}|g; s|%ENV%|${TELEGRAM_BOT_ENV}|; s|%ENV_FILE%|${env_file}|" "${service}"
|
||||||
|
|
||||||
|
status_msg "Creating environment file for instance ${i} ..."
|
||||||
|
cp "${env_template}" "${env_file}"
|
||||||
|
sed -i "s|%USER%|${USER}|; s|%CFG%|${cfg}|; s|%LOG%|${log}|" "${env_file}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,9 +375,35 @@ function remove_telegram_bot_env() {
|
|||||||
ok_msg "Directory removed!"
|
ok_msg "Directory removed!"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function remove_telegram_bot_env_file() {
|
||||||
|
local files regex="\/home\/${USER}\/([A-Za-z0-9_]+)\/systemd\/moonraker-telegram-bot\.env"
|
||||||
|
files=$(find "${HOME}" -maxdepth 3 -regextype posix-extended -regex "${regex}" | sort)
|
||||||
|
|
||||||
|
if [[ -n ${files} ]]; then
|
||||||
|
for file in ${files}; do
|
||||||
|
status_msg "Removing ${file} ..."
|
||||||
|
rm -f "${file}"
|
||||||
|
ok_msg "${file} removed!"
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
function remove_telegram_bot_logs() {
|
function remove_telegram_bot_logs() {
|
||||||
|
local files regex="\/home\/${USER}\/([A-Za-z0-9_]+)\/logs\/telegram\.log.*"
|
||||||
|
files=$(find "${HOME}" -maxdepth 3 -regextype posix-extended -regex "${regex}" | sort)
|
||||||
|
|
||||||
|
if [[ -n ${files} ]]; then
|
||||||
|
for file in ${files}; do
|
||||||
|
status_msg "Removing ${file} ..."
|
||||||
|
rm -f "${file}"
|
||||||
|
ok_msg "${file} removed!"
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove_legacy_telegram_bot_logs() {
|
||||||
local files regex="telegram(-[0-9a-zA-Z]+)?\.log(.*)?"
|
local files regex="telegram(-[0-9a-zA-Z]+)?\.log(.*)?"
|
||||||
files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" | sort)
|
files=$(find "${HOME}/klipper_logs" -maxdepth 1 -regextype posix-extended -regex "${HOME}/klipper_logs/${regex}" 2> /dev/null | sort)
|
||||||
|
|
||||||
if [[ -n ${files} ]]; then
|
if [[ -n ${files} ]]; then
|
||||||
for file in ${files}; do
|
for file in ${files}; do
|
||||||
@@ -360,7 +418,9 @@ function remove_telegram_bot() {
|
|||||||
remove_telegram_bot_systemd
|
remove_telegram_bot_systemd
|
||||||
remove_telegram_bot_dir
|
remove_telegram_bot_dir
|
||||||
remove_telegram_bot_env
|
remove_telegram_bot_env
|
||||||
|
remove_telegram_bot_env_file
|
||||||
remove_telegram_bot_logs
|
remove_telegram_bot_logs
|
||||||
|
remove_legacy_telegram_bot_logs
|
||||||
|
|
||||||
local confirm="Moonraker-Telegram-Bot was successfully removed!"
|
local confirm="Moonraker-Telegram-Bot was successfully removed!"
|
||||||
print_confirm "${confirm}" && return
|
print_confirm "${confirm}" && return
|
||||||
@@ -458,12 +518,13 @@ function compare_telegram_bot_versions() {
|
|||||||
#================================================#
|
#================================================#
|
||||||
|
|
||||||
function patch_telegram_bot_update_manager() {
|
function patch_telegram_bot_update_manager() {
|
||||||
local patched="false"
|
local patched moonraker_configs regex
|
||||||
local moonraker_configs
|
regex="\/home\/${USER}\/([A-Za-z0-9_]+)\/config\/moonraker\.conf"
|
||||||
moonraker_configs=$(find "${KLIPPER_CONFIG}" -type f -name "moonraker.conf" | sort)
|
moonraker_configs=$(find "${HOME}" -maxdepth 3 -type f -regextype posix-extended -regex "${regex}" | sort)
|
||||||
|
|
||||||
|
patched="false"
|
||||||
for conf in ${moonraker_configs}; do
|
for conf in ${moonraker_configs}; do
|
||||||
if ! grep -Eq "^\[update_manager moonraker-telegram-bot\]$" "${conf}"; then
|
if ! grep -Eq "^\[update_manager moonraker-telegram-bot\]\s*$" "${conf}"; then
|
||||||
### add new line to conf if it doesn't end with one
|
### add new line to conf if it doesn't end with one
|
||||||
[[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}"
|
[[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}"
|
||||||
|
|
||||||
|
|||||||
@@ -61,10 +61,7 @@ function install_menu() {
|
|||||||
7)
|
7)
|
||||||
do_action "install_pgc_for_klipper" "install_ui";;
|
do_action "install_pgc_for_klipper" "install_ui";;
|
||||||
8)
|
8)
|
||||||
#do_action "telegram_bot_setup_dialog" "install_ui";;
|
do_action "telegram_bot_setup_dialog" "install_ui";;
|
||||||
clear && print_header
|
|
||||||
print_error "Function currently disabled! Sorry!"
|
|
||||||
install_ui;;
|
|
||||||
9)
|
9)
|
||||||
#do_action "moonraker_obico_setup_dialog" "install_ui";;
|
#do_action "moonraker_obico_setup_dialog" "install_ui";;
|
||||||
clear && print_header
|
clear && print_header
|
||||||
|
|||||||
Reference in New Issue
Block a user