mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
feat: Separate the configuration files into env
This commit is contained in:
parent
b12ab51f35
commit
635ec3835b
2
.env.example
Normal file
2
.env.example
Normal file
@ -0,0 +1,2 @@
|
||||
Google_Gemini_API_Key="your_gemini_api_key"
|
||||
Telegram_Bot_Token="your_telegram_bot_token"
|
19
README.md
19
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
|
||||
|
||||
|
61
setup.sh
61
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 "=============================="
|
||||
|
Loading…
x
Reference in New Issue
Block a user