feat: Separate the configuration files into env

This commit is contained in:
JinWYP 2023-12-17 02:26:35 +08:00
parent b12ab51f35
commit 635ec3835b
3 changed files with 45 additions and 37 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
Google_Gemini_API_Key="your_gemini_api_key"
Telegram_Bot_Token="your_telegram_bot_token"

View File

@ -27,15 +27,16 @@ for yihong0618's channel: https://t.me/hyi0618
1. Git clone this repo 1. Git clone this repo
2. cd tg_bot_collections 2. cd tg_bot_collections
3. Edit setup.sh file and change the following variables 3. Copy file .env.example to .env
- python_bin_path (python3 path) 4. Edit .env file and change the following variables
- project_path (this repo path) - Google_Gemini_API_Key (Google Gemini API KEY)
- GOOGLE_GEMINI_KEY_Text (Google Gemini API KEY) - Telegram_Bot_Token (Telegram Bot Token)
- Telegram_Bot_KEY_Text (Telegram Bot Token) 5. Run ```chmod +x setup.sh && ./setup.sh``` or ``` bash setup.sh ``` to install and run
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 status tgbotyh``` to check the status
6. Run ```systemctl start tg_bot_collections``` to start the service 7. Run ```systemctl start tgbotyh``` to start the service
7. Run ```systemctl stop tg_bot_collections``` to stop the service 8. Run ```systemctl stop tgbotyh``` to stop the service
9. Run ```systemctl restart tgbotyh``` to restart the service
### Manually install ### Manually install

View File

@ -1,16 +1,29 @@
#!/bin/bash #!/bin/bash
python_bin_path="/usr/local/bin/python3.10" python_bin_path="$(which python3)"
venv_dir="venv" venv_dir="venv"
project_path="/root/github/tg_bot_collections" project_path="$(pwd)"
service_name="tgbotyh"
GOOGLE_GEMINI_KEY_Text="xxx"
Telegram_Bot_KEY_Text="xxx"
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 # Check Virtual Environment exist
if [ -d "$venv_dir" ]; then if [ -d "$venv_dir" ]; then
@ -20,26 +33,19 @@ fi
# created virtual environment # created virtual environment
$python_bin_path -m venv "$venv_dir" $python_bin_path -m venv "$venv_dir"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Successfully created virtual environment." echo "Successfully created virtual environment."
else else
echo "Failed to create virtual environment." echo "Failed to create virtual environment."
fi fi
source $venv_dir/bin/activate source $venv_dir/bin/activate
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install -r requirements.txt pip install -r requirements.txt
osSystemMdPath="/lib/systemd/system/" osSystemMdPath="/lib/systemd/system/"
# 检测系统发行版代号 # Chcek OS release distribution
function getLinuxOSRelease(){ function getLinuxOSRelease(){
if [[ -f /etc/redhat-release ]]; then if [[ -f /etc/redhat-release ]]; then
osRelease="centos" osRelease="centos"
@ -53,7 +59,6 @@ function getLinuxOSRelease(){
osRelease="ubuntu" osRelease="ubuntu"
osSystemPackage="apt-get" osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/" osSystemMdPath="/lib/systemd/system/"
osReleaseVersionCodeName="bionic"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
osRelease="centos" osRelease="centos"
osSystemPackage="yum" osSystemPackage="yum"
@ -74,23 +79,20 @@ function getLinuxOSRelease(){
} }
cat > ${osSystemMdPath}${service_name}.service <<-EOF
cat > ${osSystemMdPath}tgbotgemini.service <<-EOF
[Unit] [Unit]
Description=tgbotgemini service Description=$service_name service
After=network.target After=network.target
[Service] [Service]
User=root User=root
Group=root Group=root
Environment="GOOGLE_GEMINI_KEY=${GOOGLE_GEMINI_KEY_Text}" Environment="GOOGLE_GEMINI_KEY=${google_gemini_api_key}"
WorkingDirectory=$project_path 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 Restart=on-failure
RestartSec=30 RestartSec=30
@ -99,12 +101,15 @@ RestartSec=30
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service
${sudoCmd} systemctl daemon-reload
${sudoCmd} systemctl start ${service_name}.service
chmod +x ${osSystemMdPath}tgbotgemini.service echo ""
systemctl daemon-reload echo "${service_name}.service running successfully"
systemctl start tgbotgemini.service echo ""
echo "Run following command to start / stop telegram bot" echo "Run following command to start / stop telegram bot"
echo "Start: systemctl start tgbotgemini.service" echo "Start: systemctl start ${service_name}.service"
echo "Stop: systemctl stop tgbotgemini.service" echo "Stop: systemctl stop ${service_name}.service"
echo "Check running status: systemctl status tgbotgemini.service" echo "Check running status: systemctl status ${service_name}.service"
echo "=============================="