Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name wolfram_alpha_reddit_robin_bot
- // @version 2.0
- // @namespace http://moustacheminer.com/dev/alphabot/
- // @description A bot that automatically answers Wolfram Alpha questions.
- // @author lepon01
- // @match https://www.reddit.com/robin*
- // @grant GM_xmlhttpRequest
- // @require http://code.jquery.com/jquery-latest.js
- // @downloadURL http://moustacheminer.com/dev/alphabot/alphabot.js
- // @updateURL http://moustacheminer.com/dev/alphabot/alphabot.js
- // ==/UserScript==
- var wolframID = "2AURT4-KJG4J3QP3E";
- (function() {
- 'use strict';
- var websocket;
- var wsUri = r.config.robin_websocket_url;
- websocket = new WebSocket(wsUri);
- websocket.onmessage = function(evt) {onMessage(evt); };
- setTimeout(function() {
- //sendMessage("I am a bot, send a message beginning with >> ? << to ask WolframAlpha a question! https://redd.it/4d7nmc");
- }, 2000);
- function makeString(object) {
- if (object === null) return '';
- return String(object);
- }
- function truncate(str, length, truncateStr) {
- str = makeString(str);
- truncateStr = truncateStr || '...';
- length = ~~length;
- return str.length > length ? str.slice(0, length) + truncateStr : str;
- }
- function onMessage(evt){
- //recieved a message
- var jsonObject = JSON.parse(evt.data);
- if (jsonObject.type == "chat") {
- //recieved message is a chat message, compare contents
- if( jsonObject.payload.body.toLowerCase().startsWith("?") ) {
- var msg = jsonObject.payload.body.toLowerCase();
- wolfram(msg);
- console.log("Wolfram Function Executed");
- }
- }
- }
- function sendMessage(message) {
- $("#robinSendMessage > input[type='text']").val("");
- $("#robinSendMessage > input[type='text']").val(message);
- $("#robinSendMessage > input[type='submit']").click();
- }
- function wolfram(message) {
- var input = "http://api.wolframalpha.com/v2/query?appid="+wolframID+"&input="+encodeURIComponent(message.substring(1))+"&format=plaintext";
- console.log("Wolfram URL: " + input);
- GM_xmlhttpRequest ({
- method: 'GET',
- url: input,
- onload: function (responseDetails) {
- var xml = $.parseXML(responseDetails.response),
- $xml = $(xml),
- $didyoumean = $xml.find('didyoumean'),
- $input = $xml.find('pod:nth-child(1)').find('plaintext'),
- $result = $xml.find('pod:nth-child(2)').find('plaintext');
- var didyoumean = $didyoumean.text();
- var result = $result.text();
- var input = $input.text();
- var output;
- console.log(xml);
- if (result.length > 125) {
- output = truncate(result,120) + "...";
- } else {
- output = result;
- }
- if ( didyoumean !== "" ) {
- var separator = didyoumean.replace(/\n/g, " | ");
- output = "Did you mean: " + separator;
- }
- if ( input.includes("geoIP") || input.includes("lepon01") || input.includes("nearest location")) {
- output = "This command is blocked.";
- }
- console.log(didyoumean);
- console.log(output);
- sendMessage("/me Wolfram: " + output);
- }
- });
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment