##// END OF EJS Templates
piura :: telegram schain restart file [bug] - script
piura :: telegram schain restart file [bug] - script

File last commit:

r494:3b4213a1f18f
r494:3b4213a1f18f
Show More
telegram_restart.py
103 lines | 4.2 KiB | text/x-python | PythonLexer
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging, os, json, time, requests, docker
IDS_VALUE={'ids': list(map(int, os.getenv('SOPHY_IDS_TELEGRAM', '9999999999,0000000000').split(',')))}
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def restart_schain(update, context):
"""Send a message when the command /restart_schain is issued."""
if update.to_dict()['message']['from']['id'] in IDS_VALUE['ids']:
response_status = requests.get('http://sophy-schain/status')
if response_status.status_code == 200:
if response_status.json()['running']:
update.message.reply_text('Experiment name: %s' % (response_status.json()['name']))
time.sleep(1)
update.message.reply_text('Parameters: %s' % (response_status.json()['online']['polarimetric']))
time.sleep(1)
update.message.reply_text('Range: %s' % (response_status.json()['online']['range']))
time.sleep(1)
#-------------------#
try:
docker_client = docker.from_env()
docker_client.containers.get("sirm-schain").restart()
update.message.reply_text('sirm-schain container restared!')
except:
update.message.reply_text('ERROR: sirm-schain container not restared!')
time.sleep(1)
#-------------------#
#response_schain = requests.post('http://sophy-schain/start', json={'name': response_status.json()['name']})
response_schain = requests.post('http://sophy-schain/start', json=response_status.json())
if response_schain.status_code == 200:
update.message.reply_text('Done: schain has restared!')
else:
update.message.reply_text('Fail: Please, try again!')
else:
update.message.reply_text("Experiment isn't running")
else:
update.message.reply_text("ERROR: sirm-schain not running")
else:
update.message.reply_text('ERROR: You must be authorized to use this action')
#def start_schain(update, context):
# """Send a message when the command /start is issued."""
# update.message.reply_text('Hello, please write /help for to use the options!')
def help_schain(update, context):
update.message.reply_text('Commands used:')
time.sleep(1)
update.message.reply_text('/restart_schain -> Restart schain')
#time.sleep(1)
#update.message.reply_text('/show_exp -> Show experiment name')
#def show_exp(update, context):
#
# if update.message.chat.id in IDS_VALUE[update.message.chat.type]:
# response_status = requests.get('http://sophy/status')
# if response_status.status_code == 200:
# update.message.reply_text(str(response_status.json()['name']))
# else:
# update.message.reply_text("No experiment is running")
#
# else:
# update.message.reply_text('ERROR: You must be authorized to use this action')
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
updater = Updater("6026493035:AAE8vTbZqDWxu82nE8dPSMJKo4CiILIWxbs", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
#dp.add_handler(CommandHandler("start_schain", start_schain))
#dp.add_handler(CommandHandler("show_exp", show_exp))
dp.add_handler(CommandHandler("help_schain", help_schain))
dp.add_handler(CommandHandler("restart_schain", restart_schain))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press 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()