|
@@ -51,47 +51,65 @@ class default_1 {
|
|
|
};
|
|
|
}
|
|
|
});
|
|
|
- this.sendTo = (subscriber, msg) => (() => {
|
|
|
- switch (subscriber.chatType) {
|
|
|
- case 'group':
|
|
|
- return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
|
|
|
- case 'private':
|
|
|
- return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
|
|
|
- // currently disabled
|
|
|
- case 'temp':
|
|
|
- return this.bot.api.sendTempMessage(msg, subscriber.chatID.qq, subscriber.chatID.group);
|
|
|
- }
|
|
|
- })()
|
|
|
- .then(response => {
|
|
|
- logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response:`);
|
|
|
- logger.info(response);
|
|
|
- })
|
|
|
- .catch(reason => {
|
|
|
- logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
|
|
|
- throw Error(reason);
|
|
|
- });
|
|
|
- this.uploadPic = (img, timeout = -1) => {
|
|
|
+ this.sendTo = (subscriber, msg) => {
|
|
|
+ const chain = [];
|
|
|
+ const voices = [];
|
|
|
+ return (() => {
|
|
|
+ if (typeof msg !== 'string') {
|
|
|
+ msg.forEach(singleMsg => (singleMsg.type === 'Voice' ? voices : chain).push(singleMsg));
|
|
|
+ msg = chain;
|
|
|
+ }
|
|
|
+ switch (subscriber.chatType) {
|
|
|
+ case 'group':
|
|
|
+ return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
|
|
|
+ case 'private':
|
|
|
+ return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
|
|
|
+ // currently disabled
|
|
|
+ case 'temp':
|
|
|
+ return this.bot.api.sendTempMessage(msg, subscriber.chatID.qq, subscriber.chatID.group);
|
|
|
+ }
|
|
|
+ })()
|
|
|
+ .then(response => {
|
|
|
+ logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response:`);
|
|
|
+ logger.info(response);
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ if (voices.length && subscriber.chatType === 'group') {
|
|
|
+ voices.forEach((voice, index) => this.bot.api.sendGroupMessage([voice], subscriber.chatID)
|
|
|
+ .then(voiceResponse => {
|
|
|
+ logger.info(`pushing voice #${index} to ${JSON.stringify(subscriber.chatID)} was successful, response:`);
|
|
|
+ logger.info(voiceResponse);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(reason => {
|
|
|
+ logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
|
|
|
+ throw Error(reason);
|
|
|
+ });
|
|
|
+ };
|
|
|
+ this.upload = (mediaMsg, timeout = -1) => {
|
|
|
+ const idKeyName = `${mediaMsg.type.toLowerCase()}Id`;
|
|
|
if (timeout)
|
|
|
timeout = Math.floor(timeout);
|
|
|
if (timeout === 0 || timeout < -1) {
|
|
|
return Promise.reject('Error: timeout must be greater than 0ms');
|
|
|
}
|
|
|
- let imgFile;
|
|
|
- if (img.imageId !== '')
|
|
|
+ let file;
|
|
|
+ if (mediaMsg[idKeyName] !== '')
|
|
|
return Promise.resolve();
|
|
|
- if (img.url !== '') {
|
|
|
- if (img.url.split(':')[0] !== 'data') {
|
|
|
+ if (mediaMsg.url !== '') {
|
|
|
+ if (mediaMsg.url.split(':')[0] !== 'data') {
|
|
|
return Promise.reject('Error: URL must be of protocol "data"');
|
|
|
}
|
|
|
- if (img.url.split(',')[0].split(';')[1] !== 'base64') {
|
|
|
+ if (mediaMsg.url.split(',')[0].split(';')[1] !== 'base64') {
|
|
|
return Promise.reject('Error: data URL must be of encoding "base64"');
|
|
|
}
|
|
|
temp.track();
|
|
|
try {
|
|
|
const tempFile = temp.openSync();
|
|
|
- fs_1.writeSync(tempFile.fd, Buffer.from(img.url.split(',')[1], 'base64'));
|
|
|
+ fs_1.writeSync(tempFile.fd, Buffer.from(mediaMsg.url.split(',')[1], 'base64'));
|
|
|
fs_1.closeSync(tempFile.fd);
|
|
|
- imgFile = tempFile.path;
|
|
|
+ file = tempFile.path;
|
|
|
}
|
|
|
catch (error) {
|
|
|
logger.error(error);
|
|
@@ -99,16 +117,18 @@ class default_1 {
|
|
|
}
|
|
|
try {
|
|
|
this.bot.axios.defaults.timeout = timeout === -1 ? 0 : timeout;
|
|
|
- logger.info(`uploading ${JSON.stringify(exports.Message.Image(img.imageId, `${img.url.split(',')[0]},[...]`, img.path))}...`);
|
|
|
- return this.bot.api.uploadImage('group', imgFile || img.path)
|
|
|
+ logger.info(`uploading ${JSON.stringify(exports.Message[mediaMsg.type](mediaMsg[idKeyName], `${mediaMsg.url.split(',')[0]},[...]`, mediaMsg.path))}...`);
|
|
|
+ return this.bot.api[`upload${mediaMsg.type}`]('group', file || mediaMsg.path)
|
|
|
.then(response => {
|
|
|
- logger.info(`uploading ${img.path} as group image was successful, response:`);
|
|
|
+ logger.info(`uploading ${mediaMsg.path} as group ${mediaMsg.type.toLowerCase()} was successful, response:`);
|
|
|
logger.info(JSON.stringify(response));
|
|
|
- img.url = '';
|
|
|
- img.path = response.path.split(/[/\\]/).slice(-1)[0];
|
|
|
+ if (mediaMsg.type === 'Voice')
|
|
|
+ mediaMsg[idKeyName] = response[idKeyName];
|
|
|
+ mediaMsg.url = '';
|
|
|
+ mediaMsg.path = response.path.split(/[/\\]/).slice(-1)[0];
|
|
|
})
|
|
|
.catch(reason => {
|
|
|
- logger.error(`error uploading ${img.path}, reason: ${reason}`);
|
|
|
+ logger.error(`error uploading ${mediaMsg.path}, reason: ${reason}`);
|
|
|
throw Error(reason);
|
|
|
});
|
|
|
}
|
|
@@ -124,13 +144,13 @@ class default_1 {
|
|
|
host: this.botInfo.host,
|
|
|
port: this.botInfo.port,
|
|
|
});
|
|
|
- this.bot.axios.defaults.maxContentLength = Infinity;
|
|
|
+ this.bot.axios.defaults.maxContentLength = this.bot.axios.defaults.maxBodyLength = Infinity;
|
|
|
this.bot.on('NewFriendRequestEvent', evt => {
|
|
|
logger.debug(`detected new friend request event: ${JSON.stringify(evt)}`);
|
|
|
this.bot.api.groupList()
|
|
|
.then((groupList) => {
|
|
|
if (groupList.some(groupItem => groupItem.id === evt.groupId)) {
|
|
|
- evt.respond('allow');
|
|
|
+ evt.respond(0);
|
|
|
return logger.info(`accepted friend request from ${evt.fromId} (from group ${evt.groupId})`);
|
|
|
}
|
|
|
logger.warn(`received friend request from ${evt.fromId} (from group ${evt.groupId})`);
|
|
@@ -142,7 +162,7 @@ class default_1 {
|
|
|
this.bot.api.friendList()
|
|
|
.then((friendList) => {
|
|
|
if (friendList.some(friendItem => friendItem.id = evt.fromId)) {
|
|
|
- evt.respond('allow');
|
|
|
+ evt.respond(0);
|
|
|
return logger.info(`accepted group invitation from ${evt.fromId} (friend)`);
|
|
|
}
|
|
|
logger.warn(`received group invitation from ${evt.fromId} (unknown)`);
|