Skip to content

Commit 9a542f5

Browse files
committed
新增天气查询
1 parent cedaa65 commit 9a542f5

File tree

8 files changed

+2549
-60
lines changed

8 files changed

+2549
-60
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
node_modules
22
.vscode
33
data
4-
config.js
4+
.idea
5+
package-lock.json
6+
Dockerfile
7+
docker-compose.yaml

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
如果你已经清楚了相关配置,那么启动机器人只需要以下语句:
2525

2626
```shell
27-
node main 1770874035
27+
node main.js 1770874035
28+
# 使用node main可能导致无限重启
2829
# 后面是机器人的账号,当然你可以结合原项目文档自行配置
2930
```
3031

config.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
// 将此文件更名为config.js才能生效哦
3+
4+
module.exports = {
5+
6+
//通用配置
7+
general: {
8+
platform: 1, //1:安卓手机 2:aPad 3:安卓手表 4:MacOS 5:iPad
9+
debug: false, //开启debug
10+
use_cqhttp_notice: true, //是否使用cqhttp标准的notice事件格式
11+
12+
host: "0.0.0.0", //监听主机名
13+
port: 5700, //端口
14+
use_http: false, //启用http
15+
use_ws: false, //启用正向ws,和http使用相同地址和端口
16+
access_token: "", //访问api的token
17+
secret: "", //上报数据的sha1签名密钥
18+
post_timeout: 30, //post超时时间(秒)
19+
post_message_format:"array", //"string"或"array"
20+
enable_cors: false, //是否允许跨域请求
21+
enable_heartbeat: false, //是否启用ws心跳
22+
heartbeat_interval: 15000, //ws心跳间隔(毫秒)
23+
rate_limit_interval:500, //使用_rate_limited后缀限速调用api的排队间隔时间(毫秒)
24+
event_filter: "", //json格式的事件过滤器文件路径
25+
post_url: [ //上报地址,可以添加多个url
26+
// "http://your.address.com:80",
27+
],
28+
ws_reverse_url: [ //反向ws地址,可以添加多个url
29+
// "ws://your.address.com:8080",
30+
],
31+
ws_reverse_reconnect_interval: 3000, //反向ws断线重连间隔(毫秒),设为负数直接不重连
32+
ws_reverse_reconnect_on_code_1000: true, //反向ws是否在关闭状态码为1000的时候重连
33+
},
34+
35+
//每个账号的单独配置(用于覆盖通用配置)
36+
};
37+
38+
// 安全注意:
39+
// 监听0.0.0.0表示监听网卡上的所有地址。如果你的机器可以通过公网ip直接访问,同时你也没有设定access_token,则被认为是极不安全的。
40+
// 你应该知道这样做会导致以下后果:任何人都可以无限制地访问你的Bot的所有API接口。
41+
// 如果只需要在本地访问,建议将监听地址改为localhost。需要通过公网访问,你最好设定access_token。

lib/core.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,9 @@ function createBot() {
203203
data.message = data.raw_message;
204204
if (data.message_type === 'group') { // 群聊
205205
messageHandler.messageGroupHandler(data, bot)
206-
} else if (data.message_type === 'private') { // 私聊
207-
messageHandler.messagePrivateHandler(data, bot)
208-
}
209-
// console.log(data)
206+
}
210207
dipatch(data);
211208
});
212-
// bot.on('message.private', (data) => {
213-
// if (config.post_message_format === 'string')
214-
// data.message = data.raw_message
215-
216-
// })
217209
}
218210

219211
/**
@@ -320,7 +312,7 @@ function createServer() {
320312

321313
/**
322314
* ws连接建立
323-
* @param {WebSocket} ws
315+
* @param {WebSocket} ws
324316
*/
325317
function onWSOpen(ws) {
326318
ws.on("message", (data)=>{
@@ -383,7 +375,7 @@ function createWSClient(url, headers) {
383375

384376
/**
385377
* 收到http响应
386-
* @param {http.ServerResponse} res
378+
* @param {http.ServerResponse} res
387379
*/
388380
function onHttpRes(event, res) {
389381
let data = [];
@@ -400,8 +392,8 @@ function onHttpRes(event, res) {
400392

401393
/**
402394
* 收到http请求
403-
* @param {http.ClientRequest} req
404-
* @param {http.ServerResponse} res
395+
* @param {http.ClientRequest} req
396+
* @param {http.ServerResponse} res
405397
*/
406398
async function onHttpReq(req, res) {
407399
res.setHeader("Content-Type", "application/json; charset=utf-8");
@@ -448,7 +440,7 @@ async function onHttpReq(req, res) {
448440

449441
/**
450442
* 收到ws消息
451-
* @param {WebSocket} ws
443+
* @param {WebSocket} ws
452444
*/
453445
async function onWSMessage(ws, data) {
454446
debug(`收到WS消息: ` + data);

lib/message.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
const axios = require("axios");
12
// 消息收发功能,分为群发和私聊
23

34
// 允许的群组(白名单)
45
const groups = [
56
'828192817', // 2022乐程招新群
67
'742958634', // 测试群
8+
'575626767'// dev
79
]
810

911

@@ -68,13 +70,13 @@ const messageGroupConfig = [
6870
})
6971
}
7072
},
71-
{
73+
{
7274
keywords: '列表',
7375
reply: [
7476
{
7577
type: "text",
7678
data: {
77-
text: `@并回复以下关键字:\n1. 乐程是什么\n2. 丢骰子\n3. 签到`
79+
text: `@并回复以下关键字:\n1. 乐程是什么\n2. 丢骰子\n3. 签到\n4.天气`
7880
}
7981
}
8082
]
@@ -85,7 +87,7 @@ const messageGroupConfig = [
8587
{
8688
type: "text",
8789
data: {
88-
text: `@并回复以下关键字:\n1. 乐程是什么\n2. 丢骰子\n3. 签到`
90+
text: `@并回复以下关键字:\n1. 乐程是什么\n2. 丢骰子\n3. 签到\n4.天气`
8991
}
9092
}
9193
],
@@ -94,18 +96,56 @@ const messageGroupConfig = [
9496
// 这里还要处理一下数据,不然有点问题
9597
bot.sendGroupMsg(828192817, d.message)
9698
})
97-
}
99+
}
100+
},
101+
{
102+
keywords: '你是谁',
103+
reply: [
104+
{
105+
type: "text",
106+
data: {
107+
text: `我是乐程机器人人人人人人人人人人人人人人人人人人人人人人人人人人人人人人人`
108+
}
109+
}
110+
]
111+
},
112+
{
113+
keywords: '天气',
114+
reply: [],
115+
callback: (data, bot) => {
116+
117+
return new Promise((resolve, reject) => {
118+
119+
axios.get("http://aider.meizu.com/app/weather/listWeather?cityIds=101270101").then(res => {
120+
let s = []
121+
let value = res.data.value[0]
122+
123+
s.push(value.city + '气温 ' + value.realtime.temp + ' °C')
124+
value.indexes.forEach(e => {
125+
if (e.content !== '' && e.content !== undefined) {
126+
s.push(e.content)
127+
}
128+
})
129+
console.log(s.join('\n\n'))
130+
resolve(s.join('\n\n'))
131+
})
132+
133+
})
134+
}
98135
}
136+
99137
]
100138

101139

102140
// 处理群发
103141
function messageGroupHandler(data, bot) {
104-
if(data.message[0].type === 'at' && data.atme === true) { // 先要@自己才能触发
105-
if(groups.includes(data.group_id.toString())) { // 如果在白名单群组里面
106-
for(let one of messageGroupConfig) { // 遍历每个配置,观察里面是否有匹配的关键字
107-
if(data.raw_message.trim().indexOf(one.keywords) != -1) { // 匹配成功
108-
if(one.callback) { // callback回调处理数据后回复
142+
console.log(data)
143+
console.log(bot)
144+
if (data.message[0].type === 'at' && data.atme === true) { // 先要@自己才能触发
145+
if (groups.includes(data.group_id.toString())) { // 如果在白名单群组里面
146+
for (let one of messageGroupConfig) { // 遍历每个配置,观察里面是否有匹配的关键字
147+
if (data.raw_message.trim().indexOf(one.keywords) !== -1) { // 匹配成功
148+
if (one.callback) { // callback回调处理数据后回复
109149
one.callback(data, bot).then((res) => {
110150
data.reply(res)
111151
})
@@ -121,13 +161,7 @@ function messageGroupHandler(data, bot) {
121161
}
122162
}
123163

124-
// 处理私发
125-
function messagePrivateHandler(data) {
126-
127-
}
128-
129164
module.exports = {
130-
messageGroupHandler,
131-
messagePrivateHandler
165+
messageGroupHandler
132166
}
133167

main.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ try {
1111
require("./config.js");
1212
} catch (e) {
1313
console.log(e);
14-
console.log(`
15-
未找到config.js文件或配置有语法错误
16-
`);
14+
console.log(`未找到config.js文件或配置有语法错误`);
1715
process.exit(0);
1816
}
1917

20-
try {
21-
console.log(`正在检查&更新内核版本..`);
22-
require("child_process").execSync("npm up --no-save", {stdio: "ignore"});
23-
} catch (e) {
24-
console.log(`"npm up --no-save"执行失败,你可能需要手动执行。`);
25-
}
18+
// try {
19+
// console.log(`正在检查&更新内核版本..`);
20+
// require("child_process").execSync("npm up --no-save", {stdio: "ignore"});
21+
// } catch (e) {
22+
// console.log(`"npm up --no-save"执行失败,你可能需要手动执行。`);
23+
// }
2624

2725
// 引入核心依赖
2826
require("oicq");

0 commit comments

Comments
 (0)