The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

WolframAlpha.cs 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Net;
  2. using System.Text;
  3. using System.Xml;
  4. namespace Combot.Modules.ModuleClasses
  5. {
  6. public class WolframAlpha : Module
  7. {
  8. public override void Initialize()
  9. {
  10. Bot.CommandReceivedEvent += HandleCommandEvent;
  11. }
  12. public override void ParseCommand(CommandMessage command)
  13. {
  14. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  15. switch (foundCommand.Name)
  16. {
  17. case "Wolfram Alpha Search":
  18. GetResults(command);
  19. break;
  20. }
  21. }
  22. private void GetResults(CommandMessage command)
  23. {
  24. string URL = "http://api.wolframalpha.com/v2/query?input=" + System.Web.HttpUtility.UrlEncode(command.Arguments["Query"]) + "&appid=" + GetOptionValue("API") + "&format=plaintext";
  25. XmlNodeList xnList = null;
  26. try
  27. {
  28. WebClient web = new WebClient();
  29. web.Encoding = Encoding.UTF8;
  30. string results = web.DownloadString(URL);
  31. XmlDocument xmlDoc = new XmlDocument();
  32. xmlDoc.LoadXml(results);
  33. xnList = xmlDoc.SelectNodes("/queryresult/pod");
  34. }
  35. catch
  36. {
  37. string errorMessage = string.Format("Unable to fetch results for \u0002{0}\u000F.", command.Arguments["Query"]);
  38. switch (command.MessageType)
  39. {
  40. case MessageType.Channel:
  41. Bot.IRC.SendPrivateMessage(command.Location, errorMessage);
  42. break;
  43. case MessageType.Query:
  44. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, errorMessage);
  45. break;
  46. case MessageType.Notice:
  47. Bot.IRC.SendNotice(command.Nick.Nickname, errorMessage);
  48. break;
  49. }
  50. }
  51. if (xnList.Count > 1)
  52. {
  53. string queryMessage = string.Format("Result for: {0}", xnList[0]["subpod"]["plaintext"].InnerText);
  54. string resultMessage = xnList[1]["subpod"]["plaintext"].InnerText;
  55. switch (command.MessageType)
  56. {
  57. case MessageType.Channel:
  58. Bot.IRC.SendPrivateMessage(command.Location, queryMessage);
  59. Bot.IRC.SendPrivateMessage(command.Location, resultMessage);
  60. break;
  61. case MessageType.Query:
  62. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, queryMessage);
  63. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, resultMessage);
  64. break;
  65. case MessageType.Notice:
  66. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, queryMessage);
  67. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, resultMessage);
  68. break;
  69. }
  70. }
  71. else
  72. {
  73. string errorMessage = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  74. switch (command.MessageType)
  75. {
  76. case MessageType.Channel:
  77. Bot.IRC.SendPrivateMessage(command.Location, errorMessage);
  78. break;
  79. case MessageType.Query:
  80. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, errorMessage);
  81. break;
  82. case MessageType.Notice:
  83. Bot.IRC.SendNotice(command.Nick.Nickname, errorMessage);
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }