UNPKG

wechaty-qnamaker

Version:
124 lines 5.33 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 }); exports.WechatyQnAMaker = void 0; const wechaty_1 = require("wechaty"); const wechaty_plugin_contrib_1 = require("wechaty-plugin-contrib"); const asker_1 = require("./asker"); const normalize_config_1 = require("./normalize-config"); const mention_matcher_1 = require("./mention-matcher"); function WechatyQnAMaker(config) { wechaty_1.log.verbose('WechatyQnAMaker', 'WechatyQnAMaker(%s)', JSON.stringify(config)); const normalizedConfig = normalize_config_1.normalizeConfig(config); const ask = asker_1.asker(normalizedConfig); const matchContact = typeof config.contact === 'undefined' ? () => true : wechaty_plugin_contrib_1.matchers.contactMatcher(config.contact); const matchRoom = typeof config.room === 'undefined' ? () => true : wechaty_plugin_contrib_1.matchers.roomMatcher(config.room); const matchSkipMessage = typeof config.skipMessage === 'undefined' ? () => false // default not skip any messages : wechaty_plugin_contrib_1.matchers.messageMatcher(config.skipMessage); const matchMention = (typeof config.mention === 'undefined') ? mention_matcher_1.mentionMatcher(true) // default: true : mention_matcher_1.mentionMatcher(config.mention); const matchLanguage = (typeof config.language === 'undefined') ? () => true // match all language by default : wechaty_plugin_contrib_1.matchers.languageMatcher(config.language); const isPluginMessage = (message) => __awaiter(this, void 0, void 0, function* () { if (message.self()) { return false; } if (message.type() !== wechaty_1.Message.Type.Text) { return false; } const mentionList = yield message.mentionList(); if (mentionList.length > 0) { if (!(yield message.mentionSelf())) { return false; } } return true; }); const isConfigMessage = (message) => __awaiter(this, void 0, void 0, function* () { const from = message.from(); const room = message.room(); if (yield matchSkipMessage(message)) { return false; } if (room) { if (!(yield matchRoom(room))) { return false; } if (!(yield matchMention(message))) { return false; } /** * Mention others but not include the bot */ const mentionList = yield message.mentionList(); const mentionSelf = yield message.mentionSelf(); if (mentionList.length > 0 && !mentionSelf) { return false; } } else { if (from && !(yield matchContact(from))) { return false; } } const text = yield message.mentionText(); if (!matchLanguage(text)) { return false; } return true; }); /** * Connect with Wechaty */ return function WechatyQnAMakerPlugin(wechaty) { wechaty_1.log.verbose('WechatyQnAMaker', 'WechatyQnAMakerPlugin(%s)', wechaty); wechaty.on('message', (message) => __awaiter(this, void 0, void 0, function* () { wechaty_1.log.verbose('WechatyQnAMaker', 'WechatyQnAMakerPlugin() wechaty.on(message) %s', message); if (!(yield isPluginMessage(message))) { wechaty_1.log.silly('WechatyQnAMaker', 'WechatyQnAMakerPlugin() wechaty.on(message) message not match this plugin, skipped.'); return; } if (!(yield isConfigMessage(message))) { wechaty_1.log.silly('WechatyQnAMaker', 'WechatyQnAMakerPlugin() wechaty.on(message) message not match config, skipped.'); return; } const text = yield message.mentionText(); if (!text) { return; } const answers = yield ask(text); if (answers.length <= 0) { return; } const answer = answers[0].answer; if (!answer) { throw new Error('no answer?'); } const from = message.from(); const room = message.room(); if (from && room && (yield message.mentionSelf())) { yield room.say(answer, from); } else { yield message.say(answer); } })); }; } exports.WechatyQnAMaker = WechatyQnAMaker; //# sourceMappingURL=plugin.js.map