UNPKG

wechaty-puppet-padplus

Version:
265 lines 11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const xml_to_json_1 = require("./xml-to-json"); const wechaty_puppet_1 = require("wechaty-puppet"); const schemas_1 = require("../schemas"); const is_type_1 = require("./is-type"); const split_name_1 = require("./split-name"); /** * * 1. Room Join Event * * * try to find 'join' event for Room * * 1. * 李卓桓 invited Huan to the group chat * 李卓桓 invited 李佳芮, Huan to the group chat * 李卓桓 invited you to a group chat with * 李卓桓 invited you and Huan to the group chat * 2. * "李卓桓"邀请"Huan LI++"加入了群聊 * "李佳芮"邀请你加入了群聊,群聊参与人还有:小桔、桔小秘、小小桔、wuli舞哩客服、舒米 * "李卓桓"邀请你和"Huan LI++"加入了群聊 */ const ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_ZH = [ /^你邀请"(.+)"加入了群聊/, /^" ?(.+)"通过扫描你分享的二维码加入群聊/, ]; const ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_EN = [ /^You invited (.+) to the group chat/, /^" ?(.+)" joined group chat via the QR code you shared/, ]; /* ----------------------------------------------- */ const ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_ZH = [ /^"([^"]+?)"邀请你加入了群聊/, /^"([^"]+?)"邀请你和"(.+)"加入了群聊/, ]; const ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_EN = [ /^(.+) invited you to a group chat/, /^(.+) invited you and (.+) to the group chat/, ]; /* ----------------------------------------------- */ const ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_ZH = [ /^"(.+)"邀请"(.+)"加入了群聊/, ]; const ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_EN = [ /^(.+?) invited (.+?) to (the|a) group chat/, ]; /* ----------------------------------------------- */ const ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_ZH = [ /^" (.+)"通过扫描"(.+)"分享的二维码加入群聊/, ]; const ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_EN = [ /^"(.+)" joined the group chat via the QR Code shared by "(.+)"/, ]; function roomJoinEventMessageParser(rawPayload) { return __awaiter(this, void 0, void 0, function* () { if (!is_type_1.isPayload(rawPayload)) { return null; } const roomId = rawPayload.fromUserName; if (!roomId) { return null; } if (!is_type_1.isRoomId(roomId)) { return null; } const timestamp = rawPayload.createTime; let content = rawPayload.content; /** * when the message is a Recalled type, bot can undo the invitation */ if (rawPayload.msgType === schemas_1.PadplusMessageType.Recalled) { /** * content: * ``` * 3453262102@chatroom: * <sysmsg type="delchatroommember"> * ... * </sysmsg> * ``` */ const tryXmlText = content.replace(/^[^\n]+\n/, ''); const jsonPayload = yield xml_to_json_1.xmlToJson(tryXmlText); // toJson(tryXmlText, { object: true }) as XmlSchema try { if (jsonPayload.sysmsg.$.type === 'delchatroommember') { content = jsonPayload.sysmsg.delchatroommember.plain; } else if (jsonPayload.sysmsg.$.type === 'revokemsg') { content = jsonPayload.sysmsg.revokemsg.replacemsg; } else if (jsonPayload.sysmsg.$.type === 'multivoip') { return null; } else { throw new Error('unknown jsonPayload sysmsg type: ' + jsonPayload.sysmsg.$.type); } } catch (e) { console.error(e); throw e; } } let matchesForBotInviteOtherEn = null; let matchesForOtherInviteBotEn = null; let matchesForOtherInviteOtherEn = null; let matchesForOtherInviteOtherQrcodeEn = null; let matchesForBotInviteOtherZh = null; let matchesForOtherInviteBotZh = null; let matchesForOtherInviteOtherZh = null; let matchesForOtherInviteOtherQrcodeZh = null; ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_EN.some(regex => !!(matchesForBotInviteOtherEn = content.match(regex))); ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_EN.some(regex => !!(matchesForOtherInviteBotEn = content.match(regex))); ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_EN.some(regex => !!(matchesForOtherInviteOtherEn = content.match(regex))); ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_EN.some(regex => !!(matchesForOtherInviteOtherQrcodeEn = content.match(regex))); ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_ZH.some(regex => !!(matchesForBotInviteOtherZh = content.match(regex))); ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_ZH.some(regex => !!(matchesForOtherInviteBotZh = content.match(regex))); ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_ZH.some(regex => !!(matchesForOtherInviteOtherZh = content.match(regex))); ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_ZH.some(regex => !!(matchesForOtherInviteOtherQrcodeZh = content.match(regex))); const matchesForBotInviteOther = matchesForBotInviteOtherEn || matchesForBotInviteOtherZh; const matchesForOtherInviteBot = matchesForOtherInviteBotEn || matchesForOtherInviteBotZh; const matchesForOtherInviteOther = matchesForOtherInviteOtherEn || matchesForOtherInviteOtherZh; const matchesForOtherInviteOtherQrcode = matchesForOtherInviteOtherQrcodeEn || matchesForOtherInviteOtherQrcodeZh; const languageEn = matchesForBotInviteOtherEn || matchesForOtherInviteBotEn || matchesForOtherInviteOtherEn || matchesForOtherInviteOtherQrcodeEn; const languageZh = matchesForBotInviteOtherZh || matchesForOtherInviteBotZh || matchesForOtherInviteOtherZh || matchesForOtherInviteOtherQrcodeZh; const matches = matchesForBotInviteOther || matchesForOtherInviteBot || matchesForOtherInviteOther || matchesForOtherInviteOtherQrcode; if (!matches) { return null; } /** * * Parse all Names From the Event Text * */ if (matchesForBotInviteOther) { /** * 1. Bot Invite Other to join the Room * (include invite via QrCode) */ const other = matches[1]; let inviteeNameList; if (languageEn) { inviteeNameList = split_name_1.splitEnglishNameList(other); } else if (languageZh) { inviteeNameList = split_name_1.splitChineseNameList(other); } else { throw new Error('make typescript happy'); } const inviterName = wechaty_puppet_1.YOU; const joinEvent = { inviteeNameList, inviterName, roomId, timestamp, }; return joinEvent; } else if (matchesForOtherInviteBot) { /** * 2. Other Invite Bot to join the Room */ // /^"([^"]+?)"邀请你加入了群聊/, // /^"([^"]+?)"邀请你和"(.+?)"加入了群聊/, const inviterName = matches[1]; let inviteeNameList = [wechaty_puppet_1.YOU]; if (matches[2]) { let nameList; if (languageEn) { nameList = split_name_1.splitEnglishNameList(matches[2]); } else if (languageZh) { nameList = split_name_1.splitChineseNameList(matches[2]); } else { throw new Error('neither English nor Chinese'); } inviteeNameList = inviteeNameList.concat(nameList); } const joinEvent = { inviteeNameList, inviterName, roomId, timestamp, }; return joinEvent; } else if (matchesForOtherInviteOther) { /** * 3. Other Invite Other to a Room * (NOT include invite via Qrcode) */ // /^"([^"]+?)"邀请"([^"]+)"加入了群聊$/, // /^([^"]+?) invited ([^"]+?) to (the|a) group chat/, const inviterName = matches[1]; let inviteeNameList; const other = matches[2]; if (languageEn) { inviteeNameList = split_name_1.splitEnglishNameList(other); } else if (languageZh) { inviteeNameList = split_name_1.splitChineseNameList(other); } else { throw new Error('neither English nor Chinese'); } const joinEvent = { inviteeNameList, inviterName, roomId, timestamp, }; return joinEvent; } else if (matchesForOtherInviteOtherQrcode) { /** * 4. Other Invite Other via Qrcode to join a Room * /^" (.+)"通过扫描"(.+)"分享的二维码加入群聊/, */ const inviterName = matches[2]; let inviteeNameList; const other = matches[1]; if (languageEn) { inviteeNameList = split_name_1.splitEnglishNameList(other); } else if (languageZh) { inviteeNameList = split_name_1.splitChineseNameList(other); } else { throw new Error('neither English nor Chinese'); } const joinEvent = { inviteeNameList, inviterName, roomId, timestamp, }; return joinEvent; } else { throw new Error('who invite who?'); } }); } exports.roomJoinEventMessageParser = roomJoinEventMessageParser; //# sourceMappingURL=room-event-join-message-parser.js.map