Guest User

Untitled

a guest
Apr 3rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         wolfram_alpha_reddit_robin_bot
  3. // @version      2.0
  4. // @namespace    http://moustacheminer.com/dev/alphabot/
  5. // @description  A bot that automatically answers Wolfram Alpha questions.
  6. // @author       lepon01
  7. // @match        https://www.reddit.com/robin*
  8. // @grant        GM_xmlhttpRequest
  9. // @require      http://code.jquery.com/jquery-latest.js
  10. // @downloadURL  http://moustacheminer.com/dev/alphabot/alphabot.js
  11. // @updateURL    http://moustacheminer.com/dev/alphabot/alphabot.js
  12. // ==/UserScript==
  13.  
  14. var wolframID = "2AURT4-KJG4J3QP3E";
  15.  
  16. (function() {
  17.     'use strict';
  18.     var websocket;
  19.     var wsUri = r.config.robin_websocket_url;
  20.  
  21.     websocket = new WebSocket(wsUri);
  22.     websocket.onmessage = function(evt) {onMessage(evt); };
  23.  
  24.     setTimeout(function() {
  25.         //sendMessage("I am a bot, send a message beginning with >> ? << to ask WolframAlpha a question! https://redd.it/4d7nmc");
  26.     }, 2000);
  27.  
  28.     function makeString(object) {
  29.         if (object === null) return '';
  30.         return String(object);
  31.     }
  32.     function truncate(str, length, truncateStr) {
  33.         str = makeString(str);
  34.         truncateStr = truncateStr || '...';
  35.         length = ~~length;
  36.         return str.length > length ? str.slice(0, length) + truncateStr : str;
  37.     }
  38.  
  39.     function onMessage(evt){
  40.         //recieved a message
  41.         var jsonObject = JSON.parse(evt.data);
  42.         if (jsonObject.type == "chat") {
  43.             //recieved message is a chat message, compare contents
  44.             if( jsonObject.payload.body.toLowerCase().startsWith("?") ) {
  45.                 var msg = jsonObject.payload.body.toLowerCase();
  46.                 wolfram(msg);
  47.                 console.log("Wolfram Function Executed");
  48.             }
  49.         }
  50.     }
  51.  
  52.     function sendMessage(message) {
  53.         $("#robinSendMessage > input[type='text']").val("");
  54.         $("#robinSendMessage > input[type='text']").val(message);
  55.         $("#robinSendMessage > input[type='submit']").click();
  56.     }
  57.  
  58.     function wolfram(message) {
  59.         var input = "http://api.wolframalpha.com/v2/query?appid="+wolframID+"&input="+encodeURIComponent(message.substring(1))+"&format=plaintext";
  60.         console.log("Wolfram URL: " + input);
  61.  
  62.         GM_xmlhttpRequest ({
  63.             method: 'GET',
  64.             url: input,
  65.             onload: function (responseDetails) {
  66.                 var xml = $.parseXML(responseDetails.response),
  67.                     $xml = $(xml),
  68.                     $didyoumean = $xml.find('didyoumean'),
  69.                     $input = $xml.find('pod:nth-child(1)').find('plaintext'),
  70.                     $result = $xml.find('pod:nth-child(2)').find('plaintext');
  71.                 var didyoumean = $didyoumean.text();
  72.                 var result = $result.text();
  73.                 var input = $input.text();
  74.                 var output;
  75.  
  76.                 console.log(xml);
  77.  
  78.                 if (result.length > 125) {
  79.                     output = truncate(result,120) + "...";
  80.                 } else {
  81.                     output = result;
  82.                 }
  83.  
  84.                 if ( didyoumean !== "" ) {
  85.                     var separator = didyoumean.replace(/\n/g, " | ");
  86.                     output = "Did you mean: " + separator;
  87.                 }
  88.  
  89.                 if ( input.includes("geoIP") || input.includes("lepon01") || input.includes("nearest location")) {
  90.                     output = "This command is blocked.";
  91.                 }
  92.  
  93.                 console.log(didyoumean);
  94.                 console.log(output);
  95.                 sendMessage("/me Wolfram: " + output);
  96.             }
  97.         });
  98.     }
  99. })();
Advertisement
Add Comment
Please, Sign In to add comment