-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
99 lines (80 loc) · 2.34 KB
/
bot.js
File metadata and controls
99 lines (80 loc) · 2.34 KB
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
93
94
95
96
97
98
99
const aws = require('aws-sdk');
const lambda = new aws.Lambda();
const botBuilder = require('claudia-bot-builder');
const slackDelayedReply = botBuilder.slackDelayedReply;
const snow = require('snow-forecast-sfr');
const _ = require('lodash');
const api = botBuilder((message, apiRequest) => {
return new Promise((resolve, reject) => {
lambda.invoke({
FunctionName: apiRequest.lambdaContext.functionName,
Qualifier: apiRequest.lambdaContext.functionVersion,
InvocationType: 'Event',
Payload: JSON.stringify({
slackEvent: message
})
}, (err, done) => {
if (err) return reject(err);
resolve(done);
});
}).then(() => {
return {
text: `Ok, Let me check ...`,
response_type: 'in_channel'
}
}).catch(() => {
return `Could not ping forecast`
});
});
api.intercept((event) => {
if (!event.slackEvent) return event;
const message = event.slackEvent;
const place = message.text;
if (!_.trim(message.text).length) {
return slackDelayedReply(message, {
response_type: 'in_channel',
text: 'Please specify place ...'
})
.then(() => false);
}
return new Promise((resolve, reject) => {
snow.parseResort(place, 'mid', function (json) {
var result = {};
result.title = 'There is snow forecast say, on ' + place;
result.attachments = _.map(json.forecast, function (cast) {
return {
title: cast.date,
text: 'snow: ' + cast.snow + ' | rain: ' + cast.rain + ' | minTemp: ' + fToC(cast.minTemp) + ' | maxTemp: ' + fToC(cast.maxTemp)
};
});
prepareAttachments(result);
resolve(result);
});
}).then((result) => {
return slackDelayedReply(message, {
response_type: 'in_channel',
text: result.title,
attachments: result.attachments
})
.then(() => false);
});
});
function fToC(f) {
return Math.round((f - 32) / 1.8);
}
function prepareAttachments(result) {
var attachments = [];
_.each(_.groupBy(result.attachments, 'title'), function (items, title) {
var attachment = {
title: title,
text: []
};
items.forEach(function (item) {
attachment.text.push(item.text)
});
attachment.text = attachment.text.join('\n');
attachments.push(attachment);
});
result.attachments = attachments;
}
module.exports = api;