@@ -1,103 +1,103 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # -*- coding: utf-8 -*- |
|
3 | 3 | |
|
4 | 4 | from telegram.ext import Updater, CommandHandler, MessageHandler, Filters |
|
5 | 5 | import logging, os, json, time, requests, docker |
|
6 | 6 | |
|
7 | 7 | IDS_VALUE={'ids': list(map(int, os.getenv('SOPHY_IDS_TELEGRAM', '9999999999,0000000000').split(',')))} |
|
8 | 8 | |
|
9 | 9 | # Enable logging |
|
10 | 10 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
|
11 | 11 | logger = logging.getLogger(__name__) |
|
12 | 12 | |
|
13 | 13 | def restart_schain(update, context): |
|
14 | 14 | """Send a message when the command /restart_schain is issued.""" |
|
15 | 15 | |
|
16 | 16 | if update.to_dict()['message']['from']['id'] in IDS_VALUE['ids']: |
|
17 | 17 | |
|
18 | 18 | response_status = requests.get('http://sophy-schain/status') |
|
19 | 19 | |
|
20 | 20 | if response_status.status_code == 200: |
|
21 | update.message.reply_text('Experiment name: %s' % (response_status.json()['name'])) | |
|
22 | time.sleep(1) | |
|
23 | update.message.reply_text('Parameters: %s' % (response_status.json()['parameters'])) | |
|
24 | time.sleep(1) | |
|
25 | update.message.reply_text('Range: %s' % (response_status.json()['range'])) | |
|
26 | time.sleep(1) | |
|
27 | ||
|
28 | 21 | if response_status.json()['running']: |
|
22 | update.message.reply_text('Experiment name: %s' % (response_status.json()['name'])) | |
|
23 | time.sleep(1) | |
|
24 | update.message.reply_text('Parameters: %s' % (response_status.json()['online']['polarimetric'])) | |
|
25 | time.sleep(1) | |
|
26 | update.message.reply_text('Range: %s' % (response_status.json()['online']['range'])) | |
|
27 | time.sleep(1) | |
|
29 | 28 | #-------------------# |
|
30 | 29 | try: |
|
31 | 30 | docker_client = docker.from_env() |
|
32 | 31 | docker_client.containers.get("sirm-schain").restart() |
|
33 | 32 | update.message.reply_text('sirm-schain container restared!') |
|
34 | 33 | except: |
|
35 | 34 | update.message.reply_text('ERROR: sirm-schain container not restared!') |
|
36 | 35 | time.sleep(1) |
|
37 | 36 | #-------------------# |
|
38 | response_schain = requests.post('http://sophy-schain/start', json={'name': response_status.json()['name']}) | |
|
37 | #response_schain = requests.post('http://sophy-schain/start', json={'name': response_status.json()['name']}) | |
|
38 | response_schain = requests.post('http://sophy-schain/start', json=response_status.json()) | |
|
39 | 39 | if response_schain.status_code == 200: |
|
40 | 40 | update.message.reply_text('Done: schain has restared!') |
|
41 | 41 | else: |
|
42 | 42 | update.message.reply_text('Fail: Please, try again!') |
|
43 | 43 | else: |
|
44 | 44 | update.message.reply_text("Experiment isn't running") |
|
45 | 45 | else: |
|
46 | 46 | update.message.reply_text("ERROR: sirm-schain not running") |
|
47 | 47 | else: |
|
48 | 48 | update.message.reply_text('ERROR: You must be authorized to use this action') |
|
49 | 49 | |
|
50 | 50 | #def start_schain(update, context): |
|
51 | 51 | # """Send a message when the command /start is issued.""" |
|
52 | 52 | # update.message.reply_text('Hello, please write /help for to use the options!') |
|
53 | 53 | |
|
54 | 54 | def help_schain(update, context): |
|
55 | 55 | update.message.reply_text('Commands used:') |
|
56 | 56 | time.sleep(1) |
|
57 | 57 | update.message.reply_text('/restart_schain -> Restart schain') |
|
58 | 58 | #time.sleep(1) |
|
59 | 59 | #update.message.reply_text('/show_exp -> Show experiment name') |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | #def show_exp(update, context): |
|
63 | 63 | # |
|
64 | 64 | # if update.message.chat.id in IDS_VALUE[update.message.chat.type]: |
|
65 | 65 | # response_status = requests.get('http://sophy/status') |
|
66 | 66 | # if response_status.status_code == 200: |
|
67 | 67 | # update.message.reply_text(str(response_status.json()['name'])) |
|
68 | 68 | # else: |
|
69 | 69 | # update.message.reply_text("No experiment is running") |
|
70 | 70 | # |
|
71 | 71 | # else: |
|
72 | 72 | # update.message.reply_text('ERROR: You must be authorized to use this action') |
|
73 | 73 | |
|
74 | 74 | def error(update, context): |
|
75 | 75 | """Log Errors caused by Updates.""" |
|
76 | 76 | logger.warning('Update "%s" caused error "%s"', update, context.error) |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def main(): |
|
80 | 80 | updater = Updater("6026493035:AAE8vTbZqDWxu82nE8dPSMJKo4CiILIWxbs", use_context=True) |
|
81 | 81 | |
|
82 | 82 | # Get the dispatcher to register handlers |
|
83 | 83 | dp = updater.dispatcher |
|
84 | 84 | |
|
85 | 85 | # on different commands - answer in Telegram |
|
86 | 86 | #dp.add_handler(CommandHandler("start_schain", start_schain)) |
|
87 | 87 | #dp.add_handler(CommandHandler("show_exp", show_exp)) |
|
88 | 88 | dp.add_handler(CommandHandler("help_schain", help_schain)) |
|
89 | 89 | dp.add_handler(CommandHandler("restart_schain", restart_schain)) |
|
90 | 90 | |
|
91 | 91 | # log all errors |
|
92 | 92 | dp.add_error_handler(error) |
|
93 | 93 | |
|
94 | 94 | # Start the Bot |
|
95 | 95 | updater.start_polling() |
|
96 | 96 | |
|
97 | 97 | # Run the bot until you press Ctrl-C or the process receives SIGINT, |
|
98 | 98 | # SIGTERM or SIGABRT. This should be used most of the time, since |
|
99 | 99 | # start_polling() is non-blocking and will stop the bot gracefully. |
|
100 | 100 | updater.idle() |
|
101 | 101 | |
|
102 | 102 | if __name__ == '__main__': |
|
103 | 103 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now