@@ -62,7 +62,7 @@ def on_message(self, _client, _userdata, msg):
62
62
new_message = json .loads (msg .payload )
63
63
self .last_message = new_message
64
64
65
- def listen (self , duration , channel = '#' ):
65
+ def listen (self , duration , channel ):
66
66
"""Listen to messages via message broker."""
67
67
68
68
if self .client is None :
@@ -79,3 +79,33 @@ def listen(self, duration, channel='#'):
79
79
80
80
self .client .loop_stop ()
81
81
self .client .disconnect ()
82
+
83
+ ## FUNCTIONS -- HIDDEN
84
+
85
+ def hidden_on_connect (self , _client , _userdata , _flags , _rc ):
86
+ # Subscribe to all channels
87
+ self .client .subscribe (f"bot/{ self .token ['token' ]['unencoded' ]['bot' ]} /#" )
88
+
89
+ def hidden_on_message (self , _client , _userdata , msg ):
90
+ # print channel
91
+ print ('-' * 100 )
92
+ print (f'{ msg .topic } ({ datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )} )\n ' )
93
+ # print message
94
+ print (json .dumps (json .loads (msg .payload ), indent = 4 ))
95
+
96
+ def hidden_listen (self ):
97
+ if self .client is None :
98
+ self .connect ()
99
+
100
+ self .client .on_connect = self .hidden_on_connect
101
+ self .client .on_message = self .hidden_on_message
102
+
103
+ # Start loop in a separate thread
104
+ self .client .loop_start ()
105
+
106
+ # Sleep for five seconds to listen for messages
107
+ time .sleep (60 )
108
+
109
+ # Stop loop and disconnect after five seconds
110
+ self .client .loop_stop ()
111
+ self .client .disconnect ()
0 commit comments