-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
363 lines (303 loc) · 11.9 KB
/
Program.cs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using CoinNotify.Models;
using CoinNotify.Controllers;
using System.Diagnostics;
string key;
Console.WriteLine("Enter telegram bot key: ");
key = Console.ReadLine();
var botClient = new TelegramBotClient(key);
//getting coins
CoinParser parser = new CoinParser();
var coins = CoinParser.GetCoins();
UsersController usersController = new UsersController();
using CancellationTokenSource cts = new();
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
ReceiverOptions receiverOptions = new()
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types except ChatMember related updates
};
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);
var me = await botClient.GetMeAsync();
Console.WriteLine($"Start listening for @{me.Username}");
while (true)
{
UpdateInfo(cts.Token);
await Task.Delay(TimeSpan.FromSeconds(20));
}
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
//Updates data about coins and handles notifications
async Task UpdateInfo(CancellationToken cancellationToken)
{
Console.WriteLine("Check");
//Updating coins values
coins = CoinParser.GetCoins();
NotificationController notificationController = new NotificationController();
foreach( var i in
notificationController.CheckNotifications(usersController.GetUsers(), coins))
{
//Sending message for every notification price
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: i._id,
text: "Coin " + i._coins[0]._name + " has reached its notify price: " + Math.Abs(i._coins[0]._price),
cancellationToken: cancellationToken);
//Deleting notifications that has been triggered
usersController.DeleteNotification(
i._id,
(usersController.GetUsers().
First(x => x._id == i._id)._coins.IndexOf(
new Coin(
i._coins[0]._name,
usersController.GetUsers().First(x => x._id == i._id)
._coins.First(x => x._name == i._coins[0]._name)._price
)
)+2).ToString()
);
}
// Wait for 60 seconds before calling the method again
await Task.Delay(TimeSpan.FromSeconds(60));
}
//Handles messages from users
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
var chatId = message.Chat.Id;
//A bit of spaghetti code beacuse c# switch cases don`t handle Linq statements :(
//But here is all of the message handling logic
if (message.Text == "View Current Prices")
{
PriceViewHandle(message, chatId, cancellationToken);
}
else if (coins.Select(x => x._name).Contains(update.Message.Text))
{
PriceHandle(message, chatId, cancellationToken);
}
else if (message.Text == "Set new notification")
{
NotificationHandle(message, chatId, cancellationToken);
}
else if (coins.Select(x => x._name).Contains(update.Message.Text.ToString().Split(" - ")[0])
&& update.Message.Text.ToString().Split(" - ")[1] != null)
{
NotificationSelectHandle(message, chatId, cancellationToken);
}
else if ((coins.Select(x => x._name).Contains(update.Message.Text.ToString().Split(" < ")[0])
&& update.Message.Text.ToString().Split(" < ")[1] != null) ||
(coins.Select(x => x._name).Contains(update.Message.Text.ToString().Split(" > ")[0])
&& update.Message.Text.ToString().Split(" > ")[1] != null))
{
NotificationAddHandle(message, chatId, cancellationToken);
}
else if (message.Text == "Check my notifications")
{
CheckHandle(message, chatId, cancellationToken);
}
else if(message.Text == "Delete notifiaction")
{
DeleteNotificationHandle(message, chatId, cancellationToken);
}
else if (message.Text.ToString().Split(" ")[0].Contains("delete"))
{
DeleteHandle(message, chatId, cancellationToken);
}
else
{
DefaultHandle(chatId, cancellationToken);
}
}
Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}
//Returns buttons with all trackable coins name
async void PriceViewHandle(Message message, long chatId, CancellationToken cancellationToken)
{
List<KeyboardButton[]> buttonData = new List<KeyboardButton[]>();
foreach (var i in coins)
{
buttonData.Add(new KeyboardButton[] { i._name });
}
ReplyKeyboardMarkup inlineKeyboard = new ReplyKeyboardMarkup(buttonData.ToArray());
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Choose a coin",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
}
//Returns price of the coin
async void PriceHandle(Message message, long chatId, CancellationToken cancellationToken)
{
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: coins.FirstOrDefault(x => x._name == message.Text)._price.ToString(),
cancellationToken: cancellationToken);
//Sending default keyboard
DefaultHandle(chatId, cancellationToken);
}
//Returns buttons with all trackable coins and their prices
async void NotificationHandle(Message message, long chatId, CancellationToken cancellationToken)
{
List<KeyboardButton[]> buttonData = new List<KeyboardButton[]>();
foreach (var i in coins)
{
buttonData.Add(new KeyboardButton[] { i._name + " - " + i._price });
}
ReplyKeyboardMarkup inlineKeyboard = new ReplyKeyboardMarkup(buttonData.ToArray());
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Choose coin",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
}
//After some button is pressed in previous method - asks for sending message in certain form
async void NotificationSelectHandle(Message message, long chatId, CancellationToken cancellationToken)
{
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Enter a price for notification in format: \n'coin_name > or < notification_price'",
cancellationToken: cancellationToken);
}
//Reaceives message with notification setup
async void NotificationAddHandle(Message message, long chatId, CancellationToken cancellationToken)
{
string coin_name, coin_price;
//if tracking price below some value - saving as coin with negative price
if (coins.Select(x => x._name).Contains(message.Text.ToString().Split(" < ")[0])
&& message.Text.ToString().Split(" < ")[1] != null)
{
coin_name = message.Text.ToString().Split(" < ")[0];
coin_price = "-" + message.Text.ToString().Split(" < ")[1];
}
//if tracking price higher some value - as usual
else
{
coin_name = message.Text.ToString().Split(" > ")[0];
coin_price = message.Text.ToString().Split(" > ")[1];
}
//Passing data into the controller and responding according to the result
bool res = usersController.AddNotify(chatId.ToString(), coin_name, coin_price);
string reply;
if (res)
reply = "Notification added";
else
reply = "Error occured";
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: reply,
cancellationToken: cancellationToken);
}
//Returns all notifications that some user has set up
async void CheckHandle(Message message, long chatId, CancellationToken cancellationToken)
{
var notifications = usersController.GetNotifications(chatId.ToString());
if (notifications.Any())
{
foreach (var i in notifications)
{
string reply;
if (i._price < 0)
reply = i._name + " < " + Math.Abs(i._price);
else
reply = i._name + " > " + i._price;
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: reply,
cancellationToken: cancellationToken);
}
}
else
{
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Currently you dont have any notifications setted",
cancellationToken: cancellationToken);
//Sending default keyboard
DefaultHandle(chatId, cancellationToken);
}
}
//Returns all users notifications and asks to send delete message in certain format
async void DeleteNotificationHandle(Message message, long chatId, CancellationToken cancellationToken)
{
var notifications = usersController.GetNotifications(chatId.ToString());
if (notifications.Any())
{
int count = 1;
string reply;
foreach (var i in notifications)
{
if (i._price < 0)
reply = count + " " + i._name + " < " + Math.Abs(i._price);
else
reply = count + " " + i._name + " > " + i._price;
Message sentMessageMany = await botClient.SendTextMessageAsync(
chatId: chatId,
text: reply,
cancellationToken: cancellationToken);
count++;
}
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Enter notification to delete in format:\ndelete 'index_of_notification'",
cancellationToken: cancellationToken);
}
else
{
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Currently you dont have any notifications setted",
cancellationToken: cancellationToken);
//Sending default keyboard
DefaultHandle(chatId, cancellationToken);
}
}
//Sends notification to delete value to controller
async void DeleteHandle(Message message, long chatId, CancellationToken cancellationToken)
{
string notify_index = message.Text.ToString().Split(" ")[1];
usersController.DeleteNotification(chatId.ToString(), notify_index);
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Notification deleted",
cancellationToken: cancellationToken);
//Sending default keyboard
DefaultHandle(chatId, cancellationToken);
}
//Main menu that returns default keyboard
async void DefaultHandle(long chatId, CancellationToken cancellationToken)
{
List<KeyboardButton[]> buttonData = new List<KeyboardButton[]>();
buttonData.Add(new KeyboardButton[] { "Check my notifications" });
buttonData.Add(new KeyboardButton[] { "Set new notification" });
buttonData.Add(new KeyboardButton[] { "Delete notifiaction" });
buttonData.Add(new KeyboardButton[] { "View Current Prices" });
//"Check my notifications", "Set new notification", "Delete notifiaction", "View Current Prices" }
ReplyKeyboardMarkup inlineKeyboard = new ReplyKeyboardMarkup(buttonData.ToArray());
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "What do you want to do?",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
}