-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage_process.js
157 lines (147 loc) · 4.82 KB
/
Message_process.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
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
import { getNumberbyName } from './Utils.js';
import { processCommand } from './command_process.js';
import terminalImage from 'terminal-image';
import whtswebjs from 'whatsapp-web.js';
import fs from 'node:fs';
import sharp from 'sharp';
const { MessageMedia } = whtswebjs;
async function processMention(client,chat,message){
let mention = message.match(/@[^ ]*/g);
if(mention === null){
return undefined;
}
mention = mention.map(mention => mention.slice(1).trim());
for(const i in mention){
const n = mention[i];
if(n.match(/^[0-9]+$/)){
mention[i] = n + '@c.us';
}else if(chat.isGroup&&n == "everyone"){
let text = ""
mention = [];
for(participant of chat.participants){
mention.push(`${participant.id.usre}@c.us`);
text += `@${participant.id.user} `;
}
message = message.replace('@everyone',text);
break;
}else{
const number = await getNumberbyName(n,client)
if(!number){
mention[i] = undefined;
continue;
}
//repace the name with the number
message = message.replace('@' + n,'@' + number);
mention[i] = number + '@c.us';
}
}
mention = mention.filter(mention => mention !== undefined);
if(!mention.length){
return undefined;
}
return [message,mention];
}
function processContent(message){
//Format <Content Type::Content|COntent Path(default path:media/)>
const msg = message.match(/<[^>]*>/g);
let options = {};
if(msg === null|| msg.length !== 1){
return undefined;
}
let content = msg[0];
try{
content = JSON.parse(content.slice(1,-1).replaceAll("'",'"'));
}catch(e){
console.log(e);
return undefined;
}
let output;
let path;
switch(Object.keys(content)[0]){//Type requires to be on first key
case "media":
//process path
path = content["media"];
if(path.at(0) != '/'){
path = process.cwd()+'/media/' + content["media"];
}
output = MessageMedia.fromFilePath(path);
options.caption = message.replace(msg,"");
break;
case "custom_stick":
//process path
path = content["stick"];
let author;
let name;
if(path.at(0) != '/'){
path = process.cwd() +'/sticker/' + content["stick"];
}
author = content["author"]||"smarttommyau";
name = content["name"]||"Whatsapp Automation";
if(!output){
if(!fs.existsSync(path)){
return undefined;
}
output = MessageMedia.fromFilePath(path);
}
options.stickerAuthor = author;
options.stickerName = name;
options.sendMediaAsSticker = true;
break;
case "stick":
//process path
path = content["stick"];
if(path.at(0) != '/'){
path = process.cwd() +'/stickers/' + content["stick"];
}
output = MessageMedia.fromFilePath(path);
options.sendMediaAsSticker = true;
break;
// case "poll":
// case "Poll":
break;
default:
break;
}
if(!output){
return undefined;
}
return [output,options];
}
export async function processMessage(client,chat,message){
const temp = await processMention(client,chat,message);
let options = {};
let msg = message;
if(temp !== undefined){
msg = temp.at(0);
options.mentions = temp.at(1);
}
const content = processContent(msg);
if(content){
msg = content.at(0);
options = content.at(1);
}
return [msg,options];
}
export async function sendMessageWithContent(client,chat,message){//support for mention
//TODO: allow send message with Content
const [msg,options] = await processMessage(client,chat,message);
await chat.sendMessage(msg,options);
return;
}
export async function previewMessage(msg,options){
if(msg instanceof MessageMedia){
let image;
if(msg.mimetype.includes('gif')){
image = await terminalImage.gifbuffer(Buffer.from(msg.data,'base64'),{height: '30%'})
}else if(msg.mimetype.includes('/png') || msg.mimetype.includes('jpg')){
image = await terminalImage.buffer(Buffer.from(msg.data,'base64'),{height: '30%'})
}else{
image = await terminalImage.buffer(await sharp(Buffer.from(msg.data,'base64')).png().toBuffer(),{height: '30%'});
}
console.log(image)
}else{
console.log(msg);
}
console.log(options);
return;
}