Telegram Bots y Docker

Share

Os dejo una aplicación muy chorra para obtener el estado de tus dockers a través de un bot muy simple de telegram:

 

[code language="python"]
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import os

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
 level=logging.INFO)

logger = logging.getLogger(__name__)


def help(bot, update):
 bot.sendMessage(update.message.chat_id, text='Help!')


def stats(bot, update):
 bot.sendMessage(update.message.chat_id, text='ahora lo programo')
 os.system('docker stats --no-stream > /tmp/stats.tmp')

 filesize=os.path.getsize('/tmp/stats.tmp')
 if filesize > 0:
 fd = open('/tmp/stats.tmp')
 string=fd.read()
 bot.sendMessage(update.message.chat_id,text=string)
 fd.close()
 os.system('rm /tmp/stats.tmp')

def error(bot, update, error):
 logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
 # Create the EventHandler and pass it your bot's token.
 updater = Updater("740925671:tyhejsio2hustqmckopsr567a-F0k23GG9E")

 # Get the dispatcher to register handlers
 dp = updater.dispatcher




 # on different commands - answer in Telegram
 dp.add_handler(CommandHandler("stats", stats))

 # log all errors
 dp.add_error_handler(error)

 # Start the Bot
 updater.start_polling()

 # Run the bot until the you presses Ctrl-C or the process receives SIGINT,
 # SIGTERM or SIGABRT. This should be used most of the time, since
 # start_polling() is non-blocking and will stop the bot gracefully.
 updater.idle()


if __name__ == '__main__':
 main()

[/code]

 

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *