wechaty-weixin-openai
Version:
Weixin OpenAI Plugin for Wechaty
107 lines • 4.36 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ANSWER_STATUS = exports.WeixinOpenAI = void 0;
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const axios_1 = __importDefault(require("axios"));
const wechaty_1 = require("wechaty");
const const_1 = require("./const");
const aibot_1 = require("./schema/aibot");
Object.defineProperty(exports, "ANSWER_STATUS", { enumerable: true, get: function () { return aibot_1.ANSWER_STATUS; } });
const sentiment_1 = require("./schema/sentiment");
/**
* This is a singleton class
*/
class WeixinOpenAI {
/**
* ---------------------------------------------------
* Instance method below
* ---------------------------------------------------
*/
/**
* use token and encoding aes key to call weixin openai api
* @param token token
* @param encodingAESKey encoding aes key
*/
constructor(token, encodingAESKey) {
this.token = token;
this.encodingAESKey = encodingAESKey;
wechaty_1.log.info(`WeixinOpenAI constructor(${this.token.slice(0, 5)}, ${this.encodingAESKey.slice(0, 10)})`);
}
static get Instance() {
if (!this.instance) {
throw new Error('No instance found, call init first.');
}
return this.instance;
}
/**
* This method needs to be called before every other call on
* the instance, this will pass the token and encodingAESKey
* to weixin openai api for authorization
* @param token token
* @param encodingAESKey encoding aes key
*/
static init(token, encodingAESKey) {
this.instance = new WeixinOpenAI(token, encodingAESKey);
}
/**
* Call Weixin OpenAI aibot api to get the response of a give message
* @param msg query message needs to be answered
* @param username user who ask the question
*/
aiBot(query, userId) {
return __awaiter(this, void 0, void 0, function* () {
const originalUrl = const_1.AI_BOT_URL;
const signature = this.encodeJwt({ userid: userId });
const data = {
query,
signature,
};
const result = yield this.request(originalUrl, data);
return result;
});
}
sentiment(query, userId, mode = sentiment_1.SENTIMENT_MODE.SIX_CLASS) {
return __awaiter(this, void 0, void 0, function* () {
const originalUrl = const_1.SENTIMENT_URL;
const tokenData = this.encodeJwt({
data: {
mode,
q: query,
},
uid: userId,
});
const result = yield this.request(originalUrl, {
query: tokenData,
});
return result.result.reduce((prev, cur) => {
const key = sentiment_1.SENTIMENT_KEY_MAP[cur[0]];
return Object.assign(Object.assign({}, prev), { [key]: cur[1] });
}, {});
});
}
request(originalUrl, data) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${originalUrl}/${this.token}`;
const result = yield axios_1.default.post(url, data);
return result.data;
});
}
encodeJwt(data) {
const signature = jsonwebtoken_1.default.sign(data, this.encodingAESKey, { algorithm: 'HS256' });
return signature;
}
}
exports.WeixinOpenAI = WeixinOpenAI;
//# sourceMappingURL=index.js.map