Skip to content

Commit 840aab6

Browse files
authored
Merge pull request #368 from CryptoSignal/new_charts_creation_logic
Several changes to improve the way of sending notification messages
2 parents 3ded534 + d8bad32 commit 840aab6

3 files changed

Lines changed: 72 additions & 89 deletions

File tree

app/notification.py

Lines changed: 57 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from analyzers.indicators import candle_recognition
4242

4343
import sys
44+
from time import sleep
4445

4546
class Notifier(IndicatorUtils):
4647
"""Handles sending notifications via the configured notifiers
@@ -141,43 +142,53 @@ def notify_all(self, new_analysis):
141142
if not os.path.exists(charts_dir):
142143
os.mkdir(charts_dir)
143144

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)
145156

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)
147169
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)
152174
self.notify_stdout(messages)
153-
175+
154176
def notify_discord(self, messages):
155177
"""Send a notification via the discord notifier
156178
157179
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
159181
"""
160182

161183
if not self.discord_configured:
162184
return
163185

164186
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+
177188
for message in messages:
178189
formatted_message = message_template.render(message)
179-
180-
self.discord_client.notify(formatted_message.strip())
190+
191+
self.discord_client.notify(formatted_message.strip())
181192

182193

183194
def notify_slack(self, new_analysis):
@@ -228,104 +239,67 @@ def notify_gmail(self, new_analysis):
228239
self.gmail_client.notify(message)
229240

230241

231-
def notify_telegram(self, messages):
242+
def notify_telegram(self, messages, chart_file):
232243
"""Send notifications via the telegram notifier
233244
234245
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
236248
"""
237249

238250
if not self.telegram_configured:
239251
return
240252

241253
message_template = Template(self.notifier_config['telegram']['optional']['template'])
242254

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 = []
260256

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)
267269
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)
270271

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):
281273
"""Send notifications via a new webhook notifier
282274
283275
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
285278
"""
286279

287280
if not self.webhook_configured:
288281
return
289282

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)
299284

300285
def notify_stdout(self, messages):
301286
"""Send a notification via the stdout notifier
302287
303288
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
305290
"""
306291

307292
if not self.stdout_configured:
308293
return
309294

310295
message_template = Template(self.notifier_config['stdout']['optional']['template'])
311296

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):
323297
for message in messages:
324298
formatted_message = message_template.render(message)
325299

326-
#self.discord_client.notify(formatted_message.strip())
327300
self.stdout_client.notify(formatted_message.strip())
328301

302+
329303
def _validate_required_config(self, notifier, notifier_config):
330304
"""Validate the required configuration items are present for a notifier.
331305
@@ -687,7 +661,7 @@ def create_charts(self, messages):
687661

688662
def create_chart(self, exchange, market_pair, candle_period, candles_data):
689663

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))
691665

692666
now = datetime.now(timezone(self.timezone))
693667
creation_date = now.strftime("%Y-%m-%d %H:%M:%S")

app/notifiers/telegram_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,20 @@ def notify(self, message):
5353
stop=stop_after_attempt(6),
5454
wait=wait_fixed(5)
5555
)
56-
def send_chart(self, photo_url):
56+
def send_chart_messages(self, photo_url, messages = []):
5757
"""Send image chart
5858
5959
Args:
6060
photo_url (str): The photo url to send.
6161
"""
6262

63-
self.bot.send_photo(chat_id=self.chat_id, photo=photo_url, timeout=40)
63+
self.bot.send_photo(chat_id=self.chat_id, photo=photo_url, timeout=40)
64+
65+
if len(messages) > 0 :
66+
for message in messages:
67+
self.notify(message)
68+
69+
def send_messages(self, messages = []):
70+
if len(messages) > 0 :
71+
for message in messages:
72+
self.notify(message)

app/notifiers/webhook_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ def __init__(self, url, username, password):
1919
self.password = password
2020

2121

22-
def notify(self, exchange, market_pair, candle_period, messages, send_charts):
22+
def notify(self, messages, chart_file):
2323
"""Sends the notification messages.
2424
2525
Args:
2626
messages (dict): A dict with the messages to send.
2727
"""
2828

29-
market_pair = market_pair.replace('/', '_').lower()
30-
chart_file = '{}/{}_{}_{}.png'.format('./charts', exchange, market_pair, candle_period)
29+
#market_pair = market_pair.replace('/', '_').lower()
30+
#chart_file = '{}/{}_{}_{}.png'.format('./charts', exchange, market_pair, candle_period)
3131

3232
data = {'messages': json.dumps(messages)}
3333

34-
if send_charts == True and os.path.exists(chart_file):
34+
if chart_file != None and os.path.exists(chart_file):
3535
files = {'chart': open(chart_file, 'rb')}
3636

3737
if self.username and self.password:

0 commit comments

Comments
 (0)