-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgspotter-slack-bot.js
86 lines (67 loc) · 2.52 KB
/
gspotter-slack-bot.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
var Botkit = require('botkit');
var http = require('http');
var controller = Botkit.slackbot({
debug: true
});
controller.saveTeam({
id: 'T1XMTH7G8'
}, function() {});
controller.setupWebserver(6969, function(err, webserver) {
controller.createWebhookEndpoints(webserver);
});
controller.on('slash_command', function(bot, message) {
if (message.command === '/spot') {
bot.replyAcknowledge();
var symbol = message.text ? message.text : message.channel_name;
symbol = symbol.toUpperCase();
http.get('http://phisix-api3.appspot.com/stocks/' + symbol + '.json', function(response) {
var responseText = ''
response.on('data', function(chunk) {
responseText += chunk;
});
response.on('end', function() {
if (responseText) {
var data = JSON.parse(responseText);
var price = data.stock[0].price.amount;
var change = data.stock[0].percent_change;
var replyMessage = symbol + ' ' + price + ' (' + change + '%)';
bot.replyPublicDelayed(message, replyMessage)
} else {
bot.replyPublic(message, 'Sorry I didn\'t understand what you mean. Would you like to watch me dance instead?');
}
});
})
}
if (message.command === '/gainers' || message.command === '/losers' || message.command === '/actives') {
var url = 'http://www.pse.com.ph/stockMarket/dailySummary.html?method=getAdvancedSecurity&limit=10&ajax=true';
if(message.command === '/losers')
url = 'http://www.pse.com.ph/stockMarket/dailySummary.html?method=getDeclinesSecurity&limit=10&ajax=true';
if(message.command === '/actives')
url = 'http://www.pse.com.ph/stockMarket/dailySummary.html?method=getTopSecurity&limit=10&ajax=true';
console.log('Requesting data to...', url);
http.get( url , function(response) {
var responseText = '';
response.on('data', function(chunk) {
responseText += chunk;
});
response.on('end', function() {
if (responseText) {
var data = JSON.parse(responseText);
var responseSize = data.count;
var replyMessage = '';
for (var i = 0; i < responseSize; i++) {
var symbol = data.records[i].securitySymbol;
var price = data.records[i].lastTradePrice;
var change = data.records[i].percChangeClose;
change = parseFloat(Math.round(change * 100)/100).toFixed(2);
replyMessage += '#' + symbol.toLowerCase() + " " + price + " (" + change + "%)\n";
}
bot.replyPublicDelayed(message, replyMessage);
} else {
bot.replyPublicDelayed(message, 'Unrecognized response');
}
});
});
bot.replyAcknowledge();
}
});