forked from xianyu110/awesome-openclaw-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather-skill-example.js
More file actions
189 lines (171 loc) · 4.67 KB
/
Copy pathweather-skill-example.js
File metadata and controls
189 lines (171 loc) · 4.67 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* OpenClaw 天气查询 Skill 示例
*
* 功能:查询指定城市的天气信息
*/
const axios = require('axios');
module.exports = {
name: 'weather-skill',
version: '1.0.0',
description: '查询天气信息',
author: 'OpenClaw Community',
config: {
apiKey: {
type: 'string',
required: true,
description: '天气API密钥'
},
apiUrl: {
type: 'string',
default: 'https://api.openweathermap.org/data/2.5/weather',
description: 'API地址'
},
units: {
type: 'string',
default: 'metric',
description: '温度单位 (metric/imperial)'
},
lang: {
type: 'string',
default: 'zh_cn',
description: '语言'
}
},
capabilities: [
{
name: 'getCurrentWeather',
description: '获取当前天气',
parameters: {
city: {
type: 'string',
required: true,
description: '城市名称'
}
}
},
{
name: 'getForecast',
description: '获取天气预报',
parameters: {
city: {
type: 'string',
required: true,
description: '城市名称'
},
days: {
type: 'number',
default: 5,
description: '预报天数'
}
}
}
],
async initialize(context) {
this.config = context.config;
this.apiKey = this.config.apiKey;
this.apiUrl = this.config.apiUrl;
console.log('天气Skill初始化完成');
},
async execute(capability, parameters, context) {
switch (capability) {
case 'getCurrentWeather':
return await this.getCurrentWeather(parameters);
case 'getForecast':
return await this.getForecast(parameters);
default:
throw new Error(`未知的能力: ${capability}`);
}
},
async getCurrentWeather(parameters) {
const { city } = parameters;
try {
const response = await axios.get(this.apiUrl, {
params: {
q: city,
appid: this.apiKey,
units: this.config.units,
lang: this.config.lang
}
});
const data = response.data;
return {
success: true,
data: {
city: data.name,
country: data.sys.country,
temperature: data.main.temp,
feelsLike: data.main.feels_like,
humidity: data.main.humidity,
description: data.weather[0].description,
icon: data.weather[0].icon,
windSpeed: data.wind.speed
},
message: `${city}当前天气:${data.weather[0].description},温度${data.main.temp}°C`
};
} catch (error) {
return {
success: false,
error: `获取天气失败: ${error.message}`
};
}
},
async getForecast(parameters) {
const { city, days } = parameters;
try {
// 这里使用预报API
const forecastUrl = 'https://api.openweathermap.org/data/2.5/forecast';
const response = await axios.get(forecastUrl, {
params: {
q: city,
appid: this.apiKey,
units: this.config.units,
lang: this.config.lang,
cnt: days * 8 // 每天8个时间点
}
});
const data = response.data;
// 按天分组
const forecast = {};
data.list.forEach(item => {
const date = item.dt_txt.split(' ')[0];
if (!forecast[date]) {
forecast[date] = {
date: date,
temps: [],
descriptions: [],
humidity: []
};
}
forecast[date].temps.push(item.main.temp);
forecast[date].descriptions.push(item.weather[0].description);
forecast[date].humidity.push(item.main.humidity);
});
// 计算每天的平均值
const dailyForecast = Object.values(forecast).map(day => ({
date: day.date,
avgTemp: (day.temps.reduce((a, b) => a + b) / day.temps.length).toFixed(1),
maxTemp: Math.max(...day.temps).toFixed(1),
minTemp: Math.min(...day.temps).toFixed(1),
description: day.descriptions[0],
avgHumidity: (day.humidity.reduce((a, b) => a + b) / day.humidity.length).toFixed(0)
}));
return {
success: true,
data: {
city: data.city.name,
country: data.city.country,
forecast: dailyForecast
},
message: `${city}未来${days}天天气预报已获取`
};
} catch (error) {
return {
success: false,
error: `获取天气预报失败: ${error.message}`
};
}
},
async cleanup() {
console.log('天气Skill清理完成');
}
};