@pai-tech/pai-net
Version:
352 lines (279 loc) • 9.77 kB
JavaScript
const {
PAICodeCommand,
PAICodeModule,
PAIModuleConfigParam,
PAIModuleCommandSchema,
PAIModuleCommandParamSchema,
PAILogger
} = require('@pai-tech/pai-code');
const {
Config,
/* Models */
User,
Bot,
/* API Gateway */
UsersApi,
UserBotsApi,
BotAvatarsApi,
BotMessagesApi,
KnowledgeBaseApi,
OrganizationsApi,
/* OAuth */
PAIOAuthClient,
OAuthAccessToken,
OAuthManager
} = require('@pai-tech/pai-net-sdk');
/*
* Configuration parameters name
*/
const CONFIG_TOKEN = 'token';
const CONFIG_BOT_ID = 'bot_id';
const CONFIG_BASE_URL = 'base_url';
const CONFIG_USER_ID = 'user_id';
class PCM_PAI_NET extends PAICodeModule {
constructor() {
let infoText = `
welcome to pai-net:
functions:
1. login(username,password) return success
2. bot-login(username,password,bot) return success
3. bot-login-token(token,bot_id) return success
4. get-user() return user {required: login()}
5. get-bot() return bot {required: bot-login()}
6. create-bot(nickname) return bot {required: login()}
7. get-messages() return PAICodeCommand[] {required: bot-login()}
8. get-knowledge-base(filters) return KnowledgeBase[] - pass filters as JSON string for example: filters:{"since":1533543329}
9. send-message(to,content)
`;
super(infoText);
/**
*
* @type {OAuthManager|null}
*/
this.OAuthManager = null;
const BASE_URL = 'https://console.pai-net.org';
Config.init({
BASE_URL
});
const CLIENT_ID = '4468716878c843fbb806eccd2c9398db';
const CLIENT_SECRET = '4253c842ba99420988f1790aaf4d3a0e';
// create PAI Client
this.oAuthClient = new PAIOAuthClient(CLIENT_ID, CLIENT_SECRET, null);
this.apis = {
users: new UsersApi(),
bots: null,
avatars: new BotAvatarsApi(),
messages: null,
knowledgeBase: new KnowledgeBaseApi(),
organization: null
};
this.user = null;
this.bot = null;
this.config.schema = [
new PAIModuleConfigParam('PAI-NET Base url', 'Base url for PAI-NET Rest API', CONFIG_BASE_URL, 'http://stage.pai-tech.org'),
new PAIModuleConfigParam('bot token', 'please enter your bot authorization token', CONFIG_TOKEN, null),
new PAIModuleConfigParam('bot id', 'please enter your bot id', CONFIG_BOT_ID, null),
new PAIModuleConfigParam('user id', 'please enter your user id', CONFIG_USER_ID, null)
];
this.config.subscribers["PCM_PAI_NET"] = (paramName, value) => {
return this.configChanged(paramName, value);
};
}
/**
* @deprecated
* @return {string}
*/
setModuleName() {
return this.get_module_name()
}
get_module_name() {
return 'pai-net';
}
/**
*
* @return {User|null}
*/
get user() {
return this._user;
}
/**
*
* @param {User} user
*/
set user(user) {
this._user = user;
if (user) {
this.apis.bots = new UserBotsApi(user._id);
this.apis.organization = new OrganizationsApi(user._id);
this.config.setConfigParam(CONFIG_USER_ID, user._id)
.then(success => {
}).catch(err => {
PAILogger.error("PAI-NET (pcm-pai-net.set user):" + err);
});
}
else {
this.apis.bots = null;
}
}
/**
*
* @return {Bot}
*/
get bot() {
return this._bot;
}
/**
*
* @param {Bot} bot
*/
set bot(bot) {
this._bot = bot;
if (bot) {
this.apis.messages = new BotMessagesApi(bot._id);
this.config.setConfigParam(CONFIG_BOT_ID, bot._id)
.then(success => {
}).catch(err => {
PAILogger.error("PAI-NET (pcm-pai-net.set bot):" + err);
});
}
else {
this.apis.messages = null;
}
}
/**
* load basic module commands from super
* and load all the functions for this module
*/
async load() {
await super.load(this);
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "login",
func: "login",
params: {
"username": new PAIModuleCommandParamSchema("username", "Please enter pai-net username", true),
"password": new PAIModuleCommandParamSchema("password", "Please enter pai-net password", true)
}
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "bot-login",
func: "bot_login",
params: {
"username": new PAIModuleCommandParamSchema("username", "Please enter pai-net username", true),
"password": new PAIModuleCommandParamSchema("password", "Please enter pai-net password", true),
"bot_id": new PAIModuleCommandParamSchema("bot_id", "Please enter pai-net bot ID", true)
}
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "bot-login-token",
func: "bot_login_token",
params: {
"token": new PAIModuleCommandParamSchema("token", "Please enter pai-net access-token (must be owner access token)", true),
"bot_id": new PAIModuleCommandParamSchema("bot_id", "Please enter pai-net bot ID", true)
}
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-user",
func: "get_user"
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-bot",
func: "get_bot"
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "create-bot",
func: "create_bot",
params: {
"nickname": new PAIModuleCommandParamSchema("nickname", "bot nickname", true),
"organization": new PAIModuleCommandParamSchema("organization", "organization id", false)
},
showOnInterface: false
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-bots",
func: "getBots"
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "listen",
func: "listen_to_messages",
showOnInterface: false
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-messages",
func: "get_messages",
showOnInterface: false
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-knowledge-base",
func: "get_knowledge_base",
params: {
"filters": new PAIModuleCommandParamSchema("filters", "pass filters as JSON string", false)
}
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "send-message",
func: "send_message",
params: {
"to": new PAIModuleCommandParamSchema("to", "bot id", true),
"content": new PAIModuleCommandParamSchema("content", "content of the message", true)
}
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-organizations",
func: "getOrganizations"
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "get-organization-bots",
func: "getOrganizationBots",
params: {
"organization_id": new PAIModuleCommandParamSchema("organization_id", "organization id", true, "Organization ID"),
}
}));
// reload config
await this.loadConfigFromStorage();
}
/**
* Load the module configuration parameters from storage
*/
async loadConfigFromStorage() {
for (let i = 0; i < this.config.schema.length; i++) {
let paramVal = await this.config.getConfigParam(this.config.schema[i].paramName);
if (paramVal) {
await this.configChanged(this.config.schema[i].paramName,paramVal);
}
}
}
/**
*
* @param {PAICodeCommand} cmd
* @return {Promise<User>}
*/
get_user(cmd) {
return new Promise(async (resolve, reject) => {
resolve(this.user);
});
}
async configChanged(paramName, value) {
if (paramName === CONFIG_TOKEN) {
let accessToken = new OAuthAccessToken();
accessToken.access_token = value;
accessToken.expires_in = 999999999;
// TODO: bring token info from server (token introspection)
await this.postSignIn(accessToken);
}
else if (paramName === CONFIG_BASE_URL) {
Config.init({BASE_URL: value});
}
}
}
// extend the module class
const authorizationExt = require('./src/authorization');
const messagesExt = require('./src/messages');
const knowledgeBaseExt = require('./src/knowledge-base');
const botsExt = require('./src/bots');
const organizationExt = require('./src/organizations');
authorizationExt(PCM_PAI_NET);
messagesExt(PCM_PAI_NET);
knowledgeBaseExt(PCM_PAI_NET);
botsExt(PCM_PAI_NET);
organizationExt(PCM_PAI_NET);
module.exports = PCM_PAI_NET;