From 2fe34d17258f04232e5250726cc74952ca1fbec0 Mon Sep 17 00:00:00 2001 From: JinWYP Date: Fri, 15 Dec 2023 18:40:08 +0800 Subject: [PATCH 1/3] feat:Add systemd service to init project --- README.md | 27 +++++++++++++- setup.sh | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 setup.sh diff --git a/README.md b/README.md index 9a18f87..b0942fe 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,34 @@ for yihong0618's channel: https://t.me/hyi0618 3. use `gemini: ${message}` to ask -## HOW TO +## HOW TO Install and Run + +### Run with systemd service + +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 + +### Manually install 1. pip install -r requirements.txt 2. Get tg token, ask Google or ChatGPT, need get it from [BotFather](https://t.me/BotFather) -3. python tg.py ${tg_token} +3. export GOOGLE_GEMINI_KEY=${your_google_gemini_apikey} +4. python tg.py ${telegram_bot_token} + + +## HOW TO Use + +1. Type `/gemini: ${message}` to ask +2. Type `gemini: ${message}` and upload picture to ask with picture ## Contribution diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..bac9372 --- /dev/null +++ b/setup.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +python_bin_path="/usr/local/bin/python3.10" +venv_dir="venv" + +project_path="/root/github/tg_bot_collections" + +GOOGLE_GEMINI_KEY_Text="xxx" +Telegram_Bot_KEY_Text="xxx" + + + + + +# Check Virtual Environment exist +if [ -d "$venv_dir" ]; then + echo "Virtual Environment already exist" + exit 1 +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/" + +# 检测系统发行版代号 +function getLinuxOSRelease(){ + if [[ -f /etc/redhat-release ]]; then + osRelease="centos" + osSystemPackage="yum" + osSystemMdPath="/usr/lib/systemd/system/" + elif cat /etc/issue | grep -Eqi "debian|raspbian"; then + osRelease="debian" + osSystemPackage="apt-get" + osSystemMdPath="/lib/systemd/system/" + elif cat /etc/issue | grep -Eqi "ubuntu"; then + 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" + osSystemMdPath="/usr/lib/systemd/system/" + elif cat /proc/version | grep -Eqi "debian|raspbian"; then + osRelease="debian" + osSystemPackage="apt-get" + osSystemMdPath="/lib/systemd/system/" + elif cat /proc/version | grep -Eqi "ubuntu"; then + osRelease="ubuntu" + osSystemPackage="apt-get" + osSystemMdPath="/lib/systemd/system/" + elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then + osRelease="centos" + osSystemPackage="yum" + osSystemMdPath="/usr/lib/systemd/system/" + fi + +} + + + + +cat > ${osSystemMdPath}tgbotgemini.service <<-EOF + +[Unit] +Description=tgbotgemini service +After=network.target + +[Service] +User=root +Group=root + +Environment="GOOGLE_GEMINI_KEY=${GOOGLE_GEMINI_KEY_Text}" + +WorkingDirectory=$project_path +ExecStart=$project_path/venv/bin/python $project_path/tg.py "${Telegram_Bot_KEY_Text}" + +Restart=on-failure +RestartSec=30 + +[Install] +WantedBy=multi-user.target +EOF + + +chmod +x ${osSystemMdPath}tgbotgemini.service +systemctl daemon-reload +systemctl start tgbotgemini.service + +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" From 8b6865e72e107b05fd16fe2751863513dc31bef7 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Sun, 17 Dec 2023 00:18:47 +0900 Subject: [PATCH 2/3] Update map.py probelematic -> problematic --- handlers/map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/map.py b/handlers/map.py index 247b788..a9989ff 100644 --- a/handlers/map.py +++ b/handlers/map.py @@ -28,7 +28,7 @@ class Plot(PrettyPlot): self.xmax, self.ymax, ) = self.aoi_bounds - # take from aoi geometry bounds, otherwise probelematic if unequal geometry distribution over plot. + # take from aoi geometry bounds, otherwise problematic if unequal geometry distribution over plot. self.xmid = (self.xmin + self.xmax) / 2 self.ymid = (self.ymin + self.ymax) / 2 self.xdif = self.xmax - self.xmin From 635ec3835ba1a2d3b5857be08aeca39076c60897 Mon Sep 17 00:00:00 2001 From: JinWYP Date: Sun, 17 Dec 2023 02:26:35 +0800 Subject: [PATCH 3/3] 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 "=============================="