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
| using System;
using System.Data.SqlClient;
using System.Timers;
using System.Xml;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.ReplyMarkups;
namespace EX
{
class Program
{
static public int a1 = 3600000;
static SqlConnection connection = new SqlConnection();
static TelegramBotClient client;
static Timer timer = new Timer(a1);
static void Main(string[] args)
{
connection.ConnectionString = @">>>>>>>>>>>>>>>>>>>>>>>";
timer.Elapsed += TimerGetMsg;
timer.Start();
client = new TelegramBotClient(">>>>>>>>>>>>>>>>>>>>>>>");
client.OnMessage += GetMsg;
client.StartReceiving();
Console.Read();
}
private static void TimerGetMsg(object sender, ElapsedEventArgs e)
{
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
string URL = "https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=5";
XmlTextReader xmlread = new XmlTextReader(URL);
xmlread.Read();
string message = "";
while (xmlread.Read())
{
if (xmlread.AttributeCount > 3)
{
message +=
xmlread.GetAttribute("ccy") + " - " +
xmlread.GetAttribute("base_ccy") + ": buy: " +
xmlread.GetAttribute("buy") + ", sale: " +
xmlread.GetAttribute("sale") + Environment.NewLine;
}
}
Console.WriteLine("Here-1");
using (SqlCommand command = new SqlCommand($"SELECT * FROM >>>>>>>>>>>>>>>>>>>>>>>", connection))
{
try
{
SqlDataReader reade = command.ExecuteReader();
while (reade.Read())
{
client.SendTextMessageAsync(reade.GetInt32(1), message);
Console.WriteLine("Sended");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.WriteLine("Here-2");
connection.Close();
}
public static bool isClient(long a)
{
connection.Open();
using (SqlCommand command = new SqlCommand($"SELECT * FROM >>>>>>>>>>>>>>>>>>>>>>>", connection))
{
try
{
SqlDataReader sqldatareader = command.ExecuteReader();
while (sqldatareader.Read())
{
sqldatareader.GetInt32(1);
if (a == sqldatareader.GetInt32(1))
{
connection.Close();
return true;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
connection.Close();
return false;
}
private static void GetMsg(object sender, MessageEventArgs e)
{
connection = new SqlConnection();
connection.ConnectionString = @">>>>>>>>>>>>>>>>>>>>>>>";
if (e.Message.Type != Telegram.Bot.Types.Enums.MessageType.Text)
{
return;
}
if (!isClient(e.Message.Chat.Id))
{
connection.Open();
using (SqlCommand command = new SqlCommand($"INSERT INTO DZ_03 ([Chat_ID]) VALUES ({e.Message.Chat.Id})", connection))
{
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
connection.Close();
}
Console.WriteLine($"Msg from {e.Message.Chat.Id}");
Console.WriteLine($"Msg from {e.Message.Text.ToString()}");
try
{
int test1 = Convert.ToInt32(e.Message.Text);
timer.Stop();
timer = new Timer(test1);
timer.Elapsed += TimerGetMsg;
timer.Start();
}
catch
{
switch (e.Message.Text.ToLower())
{
//case "hi":
// {
// client.SendTextMessageAsync(e.Message.Chat.Id, "Welocome to my chat");
// }
// break;
case "time":
{
client.SendTextMessageAsync(e.Message.Chat.Id, DateTime.Now.ToShortDateString());
}
break;
case "er":
{
string URL = "https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=5";
XmlTextReader xmlread = new XmlTextReader(URL);
xmlread.Read();
string message = "";
while (xmlread.Read())
{
if (xmlread.AttributeCount > 3)
{
message +=
xmlread.GetAttribute("ccy") + " - " +
xmlread.GetAttribute("base_ccy") + ": buy: " +
xmlread.GetAttribute("buy") + ", sale: " +
xmlread.GetAttribute("sale") + Environment.NewLine;
}
}
client.SendTextMessageAsync(e.Message.Chat.Id, message);
}
break;
case "er_history":
{
string URL = "https://api.privatbank.ua/p24api/exchange_rates?date=01.12.2014";
XmlTextReader xmlread = new XmlTextReader(URL);
xmlread.Read();
string message = "";
while (xmlread.Read())
{
if (xmlread.AttributeCount > 2)
{
message +=
"Основная валюта: " +
xmlread.GetAttribute("baseCurrency") + ", " +
"валюта: " +
xmlread.GetAttribute("currency") + ", " +
"цена продажи НБ: " +
xmlread.GetAttribute("saleRateNB") + ", " +
"цена продажи: " +
xmlread.GetAttribute("saleRate") + ", " +
"курс покупки: " +
xmlread.GetAttribute("purchaseRate") + ", " +
"курс покупки НБ: " +
xmlread.GetAttribute("purchaseRateNB") + ". " + Environment.NewLine + Environment.NewLine;
}
}
client.SendTextMessageAsync(e.Message.Chat.Id, message);
}
break;
case "id":
{
client.SendTextMessageAsync(e.Message.Chat.Id, e.Message.Chat.Id.ToString());
}
break;
case "btn":
var markup = new ReplyKeyboardMarkup
(new[]
{
//new KeyboardButton("hi"),
new KeyboardButton("time"),
new KeyboardButton("er"),
new KeyboardButton("er_history"),
new KeyboardButton("id")
}
);
markup.OneTimeKeyboard = true;
client.SendTextMessageAsync(e.Message.Chat.Id, "Кнопки: ", replyMarkup: markup);
break;
default:
break;
}
}
}
}
} |