Utilities.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using CardCollector.Resources;
  6. using Telegram.Bot;
  7. namespace CardCollector
  8. {
  9. public static class Utilities
  10. {
  11. public static string ToJson(object obj)
  12. {
  13. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  14. }
  15. public static string CreateMd5(string input)
  16. {
  17. // Use input string to calculate MD5 hash
  18. using var md5 = System.Security.Cryptography.MD5.Create();
  19. var inputBytes = Encoding.ASCII.GetBytes(input);
  20. var hashBytes = md5.ComputeHash(inputBytes);
  21. // Convert the byte array to hexadecimal string
  22. var sb = new StringBuilder();
  23. foreach (var t in hashBytes)
  24. sb.Append(t.ToString("X2"));
  25. return sb.ToString();
  26. }
  27. public static async Task DownloadFile(string fileId)
  28. {
  29. await Task.Run(async () =>
  30. {
  31. /* Получаем информацию о файле */
  32. var fileInfo = await Bot.Client.GetFileAsync(fileId);
  33. /* Собираем ссылку на файл */
  34. var fileUri = $"https://api.telegram.org/file/bot{AppSettings.TOKEN}/{fileInfo.FilePath}";
  35. /* Загружаем файл */
  36. var client = new WebClient();
  37. client.DownloadFile(new Uri(fileUri), "pack.zip");
  38. });
  39. }
  40. }
  41. }