def subscribe(self): ''' subscribe to the list of recipients for alarms ''' known_users = get_TelegramAddresses() alarm_recipients = get_alarm_recipients() msg = '' user = '' if self.chat_id in known_users.keys(): user = known_users[self.chat_id] msg = 'Hi %s,\n' % user if self.chat_id not in alarm_recipients: alarm_recipients_file = telegram_datafile( 'telegram-alarm-recipients') if alarm_recipients_file is None: msg = 'ERROR! Could not find the alarm recipients list.' self._send_message(msg) return h = open(alarm_recipients_file, 'a') newline = '%s %s\n' % (self.chat_id, user) h.write(newline) h.close() msg += 'You are subscribed to the list of alarm recipients.' msg += '\nYou will receive a message in case of problems with the pulse tubes, or with the UPS (220V power supply)' self._send_message(msg) return
def _default_answer(self): ''' the default reply to unknown commands ''' ans = "I don't understand." known_users = get_TelegramAddresses() if self.chat_id in known_users.keys(): ans = 'Sorry %s, %s' % (known_users[self.chat_id], ans) self._send_message(ans) return
def _respuesta(self, msg): ''' this is the message receiver ''' if 'chat' in msg.keys(): self.chat_id = msg['chat']['id'] else: self.chat_id = 0xFFFFFFFF if 'text' in msg.keys(): cmd = msg['text'] else: cmd = 'NONE' cmd_list = cmd.split() if len(cmd_list) > 1: self._parseargs(cmd_list[1:]) now = dt.datetime.utcnow() user = '******' known_users = get_TelegramAddresses() if self.chat_id in known_users.keys(): user = known_users[self.chat_id] msg = "%s %i %16s %s" % (now.strftime( self.time_fmt), self.chat_id, user, cmd) print(msg) h = open('bot.log', 'a') h.write(msg + '\n') h.close() if len(cmd_list) > 0: run_cmd = cmd_list[0].lower() if run_cmd.find('/') != 0: run_cmd = '/' + run_cmd if run_cmd in self.commands.keys(): self.commands[run_cmd]() else: self._default_answer() else: self.commands['/start']() return
def unsubscribe(self): ''' remove user from the list of alarm recipients ''' known_users = get_TelegramAddresses() alarm_recipients = get_alarm_recipients() msg = '' user = '' if self.chat_id in alarm_recipients: alarm_recipients_file = telegram_datafile( 'telegram-alarm-recipients') if alarm_recipients_file is None: msg = 'ERROR! Could not find the alarm recipients list.' self._send_message(msg) return h = open(alarm_recipients_file, 'w') if alarm_recipients is None: alarm_recipients = [self.chat_id] for chat_id in alarm_recipients: if chat_id == self.chat_id: continue if chat_id in known_users.keys(): newline = '%s %s\n' % (chat_id, known_users[chat_id]) else: newline = '%s\n' % chat_id h.write(newline) h.close() notmsg = 'no longer' else: notmsg = 'not' if self.chat_id in known_users.keys(): user = known_users[self.chat_id] msg = 'Hi %s,\n' % user msg += 'You are %s subscribed to the list of alarm recipients.' % notmsg msg += '\nYou will not receive messages in case of problems with the pulse tubes, or with the UPS (220V power supply)' self._send_message(msg) return
#!/usr/bin/env python3 ''' $Id: test_alarm.py $auth: Steve Torchinsky <*****@*****.**> $created: Sun 05 Dec 2021 19:28:38 CET $license: GPLv3 or later, see https://www.gnu.org/licenses/gpl-3.0.txt This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. test the Telegram alarm ''' from qubichk.send_telegram import send_telegram, get_alarm_recipients, get_TelegramAddresses known_users = get_TelegramAddresses() alarm_recipients = get_alarm_recipients() msg = "\n\nYou can now subscribe or unsubscribe yourself from the list of recipients for alarms" msg += " by using the commands 'subscribe' and 'unsubscribe'. You are currently subscribed." msg += "\n\nI will check the compressor status every minute, and the UPS status every minute." msg += " If there's a problem, I'll send you a telegram every minute until the problem is resolved," msg += " or until you unsubscribe from the list." msg += "\n\nBest regards from your friend," msg += "\nQUBIC" for chatid in alarm_recipients: if chatid in known_users.keys(): fullmsg = 'Hi %s!' % known_users[chatid] else: fullmsg = 'Hi!' fullmsg += '\n' + msg