Page MenuHomeVyOS Platform

Telegramm integration
Open, WishlistPublicFEATURE REQUEST

Description

We can add a module to the system (import telebot) and allow users to create scripts for integration with Telegram bots. And offer instructions with boz bots for example:

@vyossentrbot

#!/usr/bin/env python3

import telebot
import psutil
import time
import datetime
import uptime

# Your bot token obtained from BotFather
TOKEN = 'YOUR_BOT_TOKEN_HERE'

# Creating a bot instance
bot = telebot.TeleBot(TOKEN)

# Function to get CPU load information
def get_cpu_load():
    return psutil.cpu_percent(interval=1)

# Function to get available RAM information
def get_available_ram():
    return psutil.virtual_memory().available / (1024 * 1024)

# Function to get available HDD space information
def get_available_hdd():
    return psutil.disk_usage('/').free / (1024 * 1024 * 1024)

# Function to get system uptime
def get_system_uptime():
    return uptime.uptime()

# Handler for the /start command
@bot.message_handler(commands=['start'])
def send_welcome(message):
    commands_list = "Available commands:\n/log - Show last 10 lines of syslog\n/cpu - Show CPU load\n/ram - Show available RAM\n/hdd - Show available HDD space\n/uptime - Show system uptime\n/help - Show command list"
    bot.reply_to(message, f"Hello! I'm a bot that provides information about your system.\n\n{commands_list}")

# Function to read last lines from the log
def read_last_log_lines():
    try:
        with open('/var/log/syslog', 'r') as log_file:
            lines = log_file.readlines()
            last_lines = lines[-10:]
            return ''.join(last_lines)
    except FileNotFoundError:
        return "Log file not found."

# Handler for the /cpu command
@bot.message_handler(commands=['cpu'])
def send_cpu_load(message):
    cpu_load = get_cpu_load()
    bot.reply_to(message, f"CPU load: {cpu_load}%")

# Handler for the /ram command
@bot.message_handler(commands=['ram'])
def send_ram_info(message):
    available_ram = get_available_ram()
    bot.reply_to(message, f"Available RAM: {available_ram:.2f} MB")

# Handler for the /hdd command
@bot.message_handler(commands=['hdd'])
def send_hdd_info(message):
    available_hdd = get_available_hdd()
    bot.reply_to(message, f"Available HDD space: {available_hdd:.2f} GB")

# Handler for the /uptime command
@bot.message_handler(commands=['uptime'])
def send_system_uptime(message):
    uptime_seconds = get_system_uptime()
    uptime_str = str(datetime.timedelta(seconds=uptime_seconds))
    bot.reply_to(message, f"System uptime: {uptime_str}")

# Handler for the /help command
@bot.message_handler(commands=['help'])
def send_help(message):
    commands_list = "Available commands:\n/cpu - Show CPU load\n/ram - Show available RAM\n/hdd - Show available HDD space\n/log - Show last 10 lines of log\n/uptime - Show system uptime\n/help - Show command list"
    bot.reply_to(message, commands_list)

# Handler for the /log command
@bot.message_handler(commands=['log'])
def send_last_log_lines(message):
    last_log_lines = read_last_log_lines()
    bot.reply_to(message, last_log_lines)

# Function to start the bot
def start_bot():
    while True:
        try:
            bot.polling(none_stop=True)
        except Exception as e:
            print(f"Error: {e}")
            time.sleep(5)

if __name__ == "__main__":
    start_bot()

Details

Difficulty level
Unknown (require assessment)
Version
-
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Unspecified (possibly destroys the router)
Issue type
Unspecified (please specify)