UNPKG

wechaty-weixin-openai

Version:
143 lines 6.31 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.WechatyWeixinOpenAI = void 0; const wechaty_1 = require("wechaty"); const wechaty_plugin_contrib_1 = require("wechaty-plugin-contrib"); const normalize_config_1 = require("./normalize-config"); const at_matcher_1 = require("./at-matcher"); const openai_1 = require("./openai"); const DEFAULT_MIN_SCORE = 70; const PRE = 'WechatyWeixinOpenAI'; function WechatyWeixinOpenAI(config) { var _a; wechaty_1.log.verbose(PRE, 'WechatyWeixinOpenAI()'); const { token, encodingAESKey } = normalize_config_1.normalizeConfig(config); const minScore = (_a = config.minScore) !== null && _a !== void 0 ? _a : DEFAULT_MIN_SCORE; wechaty_1.log.verbose(PRE, `minScore: ${minScore}`); const { noAnswerHook } = config; openai_1.WeixinOpenAI.init(token, encodingAESKey); 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') ? at_matcher_1.atMatcher(true) // default: true : at_matcher_1.atMatcher(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 WechatyWeixinOpenAIPlugin(wechaty) { wechaty_1.log.verbose(PRE, 'WechatyWeixinOpenAIPlugin(%s)', wechaty); wechaty.on('message', (message) => __awaiter(this, void 0, void 0, function* () { wechaty_1.log.verbose(PRE, 'WechatyWeixinOpenAIPlugin() wechaty.on(message) %s', message); if (!(yield isPluginMessage(message))) { wechaty_1.log.silly(PRE, 'WechatyWeixinOpenAIPlugin() wechaty.on(message) message not match this plugin, skipped.'); return; } if (!(yield isConfigMessage(message))) { wechaty_1.log.silly(PRE, 'WechatyWeixinOpenAIPlugin() wechaty.on(message) message not match config, skipped.'); return; } const text = yield message.mentionText(); if (!text) { return; } const from = message.from(); const room = message.room(); const answer = yield openai_1.WeixinOpenAI.Instance.aiBot(text, from.id); /** * PreAnswerHook logic, if the hook return false, will skip further process of the message */ if (typeof config.preAnswerHook === 'function') { let sentimentData; if (config.includeSentiment) { sentimentData = yield openai_1.WeixinOpenAI.Instance.sentiment(text, from.id); } const shouldProceed = yield config.preAnswerHook(message, answer, sentimentData); if (typeof shouldProceed === 'boolean' && !shouldProceed) { return; } } if (answer.status === openai_1.ANSWER_STATUS.NOMATCH && noAnswerHook) { yield noAnswerHook(message); return; } const msg = answer.msg; const correctMessage = msg[0]; const reply = correctMessage.content.replace(/LINE_BREAK/g, '\n'); if (from && room && (yield message.mentionSelf())) { yield room.say(reply, from); } else { yield message.say(reply); } })); }; } exports.WechatyWeixinOpenAI = WechatyWeixinOpenAI; //# sourceMappingURL=plugin.js.map