Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1155 from botpress/rl_talk_route
feat(core): add converse api
- Loading branch information
Showing
26 changed files
with
953 additions
and
679 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
config: | ||
target: 'http://localhost:3000' | ||
phases: | ||
- duration: 10 # lasts for 'X' seconds | ||
arrivalRate: 1 # 'X' new users per second | ||
payload: | ||
path: 'users.csv' | ||
fields: ['prefix', 'uuid'] | ||
order: 'sequence' | ||
processor: './functions.js' | ||
scenarios: | ||
- name: Each user sends 'X' messages to channel-api | ||
flow: | ||
- log: Testing channel-api | ||
- function: 'setupTestId' | ||
- loop: | ||
- post: | ||
url: '/api/v1/bots/welcome-bot/converse/{{ prefix }}{{ testId }}_{{ uuid }}' | ||
json: | ||
type: 'text' | ||
text: 'U{{ testId }}_{{ uuid }}|M{{ $loopCount }}' | ||
count: 50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { ContentElement } from 'botpress/sdk' | ||
import { AdminService } from 'core/services/admin/service' | ||
import AuthService, { TOKEN_AUDIENCE } from 'core/services/auth/auth-service' | ||
import { DefaultSearchParams } from 'core/services/cms' | ||
import { CMSService } from 'core/services/cms' | ||
import { RequestHandler, Router } from 'express' | ||
|
||
import { CustomRouter } from '..' | ||
import { checkTokenHeader, needPermissions } from '../util' | ||
|
||
export class ContentRouter implements CustomRouter { | ||
public readonly router: Router | ||
|
||
private _checkTokenHeader: RequestHandler | ||
private _needPermissions: (operation: string, resource: string) => RequestHandler | ||
|
||
constructor(private adminService: AdminService, private authService: AuthService, private cms: CMSService) { | ||
this._needPermissions = needPermissions(this.adminService) | ||
this._checkTokenHeader = checkTokenHeader(this.authService, TOKEN_AUDIENCE) | ||
|
||
this.router = Router({ mergeParams: true }) | ||
this.setupRoutes() | ||
} | ||
|
||
setupRoutes() { | ||
this.router.get( | ||
'/types', | ||
this._checkTokenHeader, | ||
this._needPermissions('read', 'bot.content'), | ||
async (req, res) => { | ||
const botId = req.params.botId | ||
const types = await this.cms.getAllContentTypes(botId) | ||
|
||
const response = await Promise.map(types, async type => { | ||
const count = await this.cms.countContentElementsForContentType(botId, type.id) | ||
return { | ||
id: type.id, | ||
count, | ||
title: type.title, | ||
schema: { | ||
json: type.jsonSchema, | ||
ui: type.uiSchema, | ||
title: type.title, | ||
renderer: type.id | ||
} | ||
} | ||
}) | ||
|
||
res.send(response) | ||
} | ||
) | ||
|
||
this.router.get( | ||
'/elements/count', | ||
this._checkTokenHeader, | ||
this._needPermissions('read', 'bot.content'), | ||
async (req, res) => { | ||
const botId = req.params.botId | ||
const count = await this.cms.countContentElements(botId) | ||
res.send({ count }) | ||
} | ||
) | ||
|
||
this.router.get( | ||
'/:contentType?/elements', | ||
this._checkTokenHeader, | ||
this._needPermissions('read', 'bot.content'), | ||
async (req, res) => { | ||
const { botId, contentType } = req.params | ||
const query = req.query || {} | ||
|
||
const elements = await this.cms.listContentElements(botId, contentType, { | ||
...DefaultSearchParams, | ||
count: Number(query.count) || DefaultSearchParams.count, | ||
from: Number(query.from) || DefaultSearchParams.from, | ||
searchTerm: query.searchTerm || DefaultSearchParams.searchTerm, | ||
ids: (query.ids && query.ids.split(',')) || DefaultSearchParams.ids | ||
}) | ||
|
||
const augmentedElements = await Promise.map(elements, this._augmentElement) | ||
res.send(augmentedElements) | ||
} | ||
) | ||
|
||
this.router.get( | ||
'/:contentType?/elements/count', | ||
this._checkTokenHeader, | ||
this._needPermissions('read', 'bot.content'), | ||
async (req, res) => { | ||
const { botId, contentType } = req.params | ||
const count = await this.cms.countContentElementsForContentType(botId, contentType) | ||
res.send({ count }) | ||
} | ||
) | ||
|
||
this.router.get( | ||
'/elements/:elementId', | ||
this._checkTokenHeader, | ||
this._needPermissions('read', 'bot.content'), | ||
async (req, res) => { | ||
const { botId, elementId } = req.params | ||
const element = await this.cms.getContentElement(botId, elementId) | ||
res.send(await this._augmentElement(element)) | ||
} | ||
) | ||
|
||
this.router.post( | ||
'/:contentType/elements/:elementId?', | ||
this._checkTokenHeader, | ||
this._needPermissions('write', 'bot.content'), | ||
async (req, res) => { | ||
const { botId, contentType, elementId } = req.params | ||
const element = await this.cms.createOrUpdateContentElement(botId, contentType, req.body.formData, elementId) | ||
res.send(element) | ||
} | ||
) | ||
|
||
this.router.post( | ||
'/elements/bulk_delete', | ||
this._checkTokenHeader, | ||
this._needPermissions('write', 'bot.content'), | ||
async (req, res) => { | ||
await this.cms.deleteContentElements(req.params.botId, req.body) | ||
res.sendStatus(200) | ||
} | ||
) | ||
} | ||
|
||
private _augmentElement = async (element: ContentElement) => { | ||
const contentType = await this.cms.getContentType(element.contentType) | ||
return { | ||
...element, | ||
schema: { | ||
json: contentType.jsonSchema, | ||
ui: contentType.uiSchema, | ||
title: contentType.title, | ||
renderer: contentType.id | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ConverseService } from 'core/services/converse' | ||
import { Router } from 'express' | ||
import _ from 'lodash' | ||
|
||
import { CustomRouter } from '..' | ||
|
||
export class ConverseRouter implements CustomRouter { | ||
public readonly router: Router | ||
|
||
constructor(private converseService: ConverseService) { | ||
this.router = Router({ mergeParams: true }) | ||
this.setupRoutes() | ||
} | ||
|
||
setupRoutes() { | ||
this.router.post('/:userId', async (req, res) => { | ||
const { userId, botId } = req.params | ||
const rawOutput = await this.converseService.sendMessage(botId, userId, req.body).catch(err => { | ||
return res.status(408).json({ error: err }) | ||
}) | ||
|
||
const formatedOutput = this.prepareResponse(rawOutput, req.query.include) | ||
return res.json(formatedOutput) | ||
}) | ||
} | ||
|
||
private prepareResponse(output, params: string) { | ||
const split = (params && params.toLowerCase().split(',')) || [] | ||
|
||
if (!split.includes('nlu')) { | ||
delete output.nlu | ||
} | ||
if (!split.includes('state')) { | ||
delete output.state | ||
} | ||
|
||
return output | ||
} | ||
} |
Oops, something went wrong.