-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (101 loc) · 3.79 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Plugin dependencies.
*
* @type {exports}
*/
var _ = require('lodash');
var helpers = require('unibot-helpers');
/**
* Weather plugin for UniBot.
*
* This plugin fetches weather data from http://api.openweathermap.org API.
*
* @param {Object} options Plugin options object, description below.
* db: {mongoose} the mongodb connection
* bot: {irc} the irc bot
* web: {connect} a connect + connect-rest webserver
* config: {object} UniBot configuration
*
* @return {Function} Init function to access shared resources
*/
module.exports = function init(options) {
var config = options.config;
/**
* Default plugin configuration. These can be override on your UniBot config.js file
*
* @type {{
* showFetchMessage: boolean,
* units: string,
* messages: {
* fetch: string,
* success: string,
* error: string
* }
* }}
*/
var pluginConfig = {
"commands": [
"weather"
],
"location": "Jyväskylä",
"showFetchMessage": true,
"units": "metric",
"messages": {
"fetch": "${nick}: wait a moment fetching weather data for '${location}'...",
"success": "${nick}: temperature: ${weather.main.temp}°C (min: ${weather.main.temp_min}°C, max: ${weather.main.temp_max}°C), wind speed: ${weather.wind.speed}m/s, ${weather.weather[0].main}: ${weather.weather[0].description}",
"error": "${nick}: cannot find any weather data for '${location}'..."
}
};
// Merge configuration for plugin
if (_.isObject(config.plugins) && _.isObject(config.plugins.weather)) {
pluginConfig = _.merge(pluginConfig, config.plugins.weather);
}
/**
* Actual UniBot weather plugin implementation. This plugin will fetch weather data from OpenWeatherMap API
* according to given city name.
*/
return function plugin(channel) {
/**
* Actual function to fetch weather data for specified location.
*
* @param {string} location
* @param {string} from
*/
function getWeather(location, from) {
var templateVars = {
nick: from,
location: location
};
if (pluginConfig.showFetchMessage) {
channel.say(_.template(pluginConfig.messages.fetch, templateVars));
}
// Fetch weather data from OpenWeatherMap API
helpers.download('http://api.openweathermap.org/data/2.5/find?units=' + pluginConfig.units + '&q=' + location, function(data) {
try {
var weatherData = JSON.parse(data);
if (parseInt(weatherData.count, 10) === 0) {
channel.say(_.template(pluginConfig.messages.error, templateVars));
} else {
_.each(weatherData.list, function iterator(data) {
templateVars.weather = data;
channel.say(_.template(pluginConfig.messages.success, templateVars));
});
}
} catch (error) {
channel.say(from, 'Oh noes, error: ' + error);
}
});
}
// Initialize command
var command = {};
// Actual regex for this plugin
command['^!(' + pluginConfig.commands.join('|') + ')( (.+))?'] = function match(from, matches) {
var location = pluginConfig.location;
if (!_.isUndefined(matches[2])) {
location = matches[2].trim();
}
getWeather(location, from);
};
return command;
}
};