@@ -134,7 +134,7 @@ def check_chat_id(self, chat_id):
134134 first_name = str (chat .first_name )
135135 last_name = str (chat .last_name )
136136 text = translate ('RTOC' , '{} {} joined {} for the first time.' ).format (first_name , last_name , self .logger .config ['global' ]['name' ])
137- self .send_message_to_all (text , onlyAdmin = True )
137+ self .send_message_to_all (text , permission = 'admin' )
138138 logging .info ('TELEGRAM BOT: New client connected with ID: ' + str (chat_id ))
139139 if len (self .telegram_clients .keys ()) == 0 :
140140 ad = 'admin'
@@ -187,28 +187,40 @@ def sendEvent(self, message, devicename, signalname, priority):
187187 # except Exception:
188188 # self.bot.send_message(chat_id=int(id), text=message)
189189
190- def send_message_to_all (self , message , onlyAdmin = False ):
190+ def check_permission_and_priority (self , chat_id , priority , permission ):
191+ self .check_chat_id (chat_id )
192+ perms = ['blocked' ,'read' ,'write' ,'admin' ]
193+ if priority >= self .telegram_clients [chat_id ]['eventlevel' ]:
194+ if permission in perms :
195+ idx = perms .index (self .telegram_clients [chat_id ]['permission' ])
196+ idx2 = perms .index (permission )
197+ if idx >= idx2 :
198+ return True
199+ return False
200+
201+
202+ def send_message_to_all (self , message , priority = 0 , permission = 'write' ):
191203 for id in self .telegram_clients .keys ():
192- if ( onlyAdmin and self .telegram_clients [ id ][ 'permission' ] == 'admin' ) or not onlyAdmin :
204+ if self .check_permission_and_priority ( id , priority , permission ) :
193205 self .send_message (chat_id = int (id ), text = message , delete = False )
194206
195- def send_photo (self , path , onlyAdmin = False ):
207+ def send_photo (self , path , priority = 0 , permission = 'write' ):
196208 try :
197209 for id in self .telegram_clients .keys ():
198- if ( onlyAdmin and self .telegram_clients [ id ][ 'permission' ] == 'admin' ) or not onlyAdmin :
210+ if self .check_permission_and_priority ( id , priority , permission ) :
199211 self .bot .send_photo (chat_id = int (id ), photo = open (path , 'rb' ))
200212 except Exception as error :
201213 text = translate ('RTOC' , 'Error while sending photo:\n {}' ).format (error )
202- self .send_message_to_all (text , onlyAdmin )
214+ self .send_message_to_all (text , priority , permission )
203215
204- def send_document (self , path , onlyAdmin = False ):
216+ def send_document (self , path , priority = 0 , permission = 'write' ):
205217 try :
206218 for id in self .telegram_clients .keys ():
207- if ( onlyAdmin and self .telegram_clients [ id ][ 'permission' ] == 'admin' ) or not onlyAdmin :
219+ if self .check_permission_and_priority ( id , priority , permission ) :
208220 self .bot .send_document (chat_id = int (id ), document = open (path , 'rb' ))
209221 except Exception as error :
210222 text = translate ('RTOC' , 'Error while sending file:\n {}' ).format (error )
211- self .send_message_to_all (text , onlyAdmin )
223+ self .send_message_to_all (text , priority , permission )
212224
213225 def connect (self ):
214226 idler = Thread (target = self .connectThread )
@@ -946,33 +958,7 @@ def signalsHandlerAns(self, bot, chat_id, strung):
946958 self .signals_selected [chat_id ].pop (idx )
947959 self .send_message (chat_id = chat_id ,
948960 text = translate ('RTOC' , 'Signal removed from selection.' ))
949- elif len (a ) == 2 : # and not self.get_telegram_client(chat_id, 'signalSubmenu', True):
950- if strung not in self .signals_selected [chat_id ]:
951- sigID = self .logger .database .getSignalID (a [0 ], a [1 ])
952- xmin , xmax , sigLen = self .logger .database .getSignalInfo (sigID )
953- if xmin != None :
954- self .signals_selected [chat_id ].append (strung )
955- t = self .createPlotToolTip (xmin , xmax , sigLen )
956- else :
957- self .signals_selected [chat_id ].append (strung )
958- t = translate ('RTOC' , 'Empty signal' )
959-
960- evs = self .logger .database .getEvents (sigID )
961- if evs == []:
962- evtext = '0'
963- else :
964- evtext = str (len (evs ))+ translate ('RTOC' , '\n Latest Event:\n ' )
965- if evs [0 ][6 ] == 0 :
966- prio = translate ('RTOC' , 'Information' )
967- elif evs [0 ][6 ] == 1 :
968- prio = translate ('RTOC' , 'Warning' )
969- else :
970- prio = translate ('RTOC' , 'Error' )
971961
972- evtext += prio + ': '
973- evtext += evs [0 ][3 ]+ ': ' + dt .datetime .fromtimestamp (evs [0 ][4 ]).strftime ("%d.%m.%Y %H:%M:%S" )
974- self .send_message (chat_id ,
975- translate ('RTOC' , 'Signal selected:\n {}\n Events: {}' ).format (t , evtext ), ParseMode .MARKDOWN , True , False )
976962 elif self .get_telegram_client (chat_id , 'signalSubmenu' , True ) and strung in self .logger .database .deviceNames ():
977963 self .signalsDeviceSubHandler (bot , chat_id , strung )
978964 return
@@ -1034,8 +1020,7 @@ def signalsHandlerAns(self, bot, chat_id, strung):
10341020 self .telegram_clients [str (chat_id )]['signalSubmenu' ] = True
10351021 self .saveClients ()
10361022 else :
1037- self .send_message (chat_id = chat_id ,
1038- text = translate ('RTOC' , 'Signal names consist of\n <Device>.<Signal>\n Your message didn\' t look that way.\n ' ))
1023+ self .selectSignal (bot , chat_id , strung )
10391024 self .signalsHandler (bot , chat_id , True )
10401025 return
10411026
@@ -1048,15 +1033,43 @@ def signalsDeviceSubHandler(self, bot, chat_id, device):
10481033 sname = '.' .join (signalname )
10491034 if sname not in self .signals_selected [chat_id ]:
10501035 commands .append (sname )
1036+ commands = [translate ('RTOC' , 'All' )] + commands
10511037 text = translate ('RTOC' , 'Signals of {}' ).format (device )
10521038 commands .sort ()
10531039 self .sendMenuMessage (bot , chat_id , commands , text )
10541040
10551041 def signalsDeviceSubHandlerAns (self , bot , chat_id , strung ):
1056- a = strung .split ('.' )
10571042 if strung == self .BACKBUTTON :
10581043 self .signalsHandler (bot , chat_id , True )
10591044 return
1045+ elif strung == translate ('RTOC' , 'All' ):
1046+ dev = self .telegram_clients [str (chat_id )]['menu' ].split (':' )[0 ]
1047+ availableSignals = self .logger .database .signalNames (devices = [dev ])
1048+ for signalname in availableSignals :
1049+ sname = '.' .join (signalname )
1050+ if sname not in self .signals_selected [chat_id ] and dev in sname :
1051+ self .signals_selected [chat_id ].append (sname )
1052+ self .send_message (chat_id = chat_id ,
1053+ text = translate ('RTOC' , 'All signals of {} selected.' ).format (dev ))
1054+ else :
1055+ self .selectSignal (bot , chat_id , strung )
1056+ device = self .telegram_clients [str (chat_id )]['menu' ].split (':' )[0 ]
1057+ self .signalsDeviceSubHandler (bot , chat_id , device )
1058+
1059+ def selectSignal (self , bot , chat_id , strung ):
1060+ a = strung .split ('.' )
1061+ if len (a )> 2 :
1062+ self .send_message (chat_id , translate ('RTOC' , 'Please rename this signal. Signals should not contain "." and ":".' ))
1063+ b = ['.' .join (a [:- 2 ]),a [- 1 ]]
1064+ self .selectSignal2 (bot , chat_id , strung , b )
1065+ b = [a [0 ],'.' .join (a [1 :])]
1066+ self .selectSignal2 (bot , chat_id , strung , b )
1067+ elif len (a ) == 2 :
1068+ self .selectSignal2 (bot , chat_id , strung , a )
1069+
1070+
1071+ def selectSignal2 (self , bot , chat_id , strung , a ):
1072+ a = strung .split ('.' )
10601073 if len (a ) == 2 :
10611074 if strung not in self .signals_selected [chat_id ]:
10621075 sigID = self .logger .database .getSignalID (a [0 ], a [1 ])
@@ -1084,11 +1097,10 @@ def signalsDeviceSubHandlerAns(self, bot, chat_id, strung):
10841097 evtext += evs [0 ][3 ]+ ' am ' + dt .datetime .fromtimestamp (evs [0 ][4 ]).strftime ("%d.%m.%Y %H:%M:%S" )
10851098 self .send_message (chat_id ,
10861099 translate ('RTOC' , 'Signal selected:\n {}\n Events: {}' ).format (t , evtext ), ParseMode .MARKDOWN , True , False )
1100+ return True
10871101 else :
1088- self .send_message (chat_id = chat_id ,
1089- text = translate ('RTOC' , 'Signal names consist of\n <Device>.<Signal>\n Your message didn\' t look that way.\n ' ))
1090- device = self .telegram_clients [str (chat_id )]['menu' ].split (':' )[0 ]
1091- self .signalsDeviceSubHandler (bot , chat_id , device )
1102+ return False
1103+
10921104
10931105 def signalsSelectRangeHandler (self , bot , chat_id ):
10941106 self .telegram_clients [str (chat_id )]['menu' ] = "signalSelectRange"
0 commit comments