@@ -132,20 +132,23 @@ def sendEvent(self, message, devicename, signalname, priority):
132132 ptext = ['_Information_' , '*Warnung*' , '*_Fehler_*' ][priority ]
133133 message = translate ('RTOC' , '{} von {}.{}:\n {}' ).format (ptext , devicename , signalname , message )
134134 for id in self .logger .config ['telegram' ]['chat_ids' ].keys ():
135+ self .check_chat_id (id )
135136 if priority >= self .logger .config ['telegram' ]['chat_ids' ][id ]['eventlevel' ]:
136- try :
137- self .bot .send_message (chat_id = int (id ), text = message ,
138- parse_mode = ParseMode .MARKDOWN )
139- except Exception :
140- self .bot .send_message (chat_id = int (id ), text = message )
137+ self .send_message (chat_id = int (id ), text = message , delete = False )
138+ # try:
139+ # self.bot.send_message(chat_id=int(id), text=message,
140+ # parse_mode=ParseMode.MARKDOWN)
141+ # except Exception:
142+ # self.bot.send_message(chat_id=int(id), text=message)
141143
142144 def send_message_to_all (self , message ):
143145 for id in self .logger .config ['telegram' ]['chat_ids' ].keys ():
144- try :
145- self .bot .send_message (chat_id = int (id ), text = message ,
146- parse_mode = ParseMode .MARKDOWN )
147- except Exception :
148- self .bot .send_message (chat_id = int (id ), text = message )
146+ self .send_message (chat_id = int (id ), text = message , delete = False )
147+ # try:
148+ # self.bot.send_message(chat_id=int(id), text=message,
149+ # parse_mode=ParseMode.MARKDOWN)
150+ # except Exception:
151+ # self.bot.send_message(chat_id=int(id), text=message)
149152
150153 def connect (self ):
151154 idler = Thread (target = self .connectThread )
@@ -233,6 +236,9 @@ def build_menu(self, buttons, n_cols, header_buttons=None, footer_buttons=None):
233236 return menu
234237
235238 def send_message (self , chat_id , text , parse_mode = ParseMode .MARKDOWN , disable_notification = True , delete = True ):
239+ if self .logger .config ['telegram' ]['onlyAdmin' ] and not self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ]:
240+ logging .info ('Aborted telegram answer, because telegram-option "onlyAdmin" is active.' )
241+ return
236242 try :
237243 lastMessage = self .bot .send_message (
238244 chat_id , text = text , disable_notification = True , parse_mode = ParseMode .MARKDOWN )
@@ -250,7 +256,9 @@ def send_message(self, chat_id, text, parse_mode=ParseMode.MARKDOWN, disable_not
250256 self .bot .delete_message (chat_id , message_id )
251257
252258 def sendMenuMessage (self , bot , chat_id , buttonlist , text = '' , description_text = '' , n_cols = 1 , backButton = True ):
253-
259+ if self .logger .config ['telegram' ]['onlyAdmin' ] and not self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ]:
260+ logging .info ('Aborted telegram answer, because telegram-option "onlyAdmin" is active.' )
261+ return
254262 if backButton :
255263 buttonlist .append (BACKBUTTON )
256264 if not self .logger .config ['telegram' ]['inlineMenu' ]:
@@ -382,7 +390,15 @@ def textHandler(self, bot, update, altStrung=None, alt_chat_id=None):
382390
383391 def menuHandler (self , bot , chat_id ):
384392 self .mode [chat_id ] = 'menu'
385- commands = copy .deepcopy (list (self .userActions .keys ()))
393+ # commands = copy.deepcopy(list(self.userActions.keys()))
394+ commands = []
395+ admin = self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ]
396+ for c in self .userActions .keys ():
397+ if c .startswith ('_' ):
398+ if admin :
399+ commands += [c ]
400+ else :
401+ commands += [c ]
386402 commands += self .createShortcutList (bot , chat_id )
387403 commands += self .menuCommands
388404 if self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ]:
@@ -406,15 +422,14 @@ def menuHandlerAns(self, bot, chat_id, strung):
406422 self .createEventHandler (bot , chat_id )
407423 elif idx == 5 :
408424 self .automationHandler (bot , chat_id )
409- elif self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ]:
410- if strung in self .adminMenuCommands :
411- idx = self .adminMenuCommands .index (strung )
412- if idx == 0 :
413- self .settingsHandler (bot , chat_id )
414425 elif strung in list (self .userActions .keys ()):
415426 self .executeUserAction (bot , chat_id , strung )
416427 elif strung in self .createShortcutList (bot , chat_id ):
417428 self .callShortcut (bot , chat_id , strung )
429+ elif self .logger .config ['telegram' ]['chat_ids' ][str (chat_id )]['admin' ] and strung in self .adminMenuCommands :
430+ idx = self .adminMenuCommands .index (strung )
431+ if idx == 0 :
432+ self .settingsHandler (bot , chat_id )
418433 else :
419434 self .menuHandler (bot , chat_id )
420435
@@ -762,9 +777,10 @@ def signalsHandlerAns(self, bot, chat_id, strung):
762777 range = list (self .signals_range [chat_id ])
763778 self .send_message (chat_id = chat_id ,
764779 text = translate ('RTOC' , 'Ich erzeuge jetzt einen Graphen mit {} Signalen.\n Das kann eine Weile dauern' ).format (len (plot_signals )))
765- t = Thread (target = self .sendSignalPlot , args = (
766- bot , chat_id , plot_signals , * range ))
767- t .start ()
780+ # t = Thread(target=self.sendSignalPlot, args=(
781+ # bot, chat_id, plot_signals, *range))
782+ # t.start()
783+ self .sendSignalPlot (bot , chat_id , plot_signals , * range )
768784 # self._teleThreads.append(t)
769785 # self.signalsHandler(bot, chat_id)
770786 else :
@@ -1044,11 +1060,14 @@ def settingsHandler(self, bot, chat_id):
10441060 if self .logger .config ['postgresql' ]['active' ]:
10451061 commands += [translate ('RTOC' , 'Datenbank resamplen' )]
10461062
1047- # if self.logger.config['telegram']['active']:
1048- # commands += [translate('RTOC', "*Telegram-Bot: An (!)*")]
1049- # else:
1050- # commands += [translate('RTOC', "Telegram-Bot: Aus")]
1051-
1063+ if self .logger .config ['telegram' ]['active' ]:
1064+ commands += [translate ('RTOC' , "*Telegram-Bot: An (!)*" )]
1065+ else :
1066+ commands += [translate ('RTOC' , "Telegram-Bot: Aus" )]
1067+ if self .logger .config ['telegram' ]['onlyAdmin' ]:
1068+ commands += [translate ('RTOC' , "OnlyAdmin: An" )]
1069+ else :
1070+ commands += [translate ('RTOC' , "OnlyAdmin: Aus" )]
10521071 ipadress = urllib .request .urlopen ('https://ident.me' ).read ().decode ('utf8' )
10531072 ipadress = translate ('RTOC' , '\n IP-Adresse: {}' ).format (ipadress )
10541073 obj_Disk = psutil .disk_usage ('/' )
@@ -1135,6 +1154,16 @@ def settingsHandlerAns(self, bot, chat_id, strung):
11351154 ok = self .logger .toggleTelegramBot (True )
11361155 start = True
11371156 name = 'Telegram-Bot'
1157+ elif strung == translate ('RTOC' , "OnlyAdmin: An" ):
1158+ self .logger .config ['telegram' ]['onlyAdmin' ] = False
1159+ ok = True
1160+ start = False
1161+ name = 'OnlyAdmin-Modus'
1162+ elif strung == translate ('RTOC' , "OnlyAdmin: Aus" ):
1163+ self .logger .config ['telegram' ]['onlyAdmin' ] = True
1164+ ok = True
1165+ start = True
1166+ name = 'OnlyAdmin-Modus'
11381167 elif strung == translate ('RTOC' , 'Datenbank herunterladen' ):
11391168 path = self .logger .config ['global' ]['documentfolder' ]+ '/'
11401169 filepathes = self .logger .database .exportCSV (path + 'telegram_export' , True )
0 commit comments