Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

telegram_server_cheker: adding telegram bot #13

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions moonspeak/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ networks:

services:
#===================================
telegram_server_cheker:
image: "temachpool/moonspeak-telegram_server_cheker:${TAG:-latest}"
environment:
MOONSPEAK_TGBOT_TOKEN: "${MOONSPEAK_TGBOT_TOKEN}"
MOONSPEAK_ADMIN_ID_0: "${MOONSPEAK_ADMIN_ID_0}"
MOONSPEAK_ADMIN_ID_1: "${MOONSPEAK_ADMIN_ID_1}"
networks:
- moonspeaknet
build:
context: ./telegram_server_cheker
volumes:
- /var/run/docker.sock:/var/run/docker.sock

gateway:
image: "temachpool/moonspeak-gateway:${TAG:-latest}"
volumes:
Expand Down
9 changes: 9 additions & 0 deletions moonspeak/telegram_server_cheker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python
WORKDIR /opt/moonspeak/telegram_server_cheker

COPY backend/requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY backend/main.py backend/main.py

WORKDIR /opt/moonspeak/telegram_server_cheker/backend
CMD ["python", "main.py"]
1 change: 1 addition & 0 deletions moonspeak/telegram_server_cheker/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Telegram bot to check server status
58 changes: 58 additions & 0 deletions moonspeak/telegram_server_cheker/backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import telebot
import os
import docker

bot = telebot.TeleBot(os.getenv('MOONSPEAK_TGBOT_TOKEN'))

#list of trust id
admin_ids = [os.getenv('MOONSPEAK_ADMIN_ID_0'),os.getenv('MOONSPEAK_ADMIN_ID_1')]

#return information about the docker containers

# using docker client
client = docker.from_env()


def docker_information():

#return containers list
containers = client.containers.list()

# forming a dictionary with information
containers_name = []
for container in containers:
containers_name.append(client.containers.get(container.id).name)

docker_information = {}
docker_information['containers_len'] = len(containers)
docker_information['containers_name'] = containers_name

return docker_information


#bot
@bot.message_handler(commands=['start'])
def start(message):
#user_id_checking
user_id = message.from_user.id

if str(user_id) not in admin_ids:
bot.send_message(message.chat.id,"No access!")
return

#using def
docker_info = docker_information()

bot.send_message(message.chat.id,"Running containers - {containers_len}".format (containers_len= docker_info['containers_len']))

#forming string from the list
i=0
while i < len(docker_info['containers_name']):
docker_info['containers_name'][i] = '- '+docker_info['containers_name'][i]+'\n'
i+=1
container_names = ''.join(docker_info['containers_name'])
bot.send_message(message.chat.id,'Containers:\n'+container_names)


print('Started')
bot.infinity_polling()
2 changes: 2 additions & 0 deletions moonspeak/telegram_server_cheker/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyTelegramBotAPI==4.10
docker==6.1.0