mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
Merge pull request #17 from jinwyp/dev
feat: add custom python version for setup.sh
This commit is contained in:
commit
6d3526289f
11
.editorconfig
Normal file
11
.editorconfig
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
|
||||||
|
[*.sh]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
trim_trailing_whitespace = true
|
23
README.md
23
README.md
@ -23,7 +23,7 @@ for yihong0618's channel: https://t.me/hyi0618
|
|||||||
|
|
||||||
## HOW TO Install and Run
|
## HOW TO Install and Run
|
||||||
|
|
||||||
### Run with systemd service
|
### Run with systemd service with Python virtualenv environment
|
||||||
|
|
||||||
1. Git clone this repo
|
1. Git clone this repo
|
||||||
2. cd tg_bot_collections
|
2. cd tg_bot_collections
|
||||||
@ -31,15 +31,23 @@ for yihong0618's channel: https://t.me/hyi0618
|
|||||||
4. Edit .env file and change the following variables
|
4. Edit .env file and change the following variables
|
||||||
- Google_Gemini_API_Key (Google Gemini API KEY)
|
- Google_Gemini_API_Key (Google Gemini API KEY)
|
||||||
- Telegram_Bot_Token (Telegram Bot Token)
|
- Telegram_Bot_Token (Telegram Bot Token)
|
||||||
5. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh ``` to install and run
|
- Python_Bin_Path (If you install python alternative version, you can use this to specify the python bin path. Default is blank or /usr/bin/python3)
|
||||||
|
- Python_Venv_Path (Python virtualenv path. Default is venv)
|
||||||
|
|
||||||
6. Run ```systemctl status tgbotyh``` to check the status
|
5. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh 1 ``` to install and run
|
||||||
7. Run ```systemctl start tgbotyh``` to start the service
|
6. Next time, you can run ```./setup.sh``` or ``` bash setup.sh ``` to restart / stop the tg_bot_collections service and check the status or log
|
||||||
8. Run ```systemctl stop tgbotyh``` to stop the service
|
7. you can also run ```./setup.sh 4 ``` to restart service, ```./setup.sh 5 ``` to stop, ```./setup.sh 7 ``` to check the log
|
||||||
9. Run ```systemctl restart tgbotyh``` to restart the service
|
|
||||||
|
### Run with command line with Python virtualenv environment
|
||||||
|
1. Git clone this repo
|
||||||
|
2. cd tg_bot_collections
|
||||||
|
3. Copy file .env.example to .env
|
||||||
|
4. Edit .env file and change the following variables (Same as above that Run with systemd service)
|
||||||
|
5. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh 2 ``` to install and run
|
||||||
|
6. Ctrl + C to quit and Run ```deactivate``` to exit the virtualenv environment
|
||||||
|
7. Next time, you can run ```./setup.sh``` or ``` bash setup.sh ``` to restart / stop the tg_bot_collections service and check the status or log
|
||||||
|
|
||||||
### Manually install
|
### Manually install
|
||||||
|
|
||||||
1. pip install -r requirements.txt
|
1. pip install -r requirements.txt
|
||||||
2. Get tg token, ask Google or ChatGPT, need get it from [BotFather](https://t.me/BotFather)
|
2. Get tg token, ask Google or ChatGPT, need get it from [BotFather](https://t.me/BotFather)
|
||||||
3. export GOOGLE_GEMINI_KEY=${your_google_gemini_apikey}
|
3. export GOOGLE_GEMINI_KEY=${your_google_gemini_apikey}
|
||||||
@ -74,4 +82,3 @@ for yihong0618's channel: https://t.me/hyi0618
|
|||||||
## Appreciation
|
## Appreciation
|
||||||
|
|
||||||
- Thank you, that's enough. Just enjoy it.
|
- Thank you, that's enough. Just enjoy it.
|
||||||
|
|
||||||
|
209
setup.sh
209
setup.sh
@ -1,8 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
python_bin_path="$(which python3)"
|
python_bin_path="$(which python3)"
|
||||||
venv_dir="venv"
|
|
||||||
|
|
||||||
project_path="$(pwd)"
|
project_path="$(pwd)"
|
||||||
service_name="tgbotyh"
|
service_name="tgbotyh"
|
||||||
|
|
||||||
@ -11,14 +9,21 @@ source .env
|
|||||||
google_gemini_api_key="${Google_Gemini_API_Key}"
|
google_gemini_api_key="${Google_Gemini_API_Key}"
|
||||||
telegram_bot_token="${Telegram_Bot_Token}"
|
telegram_bot_token="${Telegram_Bot_Token}"
|
||||||
|
|
||||||
echo "=============================="
|
if [ -n "$Python_Venv_Path" ]; then
|
||||||
echo "Prapare to setup telegram bot"
|
venv_dir="${Python_Venv_Path}"
|
||||||
echo ""
|
fi
|
||||||
echo "Project path: $project_path"
|
|
||||||
echo "Python bin path: $python_bin_path"
|
if [ -n "$Python_Bin_Path" ]; then
|
||||||
echo "Google_Gemini_API_Key: $Google_Gemini_API_Key"
|
python_bin_path="$Python_Bin_Path"
|
||||||
echo "Telegram Bot Token: $Telegram_Bot_Token"
|
fi
|
||||||
echo ""
|
|
||||||
|
if [ -n "$Python_Venv_Path" ]; then
|
||||||
|
venv_dir="${Python_Venv_Path}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$Python_Bin_Path" ]; then
|
||||||
|
python_bin_path="$Python_Bin_Path"
|
||||||
|
fi
|
||||||
|
|
||||||
sudoCmd=""
|
sudoCmd=""
|
||||||
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
|
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
|
||||||
@ -76,10 +81,49 @@ function getLinuxOSRelease(){
|
|||||||
osSystemPackage="yum"
|
osSystemPackage="yum"
|
||||||
osSystemMdPath="/usr/lib/systemd/system/"
|
osSystemMdPath="/usr/lib/systemd/system/"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cat > ${osSystemMdPath}${service_name}.service <<-EOF
|
function installPythonVirtualEnv(){
|
||||||
|
echo
|
||||||
|
echo "=============================="
|
||||||
|
echo "Prapare to Install 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
|
||||||
|
|
||||||
|
# Check Virtual Environment exist
|
||||||
|
if [ -d "$venv_dir" ]; then
|
||||||
|
echo "Virtual Environment already exist"
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
source $venv_dir/bin/activate
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# created virtual environment
|
||||||
|
echo "Creating virtual environment..."
|
||||||
|
$python_bin_path -m venv "$venv_dir"
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Successfully created virtual environment."
|
||||||
|
|
||||||
|
source $venv_dir/bin/activate
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
else
|
||||||
|
echo "Failed to create virtual environment."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function installSystemd(){
|
||||||
|
installPythonVirtualEnv
|
||||||
|
|
||||||
|
cat > ${osSystemMdPath}${service_name}.service <<-EOF
|
||||||
|
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=$service_name service
|
Description=$service_name service
|
||||||
@ -101,15 +145,134 @@ RestartSec=30
|
|||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service
|
${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service
|
||||||
${sudoCmd} systemctl daemon-reload
|
${sudoCmd} systemctl daemon-reload
|
||||||
${sudoCmd} systemctl start ${service_name}.service
|
${sudoCmd} systemctl enable ${service_name}.service
|
||||||
|
${sudoCmd} systemctl start ${service_name}.service
|
||||||
|
|
||||||
echo ""
|
echo
|
||||||
echo "${service_name}.service running successfully"
|
echo "${service_name}.service running successfully"
|
||||||
echo ""
|
echo
|
||||||
echo "Run following command to start / stop telegram bot"
|
echo "Run following command to start / stop telegram bot"
|
||||||
echo "Start: systemctl start ${service_name}.service"
|
echo "Start: systemctl start ${service_name}.service"
|
||||||
echo "Stop: systemctl stop ${service_name}.service"
|
echo "Stop: systemctl stop ${service_name}.service"
|
||||||
echo "Check running status: systemctl status ${service_name}.service"
|
echo "Check running status: systemctl status ${service_name}.service"
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
|
}
|
||||||
|
function uninstallSystemd(){
|
||||||
|
${sudoCmd} systemctl stop ${service_name}.service
|
||||||
|
${sudoCmd} systemctl disable ${service_name}.service
|
||||||
|
${sudoCmd} rm -rf ${osSystemMdPath}${service_name}.service
|
||||||
|
${sudoCmd} systemctl daemon-reload
|
||||||
|
}
|
||||||
|
|
||||||
|
function installCommandLine(){
|
||||||
|
installPythonVirtualEnv "noexit"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "=============================="
|
||||||
|
export GOOGLE_GEMINI_KEY=$google_gemini_api_key
|
||||||
|
python tg.py "${telegram_bot_token}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function runSystemd(){
|
||||||
|
echo
|
||||||
|
if [ "$1" == "start" ]; then
|
||||||
|
echo "systemctl start ${service_name}.service"
|
||||||
|
${sudoCmd} systemctl start ${service_name}.service
|
||||||
|
|
||||||
|
elif [ "$1" == "restart" ]; then
|
||||||
|
echo "systemctl restart ${service_name}.service"
|
||||||
|
${sudoCmd} systemctl restart ${service_name}.service
|
||||||
|
|
||||||
|
elif [ "$1" == "stop" ]; then
|
||||||
|
echo "systemctl stop ${service_name}.service"
|
||||||
|
${sudoCmd} systemctl stop ${service_name}.service
|
||||||
|
|
||||||
|
elif [ "$1" == "status" ]; then
|
||||||
|
echo "systemctl status ${service_name}.service"
|
||||||
|
${sudoCmd} systemctl status ${service_name}.service
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "journalctl -n 30 -u ${service_name}.service "
|
||||||
|
${sudoCmd} journalctl -n 30 -u ${service_name}.service
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
function start_menu(){
|
||||||
|
clear
|
||||||
|
|
||||||
|
echo "=============================="
|
||||||
|
echo " 1. Install telegram bot and Run with Systemd Service"
|
||||||
|
echo " 2. Install and Run with Command Line"
|
||||||
|
echo
|
||||||
|
echo " 3. Uninstall telegram bot and Systemd Service"
|
||||||
|
echo
|
||||||
|
echo " 4. Restart ${service_name} Systemd Service"
|
||||||
|
echo " 5. Stop ${service_name} Systemd Service"
|
||||||
|
echo " 6. Check Status of ${service_name} Systemd Service"
|
||||||
|
echo " 7. Show Log of ${service_name} Systemd Service"
|
||||||
|
echo
|
||||||
|
echo " 0. exit"
|
||||||
|
|
||||||
|
echo
|
||||||
|
read -r -p "Please input number:" menuNumberInput
|
||||||
|
case "$menuNumberInput" in
|
||||||
|
1 )
|
||||||
|
installSystemd
|
||||||
|
;;
|
||||||
|
2 )
|
||||||
|
installCommandLine
|
||||||
|
;;
|
||||||
|
3 )
|
||||||
|
uninstallSystemd
|
||||||
|
;;
|
||||||
|
4 )
|
||||||
|
runSystemd "restart"
|
||||||
|
;;
|
||||||
|
5 )
|
||||||
|
runSystemd "stop"
|
||||||
|
;;
|
||||||
|
6 )
|
||||||
|
runSystemd "status"
|
||||||
|
;;
|
||||||
|
7 )
|
||||||
|
runSystemd
|
||||||
|
;;
|
||||||
|
0 )
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
* )
|
||||||
|
clear
|
||||||
|
echo "Please input correct number !"
|
||||||
|
sleep 2s
|
||||||
|
start_menu
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
function showMenu(){
|
||||||
|
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
start_menu
|
||||||
|
elif [ "$1" == "1" ]; then
|
||||||
|
installSystemd
|
||||||
|
elif [ "$1" == "2" ]; then
|
||||||
|
installCommandLine
|
||||||
|
elif [ "$1" == "3" ]; then
|
||||||
|
uninstallSystemd
|
||||||
|
elif [ "$1" == "4" ]; then
|
||||||
|
runSystemd "restart"
|
||||||
|
elif [ "$1" == "5" ]; then
|
||||||
|
runSystemd "stop"
|
||||||
|
elif [ "$1" == "6" ]; then
|
||||||
|
runSystemd "status"
|
||||||
|
elif [ "$1" == "7" ]; then
|
||||||
|
runSystemd
|
||||||
|
else
|
||||||
|
start_menu
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
showMenu $1
|
Loading…
x
Reference in New Issue
Block a user