Guest User

Untitled

a guest
Mar 24th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. if(message.Text.StartsWith("/start"))
  2. {
  3.  
  4. try
  5. {
  6. var factory = new ConnectionFactory() { HostName = "api.chatwars.me", Port = 5673, UserName = "****", Password = "******" };
  7. factory.Ssl.Version = SslProtocols.Tls12;
  8. factory.Ssl.Enabled = true;
  9. factory.Ssl.AcceptablePolicyErrors |= System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch;
  10.  
  11. using (var connection = factory.CreateConnection())
  12. using (var channel = connection.CreateModel())
  13.  
  14. {
  15.  
  16. byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("{\"action\": \"createAuthCode\", \"payload\": {\"userId\": "+ message.From.Id + "}}");
  17. IBasicProperties props = channel.CreateBasicProperties();
  18. props.ContentType = "application/json";
  19. props.DeliveryMode = 2;
  20.  
  21.  
  22.  
  23. channel.BasicPublish("darkmane_battle_informer_ex",
  24. "darkmane_battle_informer_o", props,
  25. messageBodyBytes);
  26.  
  27.  
  28.  
  29. Console.WriteLine("sent message");
  30. await Bot.SendTextMessageAsync(message.Chat.Id, "Please check chat wars for an auth code, then enter it here. @chtwrsbot");
  31. await Bot.SendTextMessageAsync(message.Chat.Id, "Example: /auth 123456");
  32.  
  33.  
  34. }
  35.  
  36. using (IConnection connection = factory.CreateConnection())
  37. {
  38. using (IModel model = connection.CreateModel())
  39. {
  40. var subscription = new Subscription(model, "darkmane_battle_informer_i", false);
  41.  
  42. get = 1;
  43. while (get == 1)
  44. {
  45. BasicDeliverEventArgs basicDeliveryEventArgs =
  46. subscription.Next();
  47. string messageContent =
  48. Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
  49. var json = JObject.Parse(messageContent); //Reads the json from the html (?)
  50. Console.WriteLine(json);
  51.  
  52. await Bot.SendTextMessageAsync(-1001279937491, "Battle Informer: | CW API | " + messageContent);
  53.  
  54.  
  55.  
  56. subscription.Ack(basicDeliveryEventArgs);
  57. get = 0;
  58. }
  59. }
  60.  
  61.  
  62. }
  63. }
  64.  
  65. catch (Exception ex)
  66. {
  67. Console.WriteLine(ex);
  68. }
  69. }
  70.  
  71. if (message.Text.StartsWith("/auth"))
  72. {
  73. int authcode = 0;
  74.  
  75. try
  76. {
  77. authcode = Convert.ToInt32(Regex.Match(message.Text, @"\d+").Value);
  78. }
  79.  
  80. catch
  81. {
  82. try
  83. {
  84. await Bot.SendTextMessageAsync(message.Chat.Id, "Your code appears invalid..");
  85. return;
  86. }
  87.  
  88. catch
  89. {
  90. return;
  91. }
  92. }
  93.  
  94.  
  95.  
  96. try
  97. {
  98. var factory = new ConnectionFactory() { HostName = "api.chatwars.me", Port = 5673, UserName = "*******", Password = "******" };
  99. factory.Ssl.Version = SslProtocols.Tls12;
  100. factory.Ssl.Enabled = true;
  101. factory.Ssl.AcceptablePolicyErrors |= System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch;
  102.  
  103.  
  104. using (var connection = factory.CreateConnection())
  105. using (var channel = connection.CreateModel())
  106.  
  107. {
  108.  
  109. byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("{\"action\": \"grantToken\", \"payload\": {\"userId\":" + message.From.Id + ", \"authCode\":\"" + authcode + "\"}}");
  110. IBasicProperties props = channel.CreateBasicProperties();
  111. props.ContentType = "application/json";
  112. props.DeliveryMode = 2;
  113.  
  114.  
  115.  
  116. channel.BasicPublish("darkmane_battle_informer_ex",
  117. "darkmane_battle_informer_o", props,
  118. messageBodyBytes);
  119.  
  120.  
  121.  
  122.  
  123. }
  124.  
  125. using (IConnection connection = factory.CreateConnection())
  126. {
  127. using (IModel model = connection.CreateModel())
  128. {
  129. var subscription = new Subscription(model, "darkmane_battle_informer_i", false);
  130. get = 1;
  131. while (get == 1)
  132. {
  133. BasicDeliverEventArgs basicDeliveryEventArgs =
  134. subscription.Next();
  135. string messageContent =
  136. Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
  137. var json = JObject.Parse(messageContent); //Reads the json from the html (?)
  138.  
  139. Console.WriteLine(messageContent);
  140.  
  141.  
  142. try
  143. {
  144. token = json["payload"]["token"].ToString();
  145.  
  146. Console.WriteLine("Token = " + token);
  147.  
  148. await Bot.SendTextMessageAsync(-1001279937491, "Battle Informer: | CW API | " + messageContent);
  149. }
  150.  
  151. catch
  152. {
  153. await Bot.SendTextMessageAsync(-1001279937491, "Battle Informer: | WARN | Failed getting token -- " + messageContent);
  154.  
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. subscription.Ack(basicDeliveryEventArgs);
  162. get = 0;
  163. }
  164. }
  165. }
  166.  
  167. using (var connection = factory.CreateConnection())
  168. using (var channel = connection.CreateModel())
  169.  
  170. {
  171.  
  172. byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("{\"token\": \"" + token + "\",\"action\": \"requestProfile\"}");
  173. IBasicProperties props = channel.CreateBasicProperties();
  174. props.ContentType = "application/json";
  175. props.DeliveryMode = 2;
  176.  
  177.  
  178.  
  179. channel.BasicPublish("darkmane_battle_informer_ex",
  180. "darkmane_battle_informer_o", props,
  181. messageBodyBytes);
  182.  
  183.  
  184.  
  185.  
  186. }
  187.  
  188. using (IConnection connection = factory.CreateConnection())
  189. {
  190. using (IModel model = connection.CreateModel())
  191. {
  192. var subscription = new Subscription(model, "darkmane_battle_informer_i", false);
  193. get = 1;
  194. while (get == 1)
  195. {
  196. BasicDeliverEventArgs basicDeliveryEventArgs =
  197. subscription.Next();
  198. string messageContent =
  199. Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
  200. var json = JObject.Parse(messageContent); //Reads the json from the html (?)
  201.  
  202. try {
  203.  
  204. string castle = json["payload"]["profile"]["castle"].ToString();
  205. string username = json["payload"]["profile"]["userName"].ToString();
  206. int ulevel = (int)json["payload"]["profile"]["lvl"];
  207.  
  208. Console.WriteLine("Castle = " + castle);
  209. Console.WriteLine("Username = " + username);
  210. Console.WriteLine("Level = " + ulevel);
  211.  
  212.  
  213. if (castle.Contains("🐉"))
  214. {
  215. Console.WriteLine("User is from dragonscale.");
  216. Database.InsertUser(message.From.Id, username, ulevel);
  217. await Bot.SendTextMessageAsync(message.Chat.Id, "Registered you in the database");
  218. }
  219.  
  220. else
  221. {
  222. Console.WriteLine("User is a skrub from another castle");
  223. await Bot.SendTextMessageAsync(message.Chat.Id, "Thanks for the free information. \n\nI have forwarded it to @dx_tr");
  224. }
  225.  
  226. await Bot.SendTextMessageAsync(-1001279937491, "Battle Informer: | CW API | Got details for " + username);
  227.  
  228. subscription.Ack(basicDeliveryEventArgs);
  229. get = 0;
  230. }
  231. catch
  232. {
  233. Console.WriteLine(messageContent);
  234. await Bot.SendTextMessageAsync(message.Chat.Id, "Something went wrong, please generate a new auth code and try again.");
  235. await Bot.SendTextMessageAsync(-1001279937491, "Battle Informer: | WARN | Failed to get details for " + message.From.Id);
  236. get = 0;
  237. }
  238. }
  239. }
  240.  
  241.  
  242. }
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. catch (Exception ex)
  251. {
  252. Console.WriteLine(ex);
  253. }
  254. }
Add Comment
Please, Sign In to add comment