New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@chatopera/sdk

Package Overview
Dependencies
Maintainers
3
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chatopera/sdk - npm Package Compare versions

Comparing version 1.5.0 to 1.5.1

lib/generate-authorization.js

8

bin/bot.js

@@ -6,7 +6,7 @@ #!/usr/bin/env node

const Bot = require("../index.js");
const tempDir = require("temp-dir");
const tempdir = require("../lib/tempdir");
const path = require("path");
const fs = require("fs");
const debug = require("debug")("chatopera:sdk:cli");
const utils = require("../src/utils.js");
const utils = require("../lib/utils.js");

@@ -121,3 +121,3 @@ /**

let ts = utils.getTimestamp();
tempc66 = path.join(tempDir, pkg.name + "." + ts + ".c66");
tempc66 = path.join(tempdir, pkg.name + "." + ts + ".c66");

@@ -156,2 +156,2 @@ await utils.zipDirectory(botarchive, tempc66);

program.version("1.5.0").parse(process.argv);
program.version(require("../package.json").version).parse(process.argv);

@@ -5,5 +5,5 @@ /**

const debug = require("debug")("chatopera:sdk:index");
const utils = require("./src/utils");
const request = require("superagent");
const generate = require("./src/generate-authorization");
const utils = require("./lib/utils");
const generate = require("./lib/generate-authorization");
const basePath = "/api/v1/chatbot";

@@ -14,3 +14,3 @@ const fs = require("fs");

// TODO request.parse['application/json'] 有bug, 否则用它更好
function extract(res) {
function successHandler(res) {
return new Promise((resolve, reject) => {

@@ -38,2 +38,10 @@ let { rc, data, error, err, msg, message } = res.body;

/**
* 处理异常返回
* @param {*} err
*/
function failHandler(err) {
return Promise.reject(err);
}
/**
* 聊天机器人

@@ -75,28 +83,23 @@ */

detail() {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}`;
request
.get(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
let endpoint = `${basePath}/${this.clientId}`;
return request
.get(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -108,32 +111,27 @@

faq(userId, textMessage) {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}/faq/query`;
request
.post(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
let endpoint = `${basePath}/${this.clientId}/faq/query`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.send({
fromUserId: userId,
query: textMessage
})
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.send({
fromUserId: userId,
query: textMessage
})
.then(successHandler, failHandler);
}

@@ -148,33 +146,29 @@

conversation(userId, textMessage, isDebug = false) {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}/conversation/query`;
request
.post(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
debug("conversation userId %s, textMessage %s", userId, textMessage);
let endpoint = `${basePath}/${this.clientId}/conversation/query`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.send({
fromUserId: userId,
textMessage: textMessage,
isDebug: isDebug
})
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.send({
fromUserId: userId,
textMessage: textMessage,
isDebug: isDebug
})
.then(successHandler, failHandler);
}

@@ -189,32 +183,27 @@

users(limit = 50, page = 1, sortby = "-lasttime") {
return new Promise((resolve, reject) => {
let endpoint =
basePath +
"/" +
this.clientId +
`/users?page=${page}&limit=${limit}&sortby=${sortby}`;
request
.get(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
let endpoint =
basePath +
"/" +
this.clientId +
`/users?page=${page}&limit=${limit}&sortby=${sortby}`;
return request
.get(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -229,32 +218,27 @@

chats(userId, limit = 50, page = 1) {
return new Promise((resolve, reject) => {
let endpoint =
basePath +
"/" +
this.clientId +
`/users/${userId}/chats?page=${page}&limit=${limit}`;
request
.get(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
let endpoint =
basePath +
"/" +
this.clientId +
`/users/${userId}/chats?page=${page}&limit=${limit}`;
return request
.get(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.GET,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -267,28 +251,23 @@

mute(userId) {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}/users/${userId}/mute`;
request
.post(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
let endpoint = `${basePath}/${this.clientId}/users/${userId}/mute`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -301,28 +280,23 @@

unmute(userId) {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}/users/${userId}/unmute`;
request
.post(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
let endpoint = `${basePath}/${this.clientId}/users/${userId}/unmute`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -335,63 +309,53 @@

ismute(userId) {
return new Promise((resolve, reject) => {
let endpoint = `${basePath}/${this.clientId}/users/${userId}/ismute`;
request
.post(this.host + endpoint)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
let endpoint = `${basePath}/${this.clientId}/users/${userId}/ismute`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}
deployConversation(botarchive) {
return new Promise((resolve, reject) => {
let exist = fs.existsSync(botarchive);
if (!exist) {
return reject(new Error("File not exist."));
}
let exist = fs.existsSync(botarchive);
if (!exist) {
return reject(new Error("File not exist."));
}
let endpoint = `${basePath}/${this.clientId}/conversation/droplet/import`;
request
.post(this.host + endpoint)
.set("Content-Type", "multipart/form-data")
.set("Accept", "application/json")
.attach("droplet", botarchive)
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
let endpoint = `${basePath}/${this.clientId}/conversation/droplet/import`;
return request
.post(this.host + endpoint)
.set("X-Requested-With", "XMLHttpRequest")
.set("Expires", "-1")
.set(
"Cache-Control",
"no-cache,no-store,must-revalidate,max-age=-1,private"
)
.set("Content-Type", "multipart/form-data")
.set("Accept", "application/json")
.attach("droplet", botarchive)
.set(
"Authorization",
generate(
this.clientId,
this.clientSecret,
utils.HTTP_METHOD.POST,
endpoint
)
.then(res => {
return extract(res);
})
.then(data => {
resolve(data);
})
.catch(err => {
debug("catch an error %o", err);
return reject(err);
});
});
)
.then(successHandler, failHandler);
}

@@ -398,0 +362,0 @@ }

{
"name": "@chatopera/sdk",
"version": "1.5.0",
"version": "1.5.1",
"description": "Official sdk of Chatopera",

@@ -29,4 +29,3 @@ "main": "index.js",

"inquirer": "^6.3.1",
"superagent": "^5.0.5",
"temp-dir": "^1.0.0"
"superagent": "^5.0.5"
},

@@ -33,0 +32,0 @@ "devDependencies": {

@@ -7,5 +7,5 @@ const test = require("ava");

const clientSecret = "0d8e43cc4de22e9e2cb89f6924fc96e7";
const host = "https://bot.chatopera.com";
const path = require("path");
const curdir = __dirname;
// const host = "https://bot.chatopera.com";

@@ -25,5 +25,10 @@ test("Test generate token", async t => {

const chatbot = new Chatbot(clientId, clientSecret);
let resp = await chatbot.detail();
debug("detail %o, bot name: %s", resp, resp["name"]);
t.pass();
try {
let resp = await chatbot.detail();
debug("detail %o, bot name: %s", resp, resp["name"]);
t.pass();
} catch (e) {
debug("detail error %s", e);
t.pass(e);
}
});

@@ -30,0 +35,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc