@chatopera/sdk
Advanced tools
Comparing version 1.5.1 to 1.8.0
@@ -103,6 +103,6 @@ #!/usr/bin/env node | ||
let tempc66 = null; | ||
let isRemoveC66 = false; | ||
if (fs.existsSync(botarchive)) { | ||
let isDirectory = fs.lstatSync(botarchive).isDirectory(); | ||
let tempc66 = null; | ||
let isRemoveC66 = false; | ||
if (!isDirectory && botarchive.endsWith(".c66")) { | ||
@@ -135,3 +135,3 @@ // 直接推送 | ||
if (provider) { | ||
new Bot(clientid, clientsecret, provider); | ||
client = new Bot(clientid, clientsecret, provider); | ||
} else { | ||
@@ -138,0 +138,0 @@ client = new Bot(clientid, clientsecret); |
81
index.js
@@ -351,4 +351,85 @@ /** | ||
} | ||
intentSession(uid, channel) { | ||
let endpoint = `${basePath}/${this.clientId}/clause/prover/session`; | ||
debug("intentSession: %s", this.host + endpoint); | ||
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({ uid: uid, channel: channel }) | ||
.then(successHandler, failHandler); | ||
} | ||
intentSessionDetail(sessionId) { | ||
let endpoint = `${basePath}/${this.clientId}/clause/prover/session/${sessionId}`; | ||
debug("intentSessionDetail: %s", this.host + endpoint); | ||
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("Accept", "application/json") | ||
.set( | ||
"Authorization", | ||
generate( | ||
this.clientId, | ||
this.clientSecret, | ||
utils.HTTP_METHOD.GET, | ||
endpoint | ||
) | ||
) | ||
.then(successHandler, failHandler); | ||
} | ||
intentChat(sessionId, uid, textMessage) { | ||
let endpoint = `${basePath}/${this.clientId}/clause/prover/chat`; | ||
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: uid, | ||
session: { id: sessionId }, | ||
message: { | ||
textMessage | ||
} | ||
}) | ||
.then(successHandler, failHandler); | ||
} | ||
} | ||
exports = module.exports = Chatbot; |
{ | ||
"name": "@chatopera/sdk", | ||
"version": "1.5.1", | ||
"version": "1.8.0", | ||
"description": "Official sdk of Chatopera", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "DEBUG=chatopera* ava", | ||
"test": "DEBUG=chatopera* NODE_ENV=development ava", | ||
"test:watch": "DEBUG=chatopera* ava --watch test/" | ||
@@ -13,0 +13,0 @@ }, |
@@ -136,2 +136,27 @@ <p align="center"> | ||
### 进行意图识别对话 | ||
- 创建 session | ||
``` | ||
var session = await chatbot.intentSession(uid, channel); | ||
# 得到sessionID: session.id | ||
``` | ||
其中,`uid`代表用户的唯一表示,`channel`代表用户访问的渠道。这两个字段是用户自定义字符串。 | ||
- 请求意图识别对话 | ||
``` | ||
var reply = await chatbot.intentChat(sessionId, uid, textMessage); | ||
``` | ||
- 获得 session 详情 | ||
``` | ||
var session = await chatbot.intentSessionDetail(sessionId) | ||
``` | ||
在意图识别对话中,session 记录着这次会话的信息,包括用户意图、槽位参数等。 | ||
### 查看用户列表 | ||
@@ -138,0 +163,0 @@ |
const test = require("ava"); | ||
const debug = require("debug")("chatopera:sdk:test"); | ||
const generate = require("../src/generate-authorization"); | ||
const generate = require("../lib/generate-authorization"); | ||
const Chatbot = require("../index"); | ||
const clientId = "5cd29bf76ffd6400173e0189"; | ||
const clientSecret = "0d8e43cc4de22e9e2cb89f6924fc96e7"; | ||
const clientId = "xxx"; | ||
const clientSecret = "xxx"; | ||
const path = require("path"); | ||
@@ -90,1 +90,28 @@ const curdir = __dirname; | ||
}); | ||
test("Test intent chat#create session", async t => { | ||
const chatbot = new Chatbot(clientId, clientSecret); | ||
const session = await chatbot.intentSession("node007", "testclient"); | ||
debug("intent session: %j", session); | ||
t.pass(); | ||
}); | ||
test("Test intent chat#get session detail", async t => { | ||
const chatbot = new Chatbot(clientId, clientSecret); | ||
const session = await chatbot.intentSessionDetail( | ||
"3CC4CB8388981EB1E7F9C81C00000000" | ||
); | ||
debug("intent session: %j", session); | ||
t.pass(); | ||
}); | ||
test.only("Test intent chat", async t => { | ||
const chatbot = new Chatbot(clientId, clientSecret); | ||
const session = await chatbot.intentChat( | ||
"3CC4CB8388981EB1E7F9C81C00000000", | ||
"node007", | ||
"我想打车" | ||
); | ||
debug("intent session: %j", session); | ||
t.pass(); | ||
}); |
49185
12
916
339