-
Notifications
You must be signed in to change notification settings - Fork 6
/
sendMessage.js
76 lines (68 loc) · 1.92 KB
/
sendMessage.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
var request = require('request');
var getRawBody = require('raw-body');
const fs = require('fs');
require('dotenv').config({ path: './.config' });
var textMsg = {
"msgtype": "text",
"text": {
"content": ""
},
"at":{
"isAtAll": false
}
}
var markdownMsg = {
"msgtype": "markdown",
"markdown": {
"title":"函数计算",
"text":""
},
"at": {
"isAtAll": false
}
}
module.exports.handler = function(req, resp, context) {
var token = process.env.TOKEN;
var error = {
message : "token 错误"
}
var data = fs.readFileSync('urls.txt').toString();
var arr = data.split("\n")
var urls = new Array()
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== '' && arr[i].indexOf('#') === -1 && arr[i].indexOf('http') !== -1) {
urls.push(arr[i])
}
}
var size = urls.length
console.log(urls)
getRawBody(req, function(err, body) {
var str = body.toString()
if (req.queries.token !== token) {
resp.send(JSON.stringify(error, null, ' '));
} else {
if (req.queries.isAtAll === "true") {
textMsg.at.isAtAll = true
markdownMsg.at.isAtAll = true
}
var type = req.queries.type
textMsg.text.content = str
markdownMsg.markdown.text = str
urls.forEach(url => {
request({
url: url,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: type == 'markdown' ? markdownMsg : textMsg
}, function(error, response, body) {
if(--size <= 0) {
resp.send(str);
}
});
});
}
});
};