From 635ec3835ba1a2d3b5857be08aeca39076c60897 Mon Sep 17 00:00:00 2001 From: JinWYP Date: Sun, 17 Dec 2023 02:26:35 +0800 Subject: [PATCH] feat: Separate the configuration files into env --- .env.example | 2 ++ README.md | 19 ++++++++-------- setup.sh | 61 ++++++++++++++++++++++++++++------------------------ 3 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..961fa7b --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +Google_Gemini_API_Key="your_gemini_api_key" +Telegram_Bot_Token="your_telegram_bot_token" diff --git a/README.md b/README.md index 20ee684..249ccaf 100644 --- a/README.md +++ b/README.md @@ -27,15 +27,16 @@ for yihong0618's channel: https://t.me/hyi0618 1. Git clone this repo 2. cd tg_bot_collections -3. Edit setup.sh file and change the following variables - - python_bin_path (python3 path) - - project_path (this repo path) - - GOOGLE_GEMINI_KEY_Text (Google Gemini API KEY) - - Telegram_Bot_KEY_Text (Telegram Bot Token) -4. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh ``` -5. Run ```systemctl status tg_bot_collections``` to check the status -6. Run ```systemctl start tg_bot_collections``` to start the service -7. Run ```systemctl stop tg_bot_collections``` to stop the service +3. Copy file .env.example to .env +4. Edit .env file and change the following variables + - Google_Gemini_API_Key (Google Gemini API KEY) + - Telegram_Bot_Token (Telegram Bot Token) +5. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh ``` to install and run + +6. Run ```systemctl status tgbotyh``` to check the status +7. Run ```systemctl start tgbotyh``` to start the service +8. Run ```systemctl stop tgbotyh``` to stop the service +9. Run ```systemctl restart tgbotyh``` to restart the service ### Manually install diff --git a/setup.sh b/setup.sh index bac9372..463b3af 100644 --- a/setup.sh +++ b/setup.sh @@ -1,16 +1,29 @@ #!/bin/bash -python_bin_path="/usr/local/bin/python3.10" +python_bin_path="$(which python3)" venv_dir="venv" -project_path="/root/github/tg_bot_collections" - -GOOGLE_GEMINI_KEY_Text="xxx" -Telegram_Bot_KEY_Text="xxx" +project_path="$(pwd)" +service_name="tgbotyh" +source .env +google_gemini_api_key="${Google_Gemini_API_Key}" +telegram_bot_token="${Telegram_Bot_Token}" +echo "==============================" +echo "Prapare to setup telegram bot" +echo "" +echo "Project path: $project_path" +echo "Python bin path: $python_bin_path" +echo "Google_Gemini_API_Key: $Google_Gemini_API_Key" +echo "Telegram Bot Token: $Telegram_Bot_Token" +echo "" +sudoCmd="" +if [[ $(/usr/bin/id -u) -ne 0 ]]; then + sudoCmd="sudo" +fi # Check Virtual Environment exist if [ -d "$venv_dir" ]; then @@ -20,26 +33,19 @@ fi # created virtual environment $python_bin_path -m venv "$venv_dir" - - if [ $? -eq 0 ]; then echo "Successfully created virtual environment." else echo "Failed to create virtual environment." fi - source $venv_dir/bin/activate - python -m pip install --upgrade pip - pip install -r requirements.txt - - osSystemMdPath="/lib/systemd/system/" -# 检测系统发行版代号 +# Chcek OS release distribution function getLinuxOSRelease(){ if [[ -f /etc/redhat-release ]]; then osRelease="centos" @@ -53,7 +59,6 @@ function getLinuxOSRelease(){ osRelease="ubuntu" osSystemPackage="apt-get" osSystemMdPath="/lib/systemd/system/" - osReleaseVersionCodeName="bionic" elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then osRelease="centos" osSystemPackage="yum" @@ -74,23 +79,20 @@ function getLinuxOSRelease(){ } - - - -cat > ${osSystemMdPath}tgbotgemini.service <<-EOF +cat > ${osSystemMdPath}${service_name}.service <<-EOF [Unit] -Description=tgbotgemini service +Description=$service_name service After=network.target [Service] User=root Group=root -Environment="GOOGLE_GEMINI_KEY=${GOOGLE_GEMINI_KEY_Text}" +Environment="GOOGLE_GEMINI_KEY=${google_gemini_api_key}" WorkingDirectory=$project_path -ExecStart=$project_path/venv/bin/python $project_path/tg.py "${Telegram_Bot_KEY_Text}" +ExecStart=$project_path/venv/bin/python $project_path/tg.py "${telegram_bot_token}" Restart=on-failure RestartSec=30 @@ -99,12 +101,15 @@ RestartSec=30 WantedBy=multi-user.target EOF +${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service +${sudoCmd} systemctl daemon-reload +${sudoCmd} systemctl start ${service_name}.service -chmod +x ${osSystemMdPath}tgbotgemini.service -systemctl daemon-reload -systemctl start tgbotgemini.service - +echo "" +echo "${service_name}.service running successfully" +echo "" echo "Run following command to start / stop telegram bot" -echo "Start: systemctl start tgbotgemini.service" -echo "Stop: systemctl stop tgbotgemini.service" -echo "Check running status: systemctl status tgbotgemini.service" +echo "Start: systemctl start ${service_name}.service" +echo "Stop: systemctl stop ${service_name}.service" +echo "Check running status: systemctl status ${service_name}.service" +echo "=============================="