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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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));
  50. }
  51. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, queryMessage);
  52. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, resultMessage);
  53. }
  54. else
  55. {
  56. string errorMessage = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  57. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage);
  58. }
  59. }
  60. }
  61. }