-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
541 lines (470 loc) · 22 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Text;
using TeleNet.Models.Utils;
using TeleNet.Models.TL;
using TeleNet.Models;
namespace TeleNet.OpenClient
{
class Program
{
#region properties
private static UpdaterConfig _updaterConfig;
private static TelegramClient _client;
private static TelegramClient _clientFile;
private static Models.TL.Messages.TLAbsDialogs _dialogs;
private static readonly int DownloadDocumentPartSize = 128 * 1024; // 128kb for document
private static readonly int DownloadPhotoPartSize = 64 * 1024; // 64kb for photo
private static int _floodErrorCount = 0;
#endregion
static async Task Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8;
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json", optional: true);
var configuration = builder.Build();
_updaterConfig = configuration.Get<UpdaterConfig>();
ConsoleLogger.Log("Enter phone number:");
EnterPhoneNumber:
var adminPhoneNumber = Console.ReadLine();
if (!string.IsNullOrEmpty(adminPhoneNumber))
{
if (adminPhoneNumber.Length < 8)
{
ConsoleLogger.Log("Invalid phone number: Please enter a valid phone number like 989123338899 ");
goto EnterPhoneNumber;
}
adminPhoneNumber = adminPhoneNumber.Trim();
}
ConsoleLogger.Log("Starting login to telegram with " + adminPhoneNumber);
try
{
var sessionNamePrefix = _updaterConfig.SessionName + "_" + adminPhoneNumber.Substring(adminPhoneNumber.Length - 5, 4);
_client = await GetTlgClient(_updaterConfig.ApiHash, _updaterConfig.ApiId, sessionNamePrefix, adminPhoneNumber);
//define multi client to support reconnect to new dc
_clientFile = await GetTlgClient(_updaterConfig.ApiHash, _updaterConfig.ApiId, sessionNamePrefix + "_file", adminPhoneNumber);
}
catch (Exception e)
{
ConsoleLogger.Log("Global GetTlgClient " + e.GetInnerException().Message.SubstringX(300), ConsoleLogger.LogType.Error);
goto End;
}
Start:
var command = string.Empty;
try
{
Console.Write("Enter Command:");
command = Console.ReadLine();
if (string.IsNullOrEmpty(command))
{
throw new Exception("Invalid Command");
}
if (command.ToLower() == "getsticker")
{
Console.Write("StickerSet Id:");
var stickerStr = Console.ReadLine();
ulong stickersetId;
TeleNet.Models.TL.Messages.TLStickerSet stickerset;
if (ulong.TryParse(stickerStr, out stickersetId))
{
Console.Write("StickerSet Accesshash:");
var accesshash = ulong.Parse(Console.ReadLine());
ConsoleLogger.Log("Getting sticker...");
stickerset = await DownloadStickerSetAsync(accesshash, stickersetId);
}
else
{
//get dice sticker by emojiicon
if (stickerStr.StartsWith("c_"))
stickerStr = ((char)int.Parse(stickerStr.Replace("c_", ""))).ToString();
stickerset = await DownloadStickerSetAsync(0, 0, stickerStr.Length < 4 ? null : stickerStr,
stickerStr.Length < 4 ? stickerStr : null);
}
ConsoleLogger.Log("Save sticker set " + stickerset.Set.Id + " docs:" + stickerset.Documents.Count);
}
else if (command.ToLower() == "getchannel")
{
Console.Write("Channel username:");
var messageCount = await GetChannelFiles(Console.ReadLine(), 10);
ConsoleLogger.Log("Channel message count:" + messageCount, ConsoleLogger.LogType.SupperInfo);
}
else if (command.ToLower() == "getthemes")
{
var themes = GetThemes("android", 0).Result;
}
else
{
throw new Exception("Invalid Command");
}
}
catch (Exception e)
{
try
{
ConsoleLogger.Log(e.GetInnerException().Message.SubstringX(300));
}
catch (Exception)
{
//ignore
}
}
ConsoleLogger.Log("Finish.\t Start again:start \t Exit:any keys");
var cmd = Console.ReadLine();
if (cmd != null && cmd.ToLower() == "start")
goto Start;
End:
ConsoleLogger.Log("Finish!");
Environment.Exit(1);
}
private static async Task<TelegramClient> GetTlgClient(string apiHash, int apiId, string sessionName, string mainPhoneNumber,
Models.Network.TcpClientConnectionHandler clientConnectionHandler = null)
{
var phoneCodeHash = string.Empty;
var client = new TelegramClient(apiId, apiHash, null, sessionName, clientConnectionHandler);
authenticated:
ConsoleLogger.Log("Connecting...");
var task = Task.Factory.StartNew(() => { client.ConnectAsync().Wait(); });
ConsoleLogger.Log("MakeAuthentication");
if (!task.Wait(TimeSpan.FromMinutes(1)))
{
await MakeAuthentication(client, mainPhoneNumber);
}
ConsoleLogger.Log("Connected");
if (!client.IsUserAuthorized())
{
ConsoleLogger.Log("User not autorized");
if (string.IsNullOrEmpty(phoneCodeHash))
{
try
{
phoneCodeHash = await client.SendCodeRequestAsync(mainPhoneNumber);
goto authenticated;
}
catch (Exception ex)
{
if (ex.GetInnerException().Message.Contains("Password"))
{
client.MakeAuthWithPasswordAsync(new Models.TL.Account.TLPassword(), "").Wait();
}
else
throw;
}
}
ConsoleLogger.Log($"Enter login code{(sessionName.EndsWith("file") ? "(file)" : "")}:");
var authCode = Console.ReadLine();
var user = await client.MakeAuthAsync(mainPhoneNumber, phoneCodeHash, authCode);
ConsoleLogger.Log("Login successfull.");
}
return client;
}
private static async Task MakeAuthentication(TelegramClient client, string mainPhoneNumber)
{
var hash = await client.SendCodeRequestAsync(mainPhoneNumber);
ConsoleLogger.Log("waiting for code");
var code = Console.ReadLine();
var user = await client.MakeAuthAsync(mainPhoneNumber, hash, code);
if (!client.IsUserAuthorized())
{
hash = await client.SendCodeRequestAsync(mainPhoneNumber);
ConsoleLogger.Log("please try again.add code");
code = Console.ReadLine();
user = await client.MakeAuthAsync(mainPhoneNumber, hash, code);
}
}
private static async Task<int> GetChannelFiles(string username, int limit)
{
if (_floodErrorCount > 2)
{
ConsoleLogger.Log("Max Flood Count " + _floodErrorCount, ConsoleLogger.LogType.SupperInfo);
return 0;
}
ConsoleLogger.Log($"Get channel's files.... @{username} limit:{limit}");
GETDIALOGS:
try
{
_dialogs = await _client.GetUserDialogs85Async();
}
catch (Exception exception)
{
if (exception.GetInnerException()
.Message.Contains("Flood prevention"))
{
ConsoleLogger.Log("GetChannelFiles Dialogs Floood " + exception.GetInnerException().Message.SubstringX(300), ConsoleLogger.LogType.Error);
await Task.Delay(TimeSpan.FromSeconds(45));
_floodErrorCount++;
return -1;
}
throw;
}
var chats = _dialogs is Models.TL.Messages.TLDialogs tlDialogs ? tlDialogs.Chats : ((Models.TL.Messages.TLDialogsSlice)_dialogs).Chats;
//ConsoleLogger.Log("Chats count:" + chats.Count);
if (username[0] == '@')
username = username.Remove(0, 1);
//----Get Validation Code---
//var userTemp = (Models.TL.TLUser)(_dialogs as TLDialogs).Users.SingleOrDefault(c => c is Models.TL.TLUser && ((Models.TL.TLUser)c).Id == 777000);
//var hssss = _client.GetHistoryAsync(new Models.TL.TLInputPeerUser()
//{
// UserId = (int)userTemp.Id,
// AccessHash = userTemp.AccessHash.Value
//}, 0, 0, 0, 10).Result;
var channel = (Models.TL133.TLChannel)chats.FirstOrDefault(i => i is Models.TL133.TLChannel ch && string.Equals(i.Username, username,
StringComparison.CurrentCultureIgnoreCase));
if (channel == null)
{
var found = await _client.SearchUserAsync("@" + username);
if (found.Chats.Any())
{
var channely = found.Chats.FirstOrDefault(c => c is Models.TL133.TLChannel chl &&
chl.Username.Equals(username, StringComparison.CurrentCultureIgnoreCase));
if (channely != null)
{
var channelId = (Models.TL133.TLChannel)channely;
var updates = await _client.SendRequestAsync<TLAbsUpdates>(new Models.TL.Channels.TLRequestJoinChannel()
{
Channel = new TLInputChannel() { AccessHash = channelId.AccessHash.Value, ChannelId = (int)channelId.Id }
});
if (updates is Models.TL.TLUpdates || updates is Models.TL.TLUpdates)
{
ConsoleLogger.Log("Join to channel " + channelId.Username, ConsoleLogger.LogType.SupperInfo);
goto GETDIALOGS;
}
}
else
{
ConsoleLogger.Log("OpenClient ChannelNotFound " + username, ConsoleLogger.LogType.Warning);
}
}
throw new Exception("Channel " + username + " not found. You must join to channel first");
}
ConsoleLogger.Log("Get channel posts. @" + channel.Username + " ChannelId:" + channel.Id, ConsoleLogger.LogType.Info);
var histories = await _client.GetHistoryAsync(new TLInputPeerChannel()
{
ChannelId = (int)channel.Id,
AccessHash = channel.AccessHash.Value
}, 0, 0,/* LastItemId*/ 0, limit);
Models.TL140.TLMessage message = null;
var messages = ((Models.TL123.Messages.TLChannelMessages)histories).Messages
.OfType<Models.TL140.TLMessage>()
.ToList();
messages = messages.OrderBy(m => m.Id).ToList();
var errorCount = 0;
for (var index = 0; index < messages.Count; index++)
{
message = messages[index];
Console.WriteLine("Get message " + message.Id + " " + message.Message.SubstringX(50));
try
{
if (message.Media != null)
{
if (message.Media is TLMessageMediaPhoto photo)
{
Console.Write("Download photo: " + photo.Photo.Id);
}
else if (message.Media is TLMessageMediaDocument document1 &&
document1.Document is Models.TL123.TLDocument stickerDoc && (stickerDoc.MimeType == "image/webp" || stickerDoc.MimeType == "application/x-tgsticker") &&
((TLDocumentAttributeSticker)stickerDoc.Attributes.First(a => a is TLDocumentAttributeSticker)).Stickerset is TLInputStickerSetID stickerPack)
{
ConsoleLogger.Log(string.Format("Sticker ID:{0} AH:{1} ",
stickerPack.Id, stickerPack.AccessHash), ConsoleLogger.LogType.Info, false, false);
}
else if (message.Media is TLMessageMediaDocument document)
{
ConsoleLogger.Log(string.Format("Document ID:{0} MimeType:{1} ",
document.Document.Id, document.Document.MimeType), ConsoleLogger.LogType.Info, false, false);
}
else if (message.Media is TLMessageMediaWebPage mediaWebPage && mediaWebPage.Webpage is TLAbsWebPage webpage)
{
ConsoleLogger.Log(string.Format("WebPage ID:{0} DisplayUrl:{1} ", webpage.Id, webpage.DisplayUrl),
ConsoleLogger.LogType.Info, false, false);
}
else if (message.Media is Models.TL123.TLMessageMediaDice mediaDic)
{
}
else
{
ConsoleLogger.Log(" This media was not support ", ConsoleLogger.LogType.Default, false, false);
continue;
}
}
if (message.Entities != null && message.Entities.Any())
{
}
}
catch (Exception exception)
{
var addionional = "";
ConsoleLogger.Log("Error count:" + errorCount + " " + addionional + "\n" +
exception.GetInnerException().Message.SubstringX(350) + " ", ConsoleLogger.LogType.Error);
if (exception.GetInnerException().Message.Contains("Flood prevention. Telegram now requires your program"))
{
await Task.Delay(TimeSpan.FromMinutes(1));
_floodErrorCount++;
index--;
break;
}
errorCount++;
}
}
ConsoleLogger.Log("Get channel messages was done! Last:" + message.Id + " Messages Count:" + messages.Count + " ErrorCount:" + errorCount + " ------");
return messages.Count;
}
private static async Task<Models.TL.Messages.TLStickerSet> GetStickerSet(ulong accessHash, ulong stickerSetId)
{
var stickerset = await _client.GetStickerSetAsync(new TLInputStickerSetID()
{
AccessHash = (long)accessHash,
Id = (long)stickerSetId
});
//ConsoleLogger.Log(string.Format("Sticker info {0} {1} {2} {3}", stickerset.Set.Id, stickerset.Set.Count,
// stickerset.Set.Title, stickerset.Set.ShortName));
return stickerset;
}
private static async Task<Models.TL.Messages.TLStickerSet> GetStickerSet(Models.TL.TLAbsInputStickerSet stickerSet)
{
var stickerset = await _client.GetStickerSetAsync(stickerSet);
//ConsoleLogger.Log(string.Format("Sticker info {0} {1} {2} {3}", stickerset.Set.Id, stickerset.Set.Count,
// stickerset.Set.Title, stickerset.Set.ShortName));
return stickerset;
}
private static async Task<TeleNet.Models.TL.Messages.TLStickerSet> DownloadStickerSetAsync(ulong accessHash, ulong stickerSetId,
string stickerSetName = null, string emojiIcon = null)
{
var sticker = !string.IsNullOrEmpty(stickerSetName) ? await GetStickerSet(new Models.TL.TLInputStickerSetShortName
{
ShortName = stickerSetName
}) : !string.IsNullOrEmpty(emojiIcon) ?
await GetStickerSet(new Models.TL123.TLInputStickerSetDice()
{
Emoticon = emojiIcon
}) :
await GetStickerSet(new TLInputStickerSetID()
{
AccessHash = (long)accessHash,
Id = (long)stickerSetId
});
foreach (var pack in sticker.Packs)
{
ConsoleLogger.Debug("Pack:" + pack.Emoticon);
}
ConsoleLogger.Log("sticker.Documents " + sticker.Documents.Count);
foreach (var document in sticker.Documents)
{
ConsoleLogger.Debug("Sticker Doc:" + document.Id);
}
ConsoleLogger.Log("Download sticker successfully");
return sticker;
}
private async Task<byte[]> GetDocumentFile(long fileid, long accesshash, int size)
{
var filePart = 512 * 1024;
var offset = 0;
using (var ms = new MemoryStream())
{
while (offset < size)
{
try
{
var resFile = await _clientFile.GetFile(
new TLInputDocumentFileLocation()
{
Id = fileid,
AccessHash = accesshash
}, filePart, offset);
ms.Write(resFile.Bytes, 0, resFile.Bytes.Length);
offset += filePart;
}
catch (Exception ex)
{
throw ex;
}
}
return ms.ToArray();
}
}
private static async Task<byte[]> GetDocumentFile(long fileid, long accesshash, byte[] fileReference, string thumbSize = null)
{
var filePartSize = DownloadDocumentPartSize;
var offset = 0;
var bytes = new List<byte>();
while (true)
{
var file = await _clientFile.GetFile(!string.IsNullOrEmpty(thumbSize) ?
new Models.TL99.TLInputDocumentFileLocation()
{
Id = fileid,
AccessHash = accesshash,
FileReference = fileReference,
ThumbSize = thumbSize
} : (TLAbsInputFileLocation)new Models.TL96.TLInputDocumentFileLocation()
{
Id = fileid,
AccessHash = accesshash,
FileReference = fileReference,
}, filePartSize, offset);
if (file == null)
{
return null;
}
bytes.AddRange(file.Bytes);
if (file.Bytes.Length < filePartSize)
{
return bytes.ToArray();
}
offset += file.Bytes.Length;
}
}
private static async Task<byte[]> GetFile(long secret, int localid, long volumeid, byte[] fileReference, string thumbSize = null)
{
var filePartSize = DownloadPhotoPartSize;
var location = string.IsNullOrEmpty(thumbSize) ?
fileReference != null ?
(TLAbsInputFileLocation)new Models.TL96.TLInputFileLocation()
{
Secret = secret,
LocalId = localid,
VolumeId = volumeid,
FileReference = fileReference
} : (TLAbsInputFileLocation)new Models.TL.TLInputFileLocation
{
Secret = secret,
LocalId = localid,
VolumeId = volumeid,
} :
new Models.TL99.TLInputPhotoFileLocation()
{
Id = volumeid,
AccessHash = secret,
FileReference = fileReference,
ThumbSize = thumbSize
};
var offset = 0;
var bytes = new List<byte>();
while (true)
{
var file = await _clientFile.GetFile(location, filePartSize, offset);
if (file == null)
{
return null;
}
bytes.AddRange(file.Bytes);
if (file.Bytes.Length < filePartSize)
{
return bytes.ToArray();
}
offset += file.Bytes.Length;
}
}
private static async Task<Models.TL.Account.TLAbsThemes> GetThemes(string format, int hash)
{
return await _client.SendRequestAsync<Models.TL.Account.TLAbsThemes>(
new Models.TL133.Account.TLRequestGetThemes() { Format = format, Hash = hash });
}
}
}