Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Telegram.Bot;
  7. using Telegram.Bot.Args;
  8. using Telegram.Bot.Types;
  9. using Telegram.Bot.Types.Enums;
  10. using Newtonsoft.Json;
  11. using System.Net;
  12. namespace EthermineBotTelegram
  13. {
  14. class Program
  15. {
  16. static ITelegramBotClient botClient;
  17. static void Main(string[] args)
  18. {
  19. botClient = new TelegramBotClient("1785154817:AAGhXD9yQVn9HPdWTcmGJUBeZ8nA50SzHbY");
  20. var me = botClient.GetMeAsync().Result;
  21. Console.WriteLine(
  22. $"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
  23. );
  24. botClient.OnMessage += BotOnMessage;
  25. botClient.StartReceiving();
  26. Console.WriteLine("Press any key to exit");
  27. Console.ReadKey();
  28. botClient.StopReceiving();
  29. }
  30. static async void BotOnMessage(object sender, MessageEventArgs e) {
  31. if (e.Message.Text != null)
  32. {
  33. Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");
  34. var message = e.Message;
  35. if (message == null || message.Type != MessageType.Text)
  36. return;
  37. switch (message.Text.Split(' ').First())
  38. {
  39. // add user into database
  40. case "/start":
  41. //await AddUser(e.Message.Chat);
  42. break;
  43. // connect wallet to user
  44. case "/setwallet":
  45. //await SetWallet(message);
  46. break;
  47. // get actual data from ethermine
  48. case "/actual":
  49. GetActualData(e);
  50. break;
  51. // send help
  52. case "/help":
  53. //await SendHelp();
  54. break;
  55. default:
  56. //await Usage(message);
  57. await botClient.SendTextMessageAsync(
  58. chatId: e.Message.Chat,
  59. text: "Incorrect message");
  60. break;
  61. }
  62. }
  63. }
  64. static async void GetActualData(MessageEventArgs e)
  65. {
  66. try
  67. {
  68. var url = "https://api.ethermine.org/miner/" + e.Message.Text.Substring(8) + "/currentStats";
  69. var currnentStats = _download_serialized_json_data<jsonCurrnentStats>(url);
  70. await botClient.SendTextMessageAsync(
  71. chatId: e.Message.Chat,
  72. text: "Updated: " + DateTimeOffset.FromUnixTimeSeconds(currnentStats.data.time).LocalDateTime.ToString("f") + "\n"
  73. + "Reported Hashrate: " + Math.Round(currnentStats.data.reportedHashrate / 1000000D, 3) +
  74. " MH/s\n"
  75. + "Current Hashrate: " + Math.Round(currnentStats.data.currentHashrate / 1000000D, 3) +
  76. " MH/s\n"
  77. + "Valid Shares: " + currnentStats.data.validShares + "\n"
  78. + "Stale Shares: " + currnentStats.data.staleShares + "\n"
  79. + "Unpaid Balance: " + Math.Round(currnentStats.data.unpaid / Math.Pow(10, 18), 5) + " ETH\n");
  80. }
  81. catch (Exception)
  82. {
  83. await botClient.SendTextMessageAsync(
  84. chatId: e.Message.Chat,
  85. text: "Something got wrong! Check entered wallet or try later.");
  86. }
  87. }
  88. private static T _download_serialized_json_data<T>(string url) where T : new() {
  89. using (var w = new WebClient()) {
  90. var jsonData = string.Empty;
  91. // attempt to download JSON data as a string
  92. try
  93. {
  94. jsonData = w.DownloadString(url);
  95. }
  96. catch (Exception) { }
  97. // if string with JSON data is not empty, deserialize it to class and return its instance
  98. return !string.IsNullOrEmpty(jsonData) ? JsonConvert.DeserializeObject<T>(jsonData) : new T();
  99. }
  100. }
  101. }
  102. }