The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Wolfram_Alpha.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Web;
  5. using System.Xml;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Wolfram_Alpha : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. }
  14. public override void ParseCommand(CommandMessage command)
  15. {
  16. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  17. switch (foundCommand.Name)
  18. {
  19. case "Wolfram Alpha Search":
  20. GetResults(command);
  21. break;
  22. }
  23. }
  24. private void GetResults(CommandMessage command)
  25. {
  26. string URL = "http://api.wolframalpha.com/v2/query?input=" + HttpUtility.UrlEncode(command.Arguments["Query"]) + "&appid=" + GetOptionValue("API") + "&format=plaintext";
  27. XmlNodeList xnList = null;
  28. try
  29. {
  30. WebClient web = new WebClient();
  31. web.Encoding = Encoding.UTF8;
  32. string results = web.DownloadString(URL);
  33. XmlDocument xmlDoc = new XmlDocument();
  34. xmlDoc.LoadXml(results);
  35. xnList = xmlDoc.SelectNodes("/queryresult/pod");
  36. }
  37. catch
  38. {
  39. string errorMessage = string.Format("Unable to fetch results for \u0002{0}\u000F.", command.Arguments["Query"]);
  40. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage);
  41. }
  42. if (xnList != null && xnList.Count > 1)
  43. {
  44. string queryMessage = string.Format("Result for: {0}", xnList[0]["subpod"]["plaintext"].InnerText);
  45. string resultMessage = xnList[1]["subpod"]["plaintext"].InnerText;
  46. int maxResults = Convert.ToInt32(GetOptionValue("Max Response Length"));
  47. if (resultMessage.Length > (int)maxResults)
  48. {
  49. resultMessage = string.Format("{0}...", resultMessage.Substring(0, (int)maxResults - 3));
  50. }
  51. char[] tails = {';', ' '};
  52. resultMessage = resultMessage
  53. .Replace("\r\n", "; ")
  54. .Replace("\n", "; ")
  55. .Replace(" | ", ", ")
  56. .Replace(" | ", ": ")
  57. .TrimEnd(tails);
  58. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, queryMessage);
  59. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, resultMessage);
  60. }
  61. else
  62. {
  63. string errorMessage = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  64. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage);
  65. }
  66. }
  67. }
  68. }