This repository was archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwolframalpha.js
92 lines (79 loc) · 2.81 KB
/
wolframalpha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const https = require('https');
const winston = require('winston');
const Plugin = require('../utils.js').Plugin;
var wolfram = require('wolfram-alpha');
const META = {
name: 'wolframalpha',
short: 'Execute a computation using WolframAlpha\'s API',
examples: [
'## Shortened Output (Default)',
'wa integrate 2x',
'wa pi to 100 digits',
'wa length of the Eiffel tower',
'## Verbose Output',
'wav integrate 2x'
],
};
function buildAttachement(results, verboseOutput) {
return new Promise((resolve, reject) => {
let fields = [];
let outputText = results.length == 0 ? "Couldn't compute your request" : '';
const timestamp = Math.floor(new Date() / 1000);
if (!results.length == 0) {
if (verboseOutput) {
fields = results.map(function(obj) {
var rObj = {};
rObj['title'] = obj.title;
rObj['value'] = obj.subpods[0].text;
rObj['image'] = obj.subpods[0].image;
rObj['short'] = false;
return rObj;
});
} else {
fields = [{
'title': results[0].title,
'value': results[0].subpods[0].text
},
{
'title': results[1].title,
'value': results[1].subpods[0].text,
'image': results[1].subpods[0].image
}];
}
}
const outputImage = results.length == 0 ? '' : fields[1].image;
resolve({
as_user: true,
attachments: [{
text: outputText,
color: '#36a64f',
author_name: 'Bosta',
fields: fields,
image_url: outputImage,
footer: 'Wolfram|Alpha',
footer_icon: 'https://platform.slack-edge.com/img/default_application_icon.png',
ts: timestamp
}],
});
});
}
function waQuery(options, message, query, routeOptions) {
if (!query) {
message.reply('I got nothing to compute');
return;
}
wolfram.query(query)
.then(results => buildAttachement(results, routeOptions.verboseOutput))
.then(attachment => options.web.chat.postMessage(message.channel, '', attachment))
.catch(error => winston.error(`${META.name} Error: ${error}`));
}
function register(bot, rtm, web, config, secret) {
const plugin = new Plugin({ bot, rtm, web, config });
wolfram = wolfram.createClient(secret.wolframalpha_app_id);
plugin.route(/^wa (.+[^)])$/, waQuery, { verboseOutput: false });
plugin.route(/^wav (.+[^)])$/, waQuery, { verboseOutput: true });
}
module.exports = {
register,
META,
};