-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommands_notifications.py
335 lines (306 loc) · 14.8 KB
/
commands_notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Copyright (c) 2021 Alberto Rota
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime
import telegram
from telegram.ext import (Updater,
CommandHandler,
MessageHandler,
CallbackQueryHandler,
ConversationHandler,
CallbackContext,
)
from telegram import ReplyKeyboardMarkup
import pandas as pd
import create
import os
MENU=0
ASK_ASSET = 1
ASK_ATYPICAL_ASSET = 1.5
ASK_TIMELINE = 2
ASK_TO_SET_WALLET = 3
ASK_WALLET_HOW = 4
ASK_NOTIFICATIONS_WHICH = 5
# DISPLAYS NOTIFICATIONS OPTIONS
def notifications(update, context):
keyboard_assets = [
['🔔A daily wallet report','🔔🔔Two daily wallet reports'],
['📈When my assets increase/decrease rapidely'],
['🏡Home']
]
update.message.reply_text(
"When do you wanna recieve a message? [TOGGLE] ",
reply_markup=ReplyKeyboardMarkup(
keyboard_assets,
one_time_keyboard=True,
resize_keyboard=True,
),
)
return ASK_NOTIFICATIONS_WHICH
def toggle_walletreport1(update,context):
user_id = update.message.from_user['id']
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-{user_id}: ")+
" Toggled Daily Wallet Notifications [once a day]")
s = f"data/notify_settings.csv"
s = pd.read_csv(s)
user_id = update.message.from_user['id']
# chat_id = update.message.chat_id
settings = f"data/notify_settings.csv"
s = pd.read_csv(settings).set_index('ID')
if user_id in s.index:
s.loc[user_id].DailyReportOnce = not s.loc[user_id].DailyReportOnce
if s.loc[user_id].DailyReportOnce == True and s.loc[user_id].DailyReportTwice == True:
s.loc[user_id].DailyReportTwice = False
s.to_csv(settings)
else:
with open(settings, 'a+') as f:
f.write(f"{user_id},True,False,False\n")
f.close()
s = pd.read_csv(settings).set_index('ID')
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"<b>Please check that the settings are correct:</b>\n{ s.loc[user_id]}",
parse_mode=telegram.ParseMode.HTML
)
return ASK_NOTIFICATIONS_WHICH
def toggle_walletreport2(update,context):
user_id = update.message.from_user['id']
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-{user_id}: ")+
" Toggled Daily Wallet Notifications [twice a day]")
s = f"data/notify_settings.csv"
s = pd.read_csv(s)
user_id = update.message.from_user['id']
chat_id = update.message.chat_id
settings = f"data/notify_settings.csv"
s = pd.read_csv(settings).set_index('ID')
if user_id in s.index:
s.loc[user_id].DailyReportTwice = not s.loc[user_id].DailyReportTwice
if s.loc[user_id].DailyReportOnce == True and s.loc[user_id].DailyReportTwice == True:
s.loc[user_id].DailyReportOnce = False
s.to_csv(settings)
else:
with open(settings, 'a+') as f:
f.write(f"{user_id},False,True,False\n")
f.close()
s = pd.read_csv(settings).set_index('ID')
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"<b>Please check that the settings are correct:</b>\n{ s.loc[user_id]}",
parse_mode=telegram.ParseMode.HTML
)
return ASK_NOTIFICATIONS_WHICH
def toggle_rapidincrease(update,context):
user_id = update.message.from_user['id']
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-{user_id}: ")+
" Toggled Rapid Changes Notifications")
s = f"data/notify_settings.csv"
s = pd.read_csv(s)
user_id = update.message.from_user['id']
chat_id = update.message.chat_id
settings = f"data/notify_settings.csv"
s = pd.read_csv(settings).set_index('ID')
if user_id in s.index:
s.loc[user_id].SuddenChanges = not s.loc[user_id].SuddenChanges
s.to_csv(settings)
else:
with open(settings, 'a+') as f:
f.write(f"{user_id},False,False,True\n")
f.close()
s = pd.read_csv(settings).set_index('ID')
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"<b>Please check that the settings are correct:</b>\n{ s.loc[user_id]}",
parse_mode=telegram.ParseMode.HTML
)
return ASK_NOTIFICATIONS_WHICH
def check_todo(context):
s = f"data/notify_settings.csv"
s = pd.read_csv(s)
sendtime1 = {'h':21,'m':0}
sendtime2 = {'h':12,'m':0}
emptybufferfoldertime = {'h':0, 'm':0}
volatilitycheck = {'1h':5,'4h':7.5,'12h':10,'24h':12}
# Saves current timestamp
time =datetime.datetime.now()
for _,user in s.iterrows():
# Sends wallet report if time is 21.00 (t)
if user['DailyReportOnce']==True:
if time.hour==sendtime1['h'] and time.minute ==sendtime1['m']:
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-BOT: ")+
f" Sending evening daily report to {user['ID']}")
context.bot.send_message(
chat_id=user['ID'],
text=create.profits(context.dispatcher.user_data['client'],user['ID']),
parse_mode=telegram.ParseMode.HTML,
)
wallet = f"data/wallets/{user['ID']}.csv"
df = pd.read_csv(wallet)
for a in df['asset']:
#############################################################################
figpath,cptn = create.chart(client, a,'1day')
# reportmsg = create.report(client, a)
#############################################################################
context.bot.send_photo(
chat_id=user['ID'],
photo=open(figpath,'rb'),
caption=cptn,
parse_mode=telegram.ParseMode.HTML,
)
# Sends wallet report if time is 12.00
if user['DailyReportTwice']==True:
if time.hour==sendtime1['h'] and time.minute ==sendtime1['m']:
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-BOT: ")+
f" Sending evening daily report to {user['ID']}")
context.bot.send_message(
chat_id=user['ID'],
text=create.profits(context.dispatcher.user_data['client'],user['ID']),
parse_mode=telegram.ParseMode.HTML,
)
wallet = f"data/wallets/{user['ID']}.csv"
df = pd.read_csv(wallet)
for a in df['asset']:
#############################################################################
figpath,cptn = create.chart(client, a,'12hour')
# reportmsg = create.report(client, a)
#############################################################################
context.bot.send_photo(
chat_id=user['ID'],
photo=open(figpath,'rb'),
caption=cptn,
parse_mode=telegram.ParseMode.HTML,
)
if time.hour==sendtime2['h'] and time.minute ==sendtime2['m']:
print(">> "+datetime.datetime.now().strftime(f"[%d/%m/%Y-%H:%M:%S]-BOT: ")+
f" Sending midday daily report to {user['ID']}")
context.bot.send_message(
chat_id=user['ID'],
text=create.profits(context.dispatcher.user_data['client'],user['ID']),
parse_mode=telegram.ParseMode.HTML,
)
wallet = f"data/wallets/{user['ID']}.csv"
df = pd.read_csv(wallet)
for a in df['asset']:
#############################################################################
figpath,cptn = create.chart(client, a,'12hour')
# reportmsg = create.report(client, a)
#############################################################################
context.bot.send_photo(
chat_id=user['ID'],
photo=open(figpath,'rb'),
caption=cptn,
parse_mode=telegram.ParseMode.HTML,
)
if user['SuddenChanges']==True:
client=context.dispatcher.user_data['client']
wallet = f"data/wallets/{user['ID']}.csv"
try:
df = pd.read_csv(wallet)
except:
context.bot.send_message(
chat_id=user['ID'],
text=
"""It seems like you didn't register a wallet yet 😕\n
Make sure you do it from the <u>💰Set Wallet</u> button on the main menu"""
)
return MENU
for a in df['asset']:
#############################################################################
klines = client.get_historical_klines(
symbol=a+"USDT",
start_str='1day',
interval='1h'
)
#############################################################################
kl = pd.DataFrame(
klines,
columns=['dateTime', 'open', 'high', 'low', 'close','volume',
'closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol', 'takerBuyQuoteVol', 'ignore']
)
for var in ["open","high","low","close"]:
kl[var] = pd.to_numeric(kl[var], downcast="float")
# 24h, 12h, 4h and 1h variations
h24var=(kl['close'].iloc[-1]-kl['open'].iloc[0])/kl['open'].iloc[0]*100
h12var=(kl['close'].iloc[-1]-kl['open'].iloc[-13])/kl['open'].iloc[-13]*100
h4var=(kl['close'].iloc[-1]-kl['open'].iloc[-5])/kl['open'].iloc[-5]*100
h1var=(kl['close'].iloc[-1]-kl['open'].iloc[-2])/kl['open'].iloc[-2]*100
# Checks 24h high variation > 10% thrice a day
if abs(h24var) > volatilitycheck['24h'] and time.hour%4==0 and time.minute == 0:
print(">> "+datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]-BOT")+
f" Signalling a high variation for {a} in the last 24h")
if h24var>0:
h24var = "{:.2f}".format(h24var)
notification_text = f"🟩{a} is <b>UP {h24var}%</b> since the last 24 hours!"
else:
h24var = "{:.2f}".format(h24var)
notification_text = f"🟥{a} is <i>DOWN {h24var}%</i> since the last 24 hours!"
context.bot.send_message(
chat_id=user['ID'],
text=notification_text,
parse_mode=telegram.ParseMode.HTML,
)
break
# Checks 12h high variation > 7.5% 4 times a day
if abs(h12var) > volatilitycheck['12h'] and time.hour%3==0 and time.minute == 0:
print(">> "+datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]-BOT")+
f" Signalling a high variation for {a} in the last 12h")
if h12var>0:
h12var = "{:.2f}".format(h12var)
notification_text = f"🟩{a} is <b>UP {h12var}%</b> since the last 12 hours!"
else:
h12var = "{:.2f}".format(h12var)
notification_text = f"🟥{a} is <i>DOWN {h12var}%</i> since the last 12 hours!"
context.bot.send_message(
chat_id=user['ID'],
text=notification_text,
parse_mode=telegram.ParseMode.HTML,
)
break
# Checks 4h high variation > 5% 12 times a day
if abs(h4var) > volatilitycheck['4h'] and time.hour&2==0 and time.minute == 0:
print(">> "+datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]-BOT")+
f" Signalling a high variation for {a} in the last 4h")
if h4var>0:
h4var = "{:.2f}".format(h4var)
notification_text = f"🟩{a} is <b>UP {h4var}%</b> since the last 4 hours!"
else:
h4var = "{:.2f}".format(h4var)
notification_text = f"🟥{a} is <i>DOWN {h4var}%</i> since the last 4 hours!"
context.bot.send_message(
chat_id=user['ID'],
text=notification_text,
parse_mode=telegram.ParseMode.HTML,
)
break
# Checks 1h high variation > 3% 4 times an hour
if (abs(h1var) > volatilitycheck['1h']) and time.minute%15==0:
print(">> "+datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]-BOT")+
f" Signalling a high variation for {a} in the last 1h")
if h1var>0:
h1var = "{:.2f}".format(h1var)
notification_text = f"🟩{a} is <b>UP {h1var}%</b> since the last 1 hour!"
else:
h1var = "{:.2f}".format(h1var)
notification_text = f"🟥{a} is <i>DOWN {h1var}%</i> since the last 1 hour!"
context.bot.send_message(
chat_id=user['ID'],
text=notification_text,
parse_mode=telegram.ParseMode.HTML,
)
break
# Deletes all saved graphs in the buffer folder at midnight every day
if time.hour==emptybufferfoldertime['h'] and time.minute==emptybufferfoldertime['m']:
for f in os.listdir('savedfigs'):
os.remove(os.path.join('savedfigs', f))
print(">> "+datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]-BOT: ")+
f" Deleted all graphs in the buffer folder")