Spaces:
Sleeping
Sleeping
shivam
commited on
Commit
Β·
6f7c08e
1
Parent(s):
8fe1566
Upload files without png
Browse files- Auto_install.sh +130 -0
- Dockerfile +68 -0
- README.md +72 -10
- addons.txt +5 -0
- bot.py +188 -0
- config.example +4 -0
- reqs.txt +14 -0
- requirements.txt +4 -0
- server.py +88 -0
- start.sh +27 -0
Auto_install.sh
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
## Set default commands
|
| 4 |
+
pipCommand="pip"
|
| 5 |
+
pythonCommand="python"
|
| 6 |
+
useBale=0
|
| 7 |
+
|
| 8 |
+
## Display help for commands
|
| 9 |
+
showHelp() {
|
| 10 |
+
echo -e "Autoinstall usage: $0 [option..]\n
|
| 11 |
+
-b, --bale For install bale api instead of Telegram API \n
|
| 12 |
+
-h, --help This help message\n
|
| 13 |
+
-d, --debug Enable bash debug\n
|
| 14 |
+
-i, --interactive Install requirements for interactive commands\n
|
| 15 |
+
-u, --useful Install useful commands\n
|
| 16 |
+
-3, --python3 Use python3 instead of Python2\n
|
| 17 |
+
"
|
| 18 |
+
exit 0
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
## Use virtualenv
|
| 22 |
+
useVenv() {
|
| 23 |
+
## Create virtual environment
|
| 24 |
+
virtualenv -p $pythonCommand venv
|
| 25 |
+
## Activate virtual environment
|
| 26 |
+
source venv/bin/activate
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
useBaleAPI() {
|
| 31 |
+
## Change telegram API URL to Bale API URL
|
| 32 |
+
find ./ -name bot.py -exec sed -i 's/api\.telegram\.org/tapi.bale.ai/mg' {} \;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
## to use interactive commands like htop we need these packages
|
| 37 |
+
installAha() {
|
| 38 |
+
[ -f "/etc/debian_version" ] && apt-get update && apt-get install -y aha && return 0
|
| 39 |
+
(git clone https://github.com/theZiz/aha.git && cd aha && make && make install return 0) || ( echo "cannot install aha, please use -d to debug \nPlease submit an issue here:\n https://github.com/MParvin/TSMB/issues/new" && exit 1)
|
| 40 |
+
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
## Install useful commands
|
| 44 |
+
installUsefulCMD() {
|
| 45 |
+
echo "Installing htop, please wait a moment..."
|
| 46 |
+
yum install -y htop &> /dev/null || apt install -y htop &> /dev/null
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
configureBot(){
|
| 50 |
+
echo -e "To configure your bot, you must have a Telegram or Bale token\n
|
| 51 |
+
How to create Telegram bot: https://core.telegram.org/bots#3-how-do-i-create-a-bot\n
|
| 52 |
+
How to create Bale bot: https://devbale.ir/quick-start\n"
|
| 53 |
+
read -p "Enter your bot token here:"
|
| 54 |
+
|
| 55 |
+
telegramToken=$REPLY
|
| 56 |
+
|
| 57 |
+
echo -e "Get chat_id and enter here:\n
|
| 58 |
+
To get chat_id do:
|
| 59 |
+
- In telegram:\n start a chat with @id_chatbot
|
| 60 |
+
- In Bale:\n start a chat with chatid_bot
|
| 61 |
+
"
|
| 62 |
+
chatId=$REPLY
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
while :
|
| 66 |
+
do
|
| 67 |
+
case "$1" in
|
| 68 |
+
-b | --bale)
|
| 69 |
+
which virtualenv &> /dev/null || (echo "Cannot use Bale without virtualenv, please run \"pip install virtualenv\" before run this script with -b option" && exit 1)
|
| 70 |
+
useBale = "True"
|
| 71 |
+
break
|
| 72 |
+
;;
|
| 73 |
+
-h | --help)
|
| 74 |
+
showHelp
|
| 75 |
+
exit 0
|
| 76 |
+
;;
|
| 77 |
+
-d | --debug)
|
| 78 |
+
set -x
|
| 79 |
+
break
|
| 80 |
+
;;
|
| 81 |
+
-i | --interactive)
|
| 82 |
+
installAha
|
| 83 |
+
break
|
| 84 |
+
;;
|
| 85 |
+
-u | --useful)
|
| 86 |
+
installUsefulCMD
|
| 87 |
+
break
|
| 88 |
+
;;
|
| 89 |
+
-3 | --python3)
|
| 90 |
+
pythonCommand="python3"
|
| 91 |
+
pipCommand="pip3"
|
| 92 |
+
break
|
| 93 |
+
;;
|
| 94 |
+
*)
|
| 95 |
+
break
|
| 96 |
+
;;
|
| 97 |
+
|
| 98 |
+
esac
|
| 99 |
+
done
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
## Check is python installed
|
| 103 |
+
which $pythonCommand &> /dev/null || (echo "Please install python, and run this script again" && exit 1)
|
| 104 |
+
## Check is pip installed
|
| 105 |
+
which $pipCommand &> /dev/null || (echo -e "Please install pip, and run this script again\n
|
| 106 |
+
In Debian base system for python2.* use apt-get install python-pip\n
|
| 107 |
+
for python3.* use apt-get install python3-pip" && exit 1)
|
| 108 |
+
|
| 109 |
+
## Install virtualenv if is not installed
|
| 110 |
+
(which virtualenv &> /dev/null && useVenv) || read -p "Do you want to install virtualenv(y/n)? " -n 1 -r
|
| 111 |
+
## User accepted
|
| 112 |
+
[[ ! $REPLY =~ ^[yY]$ ]] && $pipCommand install virtualenv --user && useVenv
|
| 113 |
+
|
| 114 |
+
## Install requirements
|
| 115 |
+
([ -f requirements.txt ] && $pipCommand install -r requirements.txt) || echo "Could not find requirements.txt, please clone complete this repository from here:\nhttps://github.com/MParvin/TSMB/, \nthen run Autoinstall.sh"
|
| 116 |
+
if [ "$useBale" -eq 1 ]
|
| 117 |
+
then
|
| 118 |
+
useBaleAPI
|
| 119 |
+
fi
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
read -p "Do you want to configure your bot now(y/n)?"
|
| 123 |
+
if [ $REPLY =~ ^[yY] ]
|
| 124 |
+
then
|
| 125 |
+
configureBot
|
| 126 |
+
else
|
| 127 |
+
echo -e "To use this bot, first change variables in \"config\" file\n
|
| 128 |
+
then executable bot script \"chmod +x bot.py \"
|
| 129 |
+
and run it: \"./bot.py \""
|
| 130 |
+
fi
|
Dockerfile
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9.5-buster
|
| 2 |
+
|
| 3 |
+
# Fix apt sources for old Buster release
|
| 4 |
+
RUN sed -i 's|deb.debian.org|archive.debian.org|g' /etc/apt/sources.list && \
|
| 5 |
+
sed -i 's|security.debian.org|archive.debian.org|g' /etc/apt/sources.list && \
|
| 6 |
+
sed -i '/stretch-updates/d' /etc/apt/sources.list && \
|
| 7 |
+
echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99no-check-valid-until
|
| 8 |
+
|
| 9 |
+
# Set timezone
|
| 10 |
+
ENV TZ=Asia/Kolkata
|
| 11 |
+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
| 12 |
+
|
| 13 |
+
RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list && \
|
| 14 |
+
apt-get update && \
|
| 15 |
+
apt-get install -y --no-install-recommends \
|
| 16 |
+
neofetch \
|
| 17 |
+
git \
|
| 18 |
+
curl \
|
| 19 |
+
wget \
|
| 20 |
+
mediainfo \
|
| 21 |
+
ffmpeg \
|
| 22 |
+
p7zip-full \
|
| 23 |
+
unrar \
|
| 24 |
+
unzip \
|
| 25 |
+
libssl-dev \
|
| 26 |
+
libffi-dev \
|
| 27 |
+
python3-dev && \
|
| 28 |
+
apt-get autoremove --purge -y && \
|
| 29 |
+
rm -rf /var/lib/apt/lists/*
|
| 30 |
+
|
| 31 |
+
# Set working directory
|
| 32 |
+
WORKDIR /Ult
|
| 33 |
+
|
| 34 |
+
# Copy the application code
|
| 35 |
+
COPY . .
|
| 36 |
+
|
| 37 |
+
# --- FIX 1: Connection Speed & Stability ---
|
| 38 |
+
# Installing cryptg speeds up encryption, preventing timeouts.
|
| 39 |
+
# pysocks helps with connection routing.
|
| 40 |
+
RUN pip3 install --no-cache-dir pysocks cryptg
|
| 41 |
+
|
| 42 |
+
# Install requirements
|
| 43 |
+
RUN if [ -f reqs.txt ]; then pip3 install --no-cache-dir -r reqs.txt; fi
|
| 44 |
+
RUN pip3 install -U pip
|
| 45 |
+
RUN pip3 install -U redis
|
| 46 |
+
|
| 47 |
+
RUN if [ -f addons.txt ]; then pip3 install --no-cache-dir -r addons.txt; fi
|
| 48 |
+
RUN pip3 install --no-cache-dir -r requirements.txt
|
| 49 |
+
RUN if [ -f resources/startup/optional-requirements.txt ]; then pip3 install --no-cache-dir -r resources/startup/optional-requirements.txt; fi || true
|
| 50 |
+
|
| 51 |
+
# --- FIX 2: Resolve Crash (Server.py) ---
|
| 52 |
+
# Downgrade FastAPI to be compatible with Pydantic v1 (which your bot likely uses)
|
| 53 |
+
# This fixes: ImportError: cannot import name 'TypeAdapter' from 'pydantic'
|
| 54 |
+
RUN pip3 install "fastapi<0.100.0" "pydantic<2.0.0" uvicorn
|
| 55 |
+
|
| 56 |
+
# Set appropriate permissions
|
| 57 |
+
RUN chown -R 1000:0 /Ult && \
|
| 58 |
+
chmod 777 . && \
|
| 59 |
+
chmod 777 /usr && \
|
| 60 |
+
chown -R 1000:0 /usr && \
|
| 61 |
+
chmod -R 755 /Ult
|
| 62 |
+
|
| 63 |
+
# Expose port for Hugging Face Spaces
|
| 64 |
+
EXPOSE 7860
|
| 65 |
+
|
| 66 |
+
# --- TRICK 3: Auto-delete session + Force IPv4 ---
|
| 67 |
+
# Deletes old sessions AND forces Python to use IPv4 for DNS (helps with connection blocks)
|
| 68 |
+
CMD ["bash", "-c", "echo 'π Cleaning sessions...' && find . -name '*.session' -type f -delete && python3 server.py & python3 bot.py"]
|
README.md
CHANGED
|
@@ -1,10 +1,72 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Telegram Server Manager Bot
|
| 2 |
+
|
| 3 |
+
The Telegram Server Manager Bot is a tool that allows you to remotely manage your Raspberry Pi, PC, or server using Telegram. With this bot, you can run commands from anywhere using your smartphone or computer, without needing to log in to your server directly. This can be especially useful if you want to check on your server's status, restart a service, or troubleshoot an issue while you're away from your desk.
|
| 4 |
+
|
| 5 |
+
## Hugging Face Spaces Deployment
|
| 6 |
+
|
| 7 |
+
This repository is now compatible with Hugging Face Spaces! You can deploy this bot to Hugging Face Spaces for 24/7 availability.
|
| 8 |
+
|
| 9 |
+
### Features for Hugging Face Spaces:
|
| 10 |
+
- β
Runs on Python 3.9.5
|
| 11 |
+
- β
Web interface on port 7860
|
| 12 |
+
- β
Automatic session cleanup
|
| 13 |
+
- β
FastAPI health monitoring
|
| 14 |
+
- β
Support for eval command to run Python code
|
| 15 |
+
|
| 16 |
+
### Deploy to Hugging Face Spaces:
|
| 17 |
+
1. Fork this repository
|
| 18 |
+
2. Create a new Space on Hugging Face
|
| 19 |
+
3. Select "Docker" as the SDK
|
| 20 |
+
4. Connect your GitHub repository
|
| 21 |
+
5. Add your Telegram bot token and admin chat ID as Space secrets
|
| 22 |
+
6. Deploy!
|
| 23 |
+
|
| 24 |
+
## Prerequisites
|
| 25 |
+
|
| 26 |
+
To use the Telegram Server Manager Bot, you will need the following:
|
| 27 |
+
|
| 28 |
+
* A Telegram account.
|
| 29 |
+
* A Telegram bot token. You can generate this by talking to @BotFather on Telegram.
|
| 30 |
+
* Python 3.x installed on your RaspberryPi, PC, or server.
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
## Installation
|
| 34 |
+
|
| 35 |
+
1. Clone this repository to your RaspberryPi, PC, or server.
|
| 36 |
+
|
| 37 |
+
2. Install the required dependencies by running `pip install -r requirements.txt` in the terminal.
|
| 38 |
+
|
| 39 |
+
3. Rename config.example to config and replace YOUR_TOKEN with your Telegram bot token, YOUR_CHAT_ID with your Telegram chat ID, and ADMIN_CID with the chat ID of the admin who is authorized to execute commands.
|
| 40 |
+
|
| 41 |
+
4. Make the bot.py file executable by running chmod +x bot.py in the terminal.
|
| 42 |
+
|
| 43 |
+
5. Run the bot by running ./bot.py in the terminal.
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
## Usage
|
| 47 |
+
|
| 48 |
+
To use the Telegram Server Manager Bot, simply open Telegram and start a chat with your bot. Only the admin can execute commands, so make sure to add the admin chat ID to the configuration file. You can then run any of the built-in commands or enter any Linux command that you want to run on your RaspberryPi, PC, or server.
|
| 49 |
+
|
| 50 |
+
It also supports the following commands:
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
/ping8: Pings 8.8.8.8 and returns the results.
|
| 54 |
+
|
| 55 |
+
/top: Runs the top command and returns the results.
|
| 56 |
+
|
| 57 |
+
/htop: Runs the htop command and returns the results.
|
| 58 |
+
|
| 59 |
+
/eval: Execute Python code and return the result (NEW!)
|
| 60 |
+
|
| 61 |
+
/help - Shows help and usage information.
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
** You can also run any Linux command by simply entering it in the Telegram chat with the bot.
|
| 65 |
+
|
| 66 |
+
## Contributions
|
| 67 |
+
|
| 68 |
+
Contributions to this project are welcome! If you have ideas for new features or improvements, feel free to submit a pull request or open an issue.
|
| 69 |
+
|
| 70 |
+
## License
|
| 71 |
+
|
| 72 |
+
This project is licensed under the MIT License.
|
addons.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Additional add-ons and utilities
|
| 2 |
+
requests
|
| 3 |
+
beautifulsoup4
|
| 4 |
+
lxml
|
| 5 |
+
pillow
|
bot.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
Created on Wed Apr 4 19:06:08 2018
|
| 5 |
+
|
| 6 |
+
@author: mparvin
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import subprocess
|
| 10 |
+
import configparser
|
| 11 |
+
import os
|
| 12 |
+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
| 13 |
+
import logging
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
config = configparser.ConfigParser()
|
| 17 |
+
config.read("config")
|
| 18 |
+
### Get admin chat_id from config file
|
| 19 |
+
### For more security replies only send to admin chat_id
|
| 20 |
+
adminCID = config["SecretConfig"]["admincid"]
|
| 21 |
+
|
| 22 |
+
### Enable logging
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
### This function run command and send output to user
|
| 29 |
+
def runCMD(bot, update):
|
| 30 |
+
if not isAdmin(bot, update):
|
| 31 |
+
return
|
| 32 |
+
usercommand = update.message.text
|
| 33 |
+
cmdProc = subprocess.Popen(
|
| 34 |
+
usercommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
|
| 35 |
+
)
|
| 36 |
+
cmdOut, cmdErr = cmdProc.communicate()
|
| 37 |
+
if cmdOut:
|
| 38 |
+
bot.sendMessage(text=str(cmdOut, "utf-8"), chat_id=adminCID)
|
| 39 |
+
else:
|
| 40 |
+
bot.sendMessage(text=str(cmdErr, "utf-8"), chat_id=adminCID)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
### This function ping 8.8.8.8 and send you result
|
| 44 |
+
def ping8(bot, update):
|
| 45 |
+
if not isAdmin(bot, update):
|
| 46 |
+
return
|
| 47 |
+
cmdOut = str(
|
| 48 |
+
subprocess.check_output(
|
| 49 |
+
"ping 8.8.8.8 -c4", stderr=subprocess.STDOUT, shell=True
|
| 50 |
+
),
|
| 51 |
+
"utf-8",
|
| 52 |
+
)
|
| 53 |
+
bot.sendMessage(text=cmdOut, chat_id=adminCID)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def startCMD(bot, update):
|
| 57 |
+
if not isAdmin(bot, update):
|
| 58 |
+
return
|
| 59 |
+
bot.sendMessage(
|
| 60 |
+
text="Welcome to TSMB bot, this is Linux server/PC manager, Please use /help and read carefully!!",
|
| 61 |
+
chat_id=adminCID,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def helpCMD(bot, update):
|
| 66 |
+
if not isAdmin(bot, update):
|
| 67 |
+
return
|
| 68 |
+
bot.sendMessage(
|
| 69 |
+
text="This bot has access to your server/PC, So it can do anything. Please use Telegram local password to prevent others from accessing to this bot.",
|
| 70 |
+
chat_id=adminCID,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def evalCMD(bot, update):
|
| 75 |
+
"""Execute Python code and return the result
|
| 76 |
+
|
| 77 |
+
SECURITY NOTE: This command allows arbitrary Python code execution.
|
| 78 |
+
It is restricted to admin users only via isAdmin() check.
|
| 79 |
+
This is intentional for remote server management but should be used
|
| 80 |
+
with caution. Only authorized administrators should have access.
|
| 81 |
+
"""
|
| 82 |
+
if not isAdmin(bot, update):
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
# Get the Python code from the message (remove /eval command)
|
| 86 |
+
code = update.message.text.replace("/eval", "", 1).strip()
|
| 87 |
+
|
| 88 |
+
if not code:
|
| 89 |
+
bot.sendMessage(
|
| 90 |
+
text="Please provide Python code to evaluate.\nUsage: /eval <python_code>",
|
| 91 |
+
chat_id=adminCID,
|
| 92 |
+
)
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
# Create a safe namespace for eval
|
| 97 |
+
namespace = {
|
| 98 |
+
'__builtins__': __builtins__,
|
| 99 |
+
'os': os,
|
| 100 |
+
'subprocess': subprocess,
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# Try to evaluate as expression first
|
| 104 |
+
try:
|
| 105 |
+
result = eval(code, namespace)
|
| 106 |
+
output = str(result)
|
| 107 |
+
except SyntaxError:
|
| 108 |
+
# If it fails, try to execute as statement
|
| 109 |
+
exec(code, namespace)
|
| 110 |
+
output = "Code executed successfully (no return value)"
|
| 111 |
+
|
| 112 |
+
bot.sendMessage(text=f"β
Result:\n{output}", chat_id=adminCID)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
bot.sendMessage(text=f"β Error:\n{type(e).__name__}: {str(e)}", chat_id=adminCID)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def topCMD(bot, update):
|
| 118 |
+
if not isAdmin(bot, update):
|
| 119 |
+
return
|
| 120 |
+
cmdOut = str(subprocess.check_output("top -n 1", shell=True), "utf-8")
|
| 121 |
+
bot.sendMessage(text=cmdOut, chat_id=adminCID)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def HTopCMD(bot, update):
|
| 125 |
+
## Is this user admin?
|
| 126 |
+
if not isAdmin(bot, update):
|
| 127 |
+
return
|
| 128 |
+
## Checking requirements on your system
|
| 129 |
+
htopCheck = subprocess.call(["which", "htop"])
|
| 130 |
+
if htopCheck != 0:
|
| 131 |
+
bot.sendMessage(
|
| 132 |
+
text="htop is not installed on your system, Please install it first and try again",
|
| 133 |
+
chat_id=adminCID,
|
| 134 |
+
)
|
| 135 |
+
return
|
| 136 |
+
ahaCheck = subprocess.call(["which", "aha"])
|
| 137 |
+
if ahaCheck != 0:
|
| 138 |
+
bot.sendMessage(
|
| 139 |
+
text="aha is not installed on your system, Please install it first and try again",
|
| 140 |
+
chat_id=adminCID,
|
| 141 |
+
)
|
| 142 |
+
return
|
| 143 |
+
os.system("echo q | htop | aha --black --line-fix > ./htop-output.html")
|
| 144 |
+
with open("./htop-output.html", "rb") as fileToSend:
|
| 145 |
+
bot.sendDocument(document=fileToSend, chat_id=adminCID)
|
| 146 |
+
if os.path.exists("./htop-output.html"):
|
| 147 |
+
os.remove("./htop-output.html")
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def error(bot, update, error):
|
| 151 |
+
"""Log Errors caused by Updates."""
|
| 152 |
+
logger.warning('Update "%s" caused error "%s"', update, error)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def isAdmin(bot, update):
|
| 156 |
+
chat_id = update.message.chat_id
|
| 157 |
+
if str(chat_id) == adminCID:
|
| 158 |
+
return True
|
| 159 |
+
|
| 160 |
+
update.message.reply_text(
|
| 161 |
+
"You cannot use this bot, because you are not Admin!!!!"
|
| 162 |
+
)
|
| 163 |
+
alertMessage = """Some one try to use this bot with this information:\n chat_id is {} and username is {} """.format(
|
| 164 |
+
update.message.chat_id, update.message.from_user.username
|
| 165 |
+
)
|
| 166 |
+
bot.sendMessage(text=alertMessage, chat_id=adminCID)
|
| 167 |
+
return False
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def main():
|
| 171 |
+
updater = Updater(config["SecretConfig"]["Token"])
|
| 172 |
+
dp = updater.dispatcher
|
| 173 |
+
|
| 174 |
+
dp.add_handler(CommandHandler("start", startCMD))
|
| 175 |
+
dp.add_handler(CommandHandler("ping8", ping8))
|
| 176 |
+
dp.add_handler(CommandHandler("top", topCMD))
|
| 177 |
+
dp.add_handler(CommandHandler("htop", HTopCMD))
|
| 178 |
+
dp.add_handler(CommandHandler("help", helpCMD))
|
| 179 |
+
dp.add_handler(CommandHandler("eval", evalCMD))
|
| 180 |
+
dp.add_handler(MessageHandler(Filters.text, runCMD))
|
| 181 |
+
|
| 182 |
+
dp.add_error_handler(error)
|
| 183 |
+
updater.start_polling()
|
| 184 |
+
updater.idle()
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
if __name__ == "__main__":
|
| 188 |
+
main()
|
config.example
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[SecretConfig]
|
| 2 |
+
token = 193025875:AAHZ3hIanIau-Hg04B-mZREFBjLl6GvM9fk
|
| 3 |
+
admincid = 131728488
|
| 4 |
+
|
reqs.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core telegram requirements
|
| 2 |
+
python-telegram-bot==10.0.1
|
| 3 |
+
configparser==4.0.2
|
| 4 |
+
certifi==2024.7.4
|
| 5 |
+
future==0.18.3
|
| 6 |
+
|
| 7 |
+
# Web server requirements
|
| 8 |
+
fastapi<0.100.0
|
| 9 |
+
pydantic<2.0.0
|
| 10 |
+
uvicorn
|
| 11 |
+
|
| 12 |
+
# Additional utilities
|
| 13 |
+
redis
|
| 14 |
+
aiohttp
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
certifi==2024.7.4
|
| 2 |
+
future==0.18.3
|
| 3 |
+
configparser==4.0.2
|
| 4 |
+
python-telegram-bot==10.0.1
|
server.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
Simple FastAPI server for Hugging Face Spaces
|
| 5 |
+
This keeps the space alive and provides a basic web interface
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from fastapi.responses import HTMLResponse
|
| 10 |
+
import uvicorn
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
@app.get("/", response_class=HTMLResponse)
|
| 16 |
+
async def root():
|
| 17 |
+
"""Root endpoint that returns a simple HTML page"""
|
| 18 |
+
return """
|
| 19 |
+
<!DOCTYPE html>
|
| 20 |
+
<html>
|
| 21 |
+
<head>
|
| 22 |
+
<title>Telegram Server Manager Bot</title>
|
| 23 |
+
<style>
|
| 24 |
+
body {
|
| 25 |
+
font-family: Arial, sans-serif;
|
| 26 |
+
max-width: 800px;
|
| 27 |
+
margin: 50px auto;
|
| 28 |
+
padding: 20px;
|
| 29 |
+
background-color: #f5f5f5;
|
| 30 |
+
}
|
| 31 |
+
.container {
|
| 32 |
+
background-color: white;
|
| 33 |
+
padding: 30px;
|
| 34 |
+
border-radius: 10px;
|
| 35 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 36 |
+
}
|
| 37 |
+
h1 {
|
| 38 |
+
color: #333;
|
| 39 |
+
}
|
| 40 |
+
.status {
|
| 41 |
+
color: #28a745;
|
| 42 |
+
font-weight: bold;
|
| 43 |
+
}
|
| 44 |
+
.info {
|
| 45 |
+
background-color: #e7f3ff;
|
| 46 |
+
padding: 15px;
|
| 47 |
+
border-radius: 5px;
|
| 48 |
+
margin: 20px 0;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
</head>
|
| 52 |
+
<body>
|
| 53 |
+
<div class="container">
|
| 54 |
+
<h1>π€ Telegram Server Manager Bot</h1>
|
| 55 |
+
<p class="status">β
Bot is running!</p>
|
| 56 |
+
<div class="info">
|
| 57 |
+
<h3>About this bot:</h3>
|
| 58 |
+
<p>This bot allows you to remotely manage your server using Telegram.</p>
|
| 59 |
+
<ul>
|
| 60 |
+
<li>Run commands via Telegram</li>
|
| 61 |
+
<li>Monitor server status</li>
|
| 62 |
+
<li>Check system resources</li>
|
| 63 |
+
</ul>
|
| 64 |
+
<p><strong>Start a chat with your bot on Telegram to use it!</strong></p>
|
| 65 |
+
</div>
|
| 66 |
+
</div>
|
| 67 |
+
</body>
|
| 68 |
+
</html>
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
@app.get("/health")
|
| 72 |
+
async def health():
|
| 73 |
+
"""Health check endpoint"""
|
| 74 |
+
return {"status": "healthy", "bot": "running"}
|
| 75 |
+
|
| 76 |
+
@app.get("/api/status")
|
| 77 |
+
async def status():
|
| 78 |
+
"""Status endpoint"""
|
| 79 |
+
return {
|
| 80 |
+
"status": "online",
|
| 81 |
+
"service": "Telegram Server Manager Bot",
|
| 82 |
+
"port": 7860
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
# Run on port 7860 for Hugging Face Spaces
|
| 87 |
+
port = int(os.environ.get("PORT", 7860))
|
| 88 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
start.sh
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
set -e # Exit on error
|
| 4 |
+
|
| 5 |
+
echo "π Starting Telegram Server Manager Bot..."
|
| 6 |
+
echo "π Cleaning old session files..."
|
| 7 |
+
|
| 8 |
+
# Delete old session files
|
| 9 |
+
find . -name "*.session" -type f -delete || echo "No session files to clean"
|
| 10 |
+
|
| 11 |
+
echo "π Starting web server..."
|
| 12 |
+
# Start the FastAPI server in the background
|
| 13 |
+
python3 server.py &
|
| 14 |
+
SERVER_PID=$!
|
| 15 |
+
|
| 16 |
+
# Check if server started successfully
|
| 17 |
+
sleep 2
|
| 18 |
+
if ! kill -0 $SERVER_PID 2>/dev/null; then
|
| 19 |
+
echo "β Failed to start web server"
|
| 20 |
+
exit 1
|
| 21 |
+
fi
|
| 22 |
+
|
| 23 |
+
echo "β
Web server started (PID: $SERVER_PID)"
|
| 24 |
+
echo "π€ Starting Telegram bot..."
|
| 25 |
+
|
| 26 |
+
# Start the Telegram bot
|
| 27 |
+
python3 bot.py
|