From bb1f2eadca69ef8332631f59b8cf43893e0ccb23 Mon Sep 17 00:00:00 2001 From: Evgenii Shavrin <37345777+shavrin777@users.noreply.github.com> Date: Tue, 28 Sep 2021 17:52:59 +0300 Subject: [PATCH] feat: add moonraker-telegram-bot by nlef (#117) Adds installation and removing feature for https://github.com/nlef/moonraker-telegram-bot --- README.md | 6 +++ kiauh.sh | 4 ++ scripts/backup.sh | 14 ++++++ scripts/functions.sh | 15 ++++++ scripts/install_moonraker-telegram-bot.sh | 23 +++++++++ scripts/remove.sh | 43 +++++++++++++++++ scripts/status.sh | 59 +++++++++++++++++++++++ scripts/ui/backup_menu.sh | 3 ++ scripts/ui/install_menu.sh | 10 ++-- scripts/ui/main_menu.sh | 4 +- scripts/ui/remove_menu.sh | 7 ++- scripts/ui/update_menu.sh | 5 +- scripts/update.sh | 14 ++++++ 13 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 scripts/install_moonraker-telegram-bot.sh diff --git a/README.md b/README.md index 352ce27..a12bee4 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,12 @@ https://github.com/Kragrathea/pgcode --- +### **🤖Telegram bot for Moonraker** by [nlef](https://github.com/nlef) : + +https://github.com/nlef/moonraker-telegram-bot + +--- + ## **❓ FAQ** **_Q: Can i use this script to install multiple instances of Klipper on the same Pi? (Multisession?)_** diff --git a/kiauh.sh b/kiauh.sh index 5563d82..ea50a51 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -38,6 +38,9 @@ OCTOPRINT_DIR=${HOME}/OctoPrint #KlipperScreen KLIPPERSCREEN_DIR=${HOME}/KlipperScreen KLIPPERSCREEN_ENV_DIR=${HOME}/.KlipperScreen-env +#MoonrakerTelegramBot +MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot +MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env #misc INI_FILE=${HOME}/.kiauh.ini BACKUP_DIR=${HOME}/kiauh-backups @@ -48,6 +51,7 @@ ARKSINE_REPO=https://github.com/Arksine/klipper.git DMBUTYUGIN_REPO=https://github.com/dmbutyugin/klipper.git DWC2FK_REPO=https://github.com/Stephan3/dwc2-for-klipper-socket.git KLIPPERSCREEN_REPO=https://github.com/jordanruthe/KlipperScreen.git +NLEF_REPO=https://github.com/nlef/moonraker-telegram-bot.git #branches BRANCH_SCURVE_SMOOTHING=dmbutyugin/scurve-smoothing BRANCH_SCURVE_SHAPING=dmbutyugin/scurve-shaping diff --git a/scripts/backup.sh b/scripts/backup.sh index 305d429..e0f6901 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -179,3 +179,17 @@ backup_klipperscreen(){ ERROR_MSG=" Can't backup KlipperScreen directory!\n Not found!" fi } + +backup_MoonrakerTelegramBot(){ + if [ -d $MOONRAKER_TELEGRAM_BOT_DIR ] ; then + status_msg "Creating MoonrakerTelegramBot backup ..." + check_for_backup_dir + get_date + status_msg "Timestamp: $current_date" + mkdir -p $BACKUP_DIR/MoonrakerTelegramBot-backups/"$current_date" + cp -r $MOONRAKER_TELEGRAM_BOT_DIR $_ + ok_msg "Backup complete!" + else + ERROR_MSG=" Can't backup MoonrakerTelegramBot directory!\n Not found!" + fi +} diff --git a/scripts/functions.sh b/scripts/functions.sh index 3a56233..56bf80a 100755 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -209,6 +209,21 @@ restart_klipperscreen(){ sudo systemctl restart KlipperScreen && ok_msg "KlipperScreen Service restarted!" } +start_MoonrakerTelegramBot(){ + status_msg "Starting MoonrakerTelegramBot Service ..." + sudo systemctl start moonraker-telegram-bot && ok_msg "MoonrakerTelegramBot Service started!" +} + +stop_MoonrakerTelegramBot(){ + status_msg "Stopping MoonrakerTelegramBot Service ..." + sudo systemctl stop moonraker-telegram-bot && ok_msg "MoonrakerTelegramBot Service stopped!" +} + +restart_MoonrakerTelegramBot(){ + status_msg "Restarting MoonrakerTelegramBot Service ..." + sudo systemctl restart moonraker-telegram-bot && ok_msg "MoonrakerTelegramBot Service restarted!" +} + restart_nginx(){ if [ "$(systemctl list-units --full -all -t service --no-legend | grep -F "nginx.service")" ]; then status_msg "Restarting NGINX Service ..." diff --git a/scripts/install_moonraker-telegram-bot.sh b/scripts/install_moonraker-telegram-bot.sh new file mode 100644 index 0000000..13d3e02 --- /dev/null +++ b/scripts/install_moonraker-telegram-bot.sh @@ -0,0 +1,23 @@ +install_MoonrakerTelegramBot(){ + source_kiauh_ini + #MoonrakerTelegramBot main installation + MoonrakerTelegramBot_setup + #after install actions + restart_MoonrakerTelegramBot +} + +MoonrakerTelegramBot_setup(){ + source_kiauh_ini + export klipper_cfg_loc + dep=(virtualenv) + dependency_check + status_msg "Downloading MoonrakerTelegramBot ..." + #force remove existing MoonrakerTelegramBot dir + [ -d $MOONRAKER_TELEGRAM_BOT_DIR ] && rm -rf $MOONRAKER_TELEGRAM_BOT_DIR + #clone into fresh MoonrakerTelegramBot dir + cd ${HOME} && git clone $NLEF_REPO + ok_msg "Download complete!" + status_msg "Installing MoonrakerTelegramBot ..." + $MOONRAKER_TELEGRAM_BOT_DIR/scripts/install.sh + echo; ok_msg "MoonrakerTelegramBot successfully installed!" +} diff --git a/scripts/remove.sh b/scripts/remove.sh index bc6827b..ce36648 100755 --- a/scripts/remove.sh +++ b/scripts/remove.sh @@ -410,6 +410,49 @@ remove_klipperscreen(){ CONFIRM_MSG="KlipperScreen successfully removed!" } +remove_MoonrakerTelegramBot(){ + source_kiauh_ini + + ### remove MoonrakerTelegramBot dir + if [ -d $MOONRAKER_TELEGRAM_BOT_DIR ]; then + status_msg "Removing MoonrakerTelegramBot directory ..." + rm -rf $MOONRAKER_TELEGRAM_BOT_DIR && ok_msg "Directory removed!" + fi + + ### remove MoonrakerTelegramBot VENV dir + if [ -d $MOONRAKER_TELEGRAM_BOT_ENV_DIR ]; then + status_msg "Removing MoonrakerTelegramBot VENV directory ..." + rm -rf $MOONRAKER_TELEGRAM_BOT_ENV_DIR && ok_msg "Directory removed!" + fi + + ### remove MoonrakerTelegramBot service + if [ -e /etc/systemd/system/moonraker-telegram-bot.service ]; then + status_msg "Removing MoonrakerTelegramBot service ..." + sudo systemctl stop moonraker-telegram-bot + sudo systemctl disable moonraker-telegram-bot + sudo rm -f $SYSTEMDDIR/moonraker-telegram-bot.service + ###reloading units + sudo systemctl daemon-reload + sudo systemctl reset-failed + ok_msg "MoonrakerTelegramBot Service removed!" + fi + + ### remove MoonrakerTelegramBot log + if [ -e /tmp/telegram.log ]; then + status_msg "Removing MoonrakerTelegramBot log file ..." + rm -f /tmp/telegram.log && ok_msg "File removed!" + fi + + ### remove MoonrakerTelegramBot log symlink in config dir + + if [ -e $klipper_cfg_loc/telegram.log ]; then + status_msg "Removing MoonrakerTelegramBot log symlink ..." + rm -f $klipper_cfg_loc/telegram.log && ok_msg "File removed!" + fi + + CONFIRM_MSG="MoonrakerTelegramBot successfully removed!" +} + remove_mjpg-streamer(){ ### remove MJPG-Streamer service if [ -e $SYSTEMDDIR/webcamd.service ]; then diff --git a/scripts/status.sh b/scripts/status.sh index e82938f..a9d02d6 100755 --- a/scripts/status.sh +++ b/scripts/status.sh @@ -228,6 +228,36 @@ klipperscreen_status(){ fi } +MoonrakerTelegramBot_status(){ + mtbcount=0 + MoonrakerTelegramBot_data=( + SERVICE + $MOONRAKER_TELEGRAM_BOT_DIR + $MOONRAKER_TELEGRAM_BOT_ENV_DIR + ) + + ### count amount of MoonrakerTelegramBot_data service files in /etc/systemd/system + SERVICE_FILE_COUNT=$(ls /etc/systemd/system | grep -E "moonraker-telegram-bot" | wc -l) + + ### remove the "SERVICE" entry from the MoonrakerTelegramBot_data array if a MoonrakerTelegramBot service is installed + [ $SERVICE_FILE_COUNT -gt 0 ] && unset MoonrakerTelegramBot_data[0] + + #count+1 for each found data-item from array + for mtbd in "${MoonrakerTelegramBot_data[@]}" + do + if [ -e $mtbd ]; then + mtbcount=$(expr $mtbcount + 1) + fi + done + if [ "$mtbcount" == "${#MoonrakerTelegramBot_data[*]}" ]; then + MOONRAKER_TELEGRAM_BOT_STATUS="${green}Installed!${default} " + elif [ "$mtbcount" == 0 ]; then + MOONRAKER_TELEGRAM_BOT_STATUS="${red}Not installed!${default} " + else + MOONRAKER_TELEGRAM_BOT_STATUS="${yellow}Incomplete!${default} " + fi +} + ############################################################# ############################################################# @@ -517,6 +547,34 @@ compare_klipperscreen_versions(){ fi } +read_MoonrakerTelegramBot_versions(){ + if [ -d $MOONRAKER_TELEGRAM_BOT_DIR ] && [ -d $MOONRAKER_TELEGRAM_BOT_DIR/.git ]; then + cd $MOONRAKER_TELEGRAM_BOT_DIR + git fetch origin master -q + LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT=$(git describe HEAD --always --tags | cut -d "-" -f 1,2) + REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) + else + LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT=$NONE + REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT=$NONE + fi +} + +compare_MoonrakerTelegramBot_versions(){ + unset MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL + read_MoonrakerTelegramBot_versions + if [ "$LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT" != "$REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT" ]; then + LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT="${yellow}$(printf "%-12s" "$LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT")${default}" + REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT="${green}$(printf "%-12s" "$REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT")${default}" + MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL="true" + update_arr+=(update_MoonrakerTelegramBot) + else + LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT="${green}$(printf "%-12s" "$LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT")${default}" + REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT="${green}$(printf "%-12s" "$REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT")${default}" + MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL="false" + fi +} + + ############################################################# ############################################################# @@ -564,5 +622,6 @@ ui_print_versions(){ compare_mainsail_versions compare_fluidd_versions compare_klipperscreen_versions + compare_MoonrakerTelegramBot_versions compare_pgc_versions } diff --git a/scripts/ui/backup_menu.sh b/scripts/ui/backup_menu.sh index 231176c..4c612ae 100755 --- a/scripts/ui/backup_menu.sh +++ b/scripts/ui/backup_menu.sh @@ -14,6 +14,7 @@ backup_ui(){ echo -e "| 2) [Moonraker] | Other: | " echo -e "| 3) [Moonraker DB] | 7) [Duet Web Control] | " echo -e "| | 8) [OctoPrint] | " + echo -e "| | 9) [MoonrakerTelegramBot] | " back_footer } @@ -40,6 +41,8 @@ backup_menu(){ do_action "backup_dwc2" "backup_ui";; 8) do_action "backup_octoprint" "backup_ui";; + 9) + do_action "backup_MoonrakerTelegramBot" "backup_ui";; B|b) clear; main_menu; break;; *) diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 40978b8..c16b8f9 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -13,10 +13,10 @@ install_ui(){ echo -e "| 2) [Moonraker] | 6) [Duet Web Control] | " echo -e "| | 7) [OctoPrint] | " echo -e "| Klipper Webinterface: | 8) [PrettyGCode] | " - echo -e "| 3) [Mainsail] | | " - echo -e "| 4) [Fluidd] | Webcam: | " - echo -e "| | 9) [MJPG-Streamer] | " - echo -e "| | | " + echo -e "| 3) [Mainsail] | 9) [MoonrakerTelegramBot]| " + echo -e "| 4) [Fluidd] | | " + echo -e "| | Webcam: | " + echo -e "| | 10) [MJPG-Streamer] | " back_footer } @@ -42,6 +42,8 @@ install_menu(){ 8) do_action "install_pgc_for_klipper" "install_ui";; 9) + do_action "install_MoonrakerTelegramBot" "install_ui";; + 10) do_action "install_mjpg-streamer" "install_ui";; B|b) clear; main_menu; break;; diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index e279ac4..0715129 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -11,7 +11,8 @@ main_ui(){ echo -e "| 4) [Advanced] | Mainsail: $MAINSAIL_STATUS|" echo -e "| 5) [Backup] | Fluidd: $FLUIDD_STATUS|" echo -e "| | KlipperScreen: $KLIPPERSCREEN_STATUS|" - echo -e "| 6) [Settings] | |" + echo -e "| 6) [Settings] | MTelegramBot: $MOONRAKER_TELEGRAM_BOT_STATUS|" + echo -e "| | |" echo -e "| | DWC2: $DWC2_STATUS|" echo -e "| ${cyan}$KIAUH_VER${default}| Octoprint: $OCTOPRINT_STATUS|" quit_footer @@ -53,6 +54,7 @@ main_menu(){ mainsail_status octoprint_status klipperscreen_status + MoonrakerTelegramBot_status print_branch print_msg && clear_msg main_ui diff --git a/scripts/ui/remove_menu.sh b/scripts/ui/remove_menu.sh index 83d39fc..5abea31 100755 --- a/scripts/ui/remove_menu.sh +++ b/scripts/ui/remove_menu.sh @@ -15,8 +15,9 @@ remove_ui(){ echo -e "| | 7) [OctoPrint] | " echo -e "| Klipper Webinterface: | 8) [MJPG-Streamer] | " echo -e "| 3) [Mainsail] | 9) [PrettyGCode] | " - echo -e "| 4) [Fluidd] | | " - echo -e "| | 10) [NGINX] | " + echo -e "| 4) [Fluidd] | 10) [MTelegramBot] | " + echo -e "| | | " + echo -e "| | 11) [NGINX] | " back_footer } @@ -44,6 +45,8 @@ remove_menu(){ 9) do_action "remove_prettygcode" "remove_ui";; 10) + do_action "remove_MoonrakerTelegramBot" "remove_ui";; + 11) do_action "remove_nginx" "remove_ui";; B|b) clear; main_menu; break;; diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index 26fb096..8986a17 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -22,8 +22,9 @@ update_ui(){ echo -e "| 6) [DWC2-for-Klipper] | $LOCAL_DWC2FK_COMMIT | $REMOTE_DWC2FK_COMMIT | " echo -e "| 7) [DWC2 Web UI] | $DWC2_LOCAL_VER | $DWC2_REMOTE_VER | " echo -e "| 8) [PrettyGCode] | $LOCAL_PGC_COMMIT | $REMOTE_PGC_COMMIT | " + echo -e "| 9) [MTelegramBot] | $LOCAL_MOONRAKER_TELEGRAM_BOT_COMMIT | $REMOTE_MOONRAKER_TELEGRAM_BOT_COMMIT | " echo -e "| |------------------------------| " - echo -e "| 9) [System] | $DISPLAY_SYS_UPDATE | " + echo -e "| 10) [System] | $DISPLAY_SYS_UPDATE | " back_footer } @@ -52,6 +53,8 @@ update_menu(){ 8) do_action "update_pgc_for_klipper" "update_ui";; 9) + do_action "update_MoonrakerTelegramBot" "update_ui";; + 10) do_action "update_system" "update_ui";; a) do_action "update_all" "update_ui";; diff --git a/scripts/update.sh b/scripts/update.sh index 9ed856e..b37233d 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -42,6 +42,9 @@ update_all(){ if [ "$PGC_UPDATE_AVAIL" = "true" ]; then echo -e "| ${cyan}● PrettyGCode for Klipper${default} |" fi + if [ "$MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL" = "true" ]; then + echo -e "| ${cyan}● MoonrakerTelegramBot${default} |" + fi if [ "$SYS_UPDATE_AVAIL" = "true" ]; then echo -e "| ${cyan}● System${default} |" fi @@ -313,6 +316,17 @@ update_pgc_for_klipper(){ ok_msg "Update complete!" } +update_MoonrakerTelegramBot(){ + source_kiauh_ini + export klipper_cfg_loc + stop_MoonrakerTelegramBot + cd $MOONRAKER_TELEGRAM_BOT_DIR + git pull + ./scripts/install.sh + ok_msg "Update complete!" + start_MoonrakerTelegramBot +} + update_system(){ status_msg "Updating System ..." sudo apt-get update --allow-releaseinfo-change && sudo apt-get upgrade -y