Merge pull request #9 from jinwyp/main

feat: Add systemd service installation script to init project
This commit is contained in:
yihong 2023-12-16 21:15:04 +08:00 committed by GitHub
commit b12ab51f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 2 deletions

View File

@ -21,11 +21,34 @@ for yihong0618's channel: https://t.me/hyi0618
![telegram-cloud-photo-size-5-6336976091083817765-y](https://github.com/yihong0618/tg_bot_collections/assets/15976103/683a9c22-6f64-4a51-93e6-5e36218e1668)
## 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
> [!Note]
> If you don't want to use one of these command, you can use `--disable-command <command>` option to disable it. This option can be used multiple times.

110
setup.sh Normal file
View File

@ -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"