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 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
''' $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 send_telegram(fullmsg, chatid=chatid)