Merge branch 'main' of https://github.com/yihong0618/tg_bot_collections
This commit is contained in:
yihong0618 2023-12-17 22:57:01 +08:00
commit 2c76a6b7d1
4 changed files with 144 additions and 3 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

@ -21,11 +21,35 @@ 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. 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
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.

View File

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

115
setup.sh Normal file
View File

@ -0,0 +1,115 @@
#!/bin/bash
python_bin_path="$(which python3)"
venv_dir="venv"
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
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/"
# Chcek OS release distribution
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/"
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}${service_name}.service <<-EOF
[Unit]
Description=$service_name service
After=network.target
[Service]
User=root
Group=root
Environment="GOOGLE_GEMINI_KEY=${google_gemini_api_key}"
WorkingDirectory=$project_path
ExecStart=$project_path/venv/bin/python $project_path/tg.py "${telegram_bot_token}"
Restart=on-failure
RestartSec=30
[Install]
WantedBy=multi-user.target
EOF
${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service
${sudoCmd} systemctl daemon-reload
${sudoCmd} systemctl start ${service_name}.service
echo ""
echo "${service_name}.service running successfully"
echo ""
echo "Run following command to start / stop telegram bot"
echo "Start: systemctl start ${service_name}.service"
echo "Stop: systemctl stop ${service_name}.service"
echo "Check running status: systemctl status ${service_name}.service"
echo "=============================="