|
41 | 41 | from analyzers.indicators import candle_recognition |
42 | 42 |
|
43 | 43 | import sys |
| 44 | +from time import sleep |
44 | 45 |
|
45 | 46 | class Notifier(IndicatorUtils): |
46 | 47 | """Handles sending notifications via the configured notifiers |
@@ -141,43 +142,53 @@ def notify_all(self, new_analysis): |
141 | 142 | if not os.path.exists(charts_dir): |
142 | 143 | os.mkdir(charts_dir) |
143 | 144 |
|
144 | | - self.create_charts(messages) |
| 145 | + #self.create_charts(messages) |
| 146 | + for exchange in messages: |
| 147 | + for market_pair in messages[exchange]: |
| 148 | + _messages = messages[exchange][market_pair] |
| 149 | + |
| 150 | + for candle_period in _messages: |
| 151 | + if not isinstance(_messages[candle_period], list) or len (_messages[candle_period]) == 0: |
| 152 | + continue |
| 153 | + |
| 154 | + self.notify_all_messages(exchange, market_pair, candle_period, _messages[candle_period]) |
| 155 | + sleep(4) |
145 | 156 |
|
146 | | - self.notify_slack(new_analysis) |
| 157 | + def notify_all_messages(self, exchange, market_pair, candle_period, messages): |
| 158 | + chart_file = None |
| 159 | + |
| 160 | + if self.enable_charts == True: |
| 161 | + try: |
| 162 | + candles_data = self.all_historical_data[exchange][market_pair][candle_period] |
| 163 | + chart_file = self.create_chart(exchange, market_pair, candle_period, candles_data) |
| 164 | + #self.logger.info('Chart file %s', chart_file) |
| 165 | + except Exception : |
| 166 | + self.logger.info('Error creating chart for %s %s', market_pair, candle_period) |
| 167 | + |
| 168 | + #self.notify_slack(new_analysis) |
147 | 169 | self.notify_discord(messages) |
148 | | - self.notify_twilio(new_analysis) |
149 | | - self.notify_gmail(new_analysis) |
150 | | - self.notify_telegram(messages) |
151 | | - self.notify_webhook(messages) |
| 170 | + self.notify_webhook(messages, chart_file) |
| 171 | + #self.notify_twilio(new_analysis) |
| 172 | + #self.notify_gmail(new_analysis) |
| 173 | + self.notify_telegram(messages, chart_file) |
152 | 174 | self.notify_stdout(messages) |
153 | | - |
| 175 | + |
154 | 176 | def notify_discord(self, messages): |
155 | 177 | """Send a notification via the discord notifier |
156 | 178 |
|
157 | 179 | Args: |
158 | | - messages (dict): The notification messages to send. |
| 180 | + messages (list): List of messages to send for a specific Exchanche/Market Pair/Candle Period |
159 | 181 | """ |
160 | 182 |
|
161 | 183 | if not self.discord_configured: |
162 | 184 | return |
163 | 185 |
|
164 | 186 | message_template = Template(self.notifier_config['discord']['optional']['template']) |
165 | | - |
166 | | - for exchange in messages: |
167 | | - for market_pair in messages[exchange]: |
168 | | - _messages = messages[exchange][market_pair] |
169 | | - |
170 | | - for candle_period in _messages: |
171 | | - if not isinstance(_messages[candle_period], list) or len (_messages[candle_period]) == 0: |
172 | | - continue |
173 | | - |
174 | | - self.notify_discord_message(_messages[candle_period], message_template) |
175 | | - |
176 | | - def notify_discord_message(self, messages, message_template): |
| 187 | + |
177 | 188 | for message in messages: |
178 | 189 | formatted_message = message_template.render(message) |
179 | | - |
180 | | - self.discord_client.notify(formatted_message.strip()) |
| 190 | + |
| 191 | + self.discord_client.notify(formatted_message.strip()) |
181 | 192 |
|
182 | 193 |
|
183 | 194 | def notify_slack(self, new_analysis): |
@@ -228,104 +239,67 @@ def notify_gmail(self, new_analysis): |
228 | 239 | self.gmail_client.notify(message) |
229 | 240 |
|
230 | 241 |
|
231 | | - def notify_telegram(self, messages): |
| 242 | + def notify_telegram(self, messages, chart_file): |
232 | 243 | """Send notifications via the telegram notifier |
233 | 244 |
|
234 | 245 | Args: |
235 | | - messages (dict): The notification messages to send. |
| 246 | + messages (list): List of messages to send for a specific Exchanche/Market Pair/Candle Period |
| 247 | + chart_file (string): Path to chart file |
236 | 248 | """ |
237 | 249 |
|
238 | 250 | if not self.telegram_configured: |
239 | 251 | return |
240 | 252 |
|
241 | 253 | message_template = Template(self.notifier_config['telegram']['optional']['template']) |
242 | 254 |
|
243 | | - for exchange in messages: |
244 | | - for market_pair in messages[exchange]: |
245 | | - _messages = messages[exchange][market_pair] |
246 | | - |
247 | | - for candle_period in _messages: |
248 | | - if not isinstance(_messages[candle_period], list) or len (_messages[candle_period]) == 0: |
249 | | - continue |
250 | | - |
251 | | - if self.enable_charts == True: |
252 | | - self.notify_telegram_chart(exchange, market_pair, candle_period, _messages[candle_period], message_template) |
253 | | - else: |
254 | | - self.notify_telegram_message(_messages[candle_period], message_template) |
255 | | - |
256 | | - |
257 | | - def notify_telegram_chart(self, exchange, market_pair, candle_period, messages, message_template): |
258 | | - market_pair = market_pair.replace('/', '_').lower() |
259 | | - chart_file = '{}/{}_{}_{}.png'.format('./charts', exchange, market_pair, candle_period) |
| 255 | + formatted_messages = [] |
260 | 256 |
|
261 | | - if os.path.exists(chart_file): |
262 | | - try: |
263 | | - self.telegram_client.send_chart(open(chart_file, 'rb')) |
264 | | - self.notify_telegram_message(messages, message_template) |
265 | | - except (IOError, SyntaxError) : |
266 | | - self.notify_telegram_message(messages, message_template) |
| 257 | + for message in messages: |
| 258 | + formatted_messages.append(message_template.render(message)) |
| 259 | + |
| 260 | + if self.enable_charts == True: |
| 261 | + if chart_file != None and os.path.exists(chart_file): |
| 262 | + try: |
| 263 | + self.telegram_client.send_chart_messages(open(chart_file, 'rb'), formatted_messages) |
| 264 | + except (IOError, SyntaxError) : |
| 265 | + self.telegram_client.send_messages(formatted_messages) |
| 266 | + else: |
| 267 | + self.logger.info('Chart file %s doesnt exist, sending text message.', chart_file) |
| 268 | + self.telegram_client.send_messages(formatted_messages) |
267 | 269 | else: |
268 | | - self.logger.info('Chart file %s doesnt exist, sending text message.', chart_file) |
269 | | - self.notify_telegram_message(messages, message_template) |
| 270 | + self.telegram_client.send_messages(formatted_messages) |
270 | 271 |
|
271 | | - def notify_telegram_message(self, messages, message_template): |
272 | | - try: |
273 | | - for message in messages: |
274 | | - formatted_message = message_template.render(message) |
275 | | - self.telegram_client.notify(formatted_message.strip()) |
276 | | - except (TelegramTimedOut) as e: |
277 | | - self.logger.info('Error TimeOut!') |
278 | | - self.logger.info(e) |
279 | | - |
280 | | - def notify_webhook(self, messages): |
| 272 | + def notify_webhook(self, messages, chart_file): |
281 | 273 | """Send notifications via a new webhook notifier |
282 | 274 |
|
283 | 275 | Args: |
284 | | - messages (dict): The notification messages to send. |
| 276 | + messages (list): List of messages to send for a specific Exchanche/Market Pair/Candle Period |
| 277 | + chart_file (string): Path to chart file |
285 | 278 | """ |
286 | 279 |
|
287 | 280 | if not self.webhook_configured: |
288 | 281 | return |
289 | 282 |
|
290 | | - for exchange in messages: |
291 | | - for market_pair in messages[exchange]: |
292 | | - _messages = messages[exchange][market_pair] |
293 | | - |
294 | | - for candle_period in _messages: |
295 | | - if not isinstance(_messages[candle_period], list) or len (_messages[candle_period]) == 0: |
296 | | - continue |
297 | | - |
298 | | - self.webhook_client.notify(exchange, market_pair, candle_period, _messages[candle_period], self.enable_charts) |
| 283 | + self.webhook_client.notify(messages, chart_file) |
299 | 284 |
|
300 | 285 | def notify_stdout(self, messages): |
301 | 286 | """Send a notification via the stdout notifier |
302 | 287 |
|
303 | 288 | Args: |
304 | | - new_analysis (dict): The new_analysis to send. |
| 289 | + messages (list): List of messages to send for a specific Exchanche/Market Pair/Candle Period |
305 | 290 | """ |
306 | 291 |
|
307 | 292 | if not self.stdout_configured: |
308 | 293 | return |
309 | 294 |
|
310 | 295 | message_template = Template(self.notifier_config['stdout']['optional']['template']) |
311 | 296 |
|
312 | | - for exchange in messages: |
313 | | - for market_pair in messages[exchange]: |
314 | | - _messages = messages[exchange][market_pair] |
315 | | - |
316 | | - for candle_period in _messages: |
317 | | - if not isinstance(_messages[candle_period], list) or len (_messages[candle_period]) == 0: |
318 | | - continue |
319 | | - |
320 | | - self.notify_stdout_message(_messages[candle_period], message_template) |
321 | | - |
322 | | - def notify_stdout_message(self, messages, message_template): |
323 | 297 | for message in messages: |
324 | 298 | formatted_message = message_template.render(message) |
325 | 299 |
|
326 | | - #self.discord_client.notify(formatted_message.strip()) |
327 | 300 | self.stdout_client.notify(formatted_message.strip()) |
328 | 301 |
|
| 302 | + |
329 | 303 | def _validate_required_config(self, notifier, notifier_config): |
330 | 304 | """Validate the required configuration items are present for a notifier. |
331 | 305 |
|
@@ -687,7 +661,7 @@ def create_charts(self, messages): |
687 | 661 |
|
688 | 662 | def create_chart(self, exchange, market_pair, candle_period, candles_data): |
689 | 663 |
|
690 | | - self.logger.info("Beginning creation of charts: {} - {} - {}".format(exchange, market_pair, candle_period)) |
| 664 | + #self.logger.info("Beginning creation of charts: {} - {} - {}".format(exchange, market_pair, candle_period)) |
691 | 665 |
|
692 | 666 | now = datetime.now(timezone(self.timezone)) |
693 | 667 | creation_date = now.strftime("%Y-%m-%d %H:%M:%S") |
|
0 commit comments