forked from kaiyuanshe/wechat-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg-parser.js
More file actions
44 lines (41 loc) · 1.41 KB
/
msg-parser.js
File metadata and controls
44 lines (41 loc) · 1.41 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
const parseString = require('xml2js').parseString;
const Server_Address = process.env.SERVER_ADDRESS;
const Web_Files_Path = process.env.WEB_FILES_PATH;
exports.getJson = function (xml) {
var json = null;
parseString(xml, function (err, result) {
json = result;
});
return json;
}
exports.saveMessageFiles = async function (bot, msg) {
var msg_type = msg.payload.type;
if (msg_type == bot.Message.Type.Image) {
const file = await msg.toFileBox();
const name = Web_Files_Path + file.name;
console.log('Save file to: ' + name);
file.toFile(name);
return Server_Address + file.name;
} else {
return null;
}
}
exports.getMsgText = async function (bot, msg) {
msg_text = msg.text();
if (msg.payload.type == bot.Message.Type.Image) {
img_url = await saveMessageFiles(bot, msg).catch(error => console.log(error.message));
msg_text = "";
}
if (msg.payload.type == bot.Message.Type.Emoticon) {
var json = getJson(msg.text());
img_url = json.msg.emoji[0]["$"].cdnurl;
msg_text = "";
}
if (msg.payload.type == bot.Message.Type.Url) {
var json = getJson(msg.text());
var title = json.msg.appmsg[0].title[0];
var url = json.msg.appmsg[0].url[0];
msg_text = "[" + title + "](" + url + ")";
}
return msg_text;
}