-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathapi.ts
85 lines (69 loc) · 2.76 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Config } from '../config'
import { SDK } from '.'
import Database from './db'
import { SessionIdentity } from './typings'
export default async (bp: SDK, db: Database) => {
const router = bp.http.createRouterForBot('hitl')
router.get('/sessions', async (req, res) => {
const pausedOnly = req.query.pausedOnly === 'true'
const sessionIds = req.query.searchText && (await db.searchSessions(req.query.searchText))
res.send(await db.getAllSessions(pausedOnly, req.params.botId, sessionIds))
})
router.get('/sessions/:sessionId', async (req, res) => {
const messages = await db.getSessionMessages(req.params.sessionId)
res.send(messages)
})
router.post('/sessions/:sessionId/message', async (req, res) => {
const session = await db.getSessionById(req.params.sessionId)
if (!session) {
return res.sendStatus(404)
}
await bp.events.sendEvent(
bp.IO.Event({
type: 'text',
channel: session.channel,
target: session.userId,
threadId: session.threadId,
botId: req.params.botId,
direction: 'outgoing',
payload: {
agent: true,
text: req.body.message,
preview: req.body.message
}
})
)
res.sendStatus(200)
})
router.post('/sessions/:sessionId/isPaused', async (req, res) => {
res.send(await db.isSessionPaused({ sessionId: req.params.sessionId }))
})
router.post('/channel/:channel/user/:userId/isPaused', async (req, res) => {
const { botId, channel, userId } = req.params
const { threadId } = req.query
res.send(await db.isSessionPaused({ botId, channel, userId, threadId }))
})
const changePauseState = async (isPaused: boolean, targetUser: SessionIdentity, trigger: string = 'operator') => {
const sessionId = await db.setSessionPauseState(isPaused, targetUser, trigger)
bp.realtime.sendPayload(bp.RealTimePayload.forAdmins('hitl.session.changed', { id: sessionId, isPaused }))
}
router.post('/sessions/:sessionId/:action', async (req, res) => {
const { sessionId, action, trigger } = req.params
await changePauseState(action === 'pause', { sessionId }, trigger)
res.sendStatus(200)
})
router.post('/channel/:channel/user/:userId/:action', async (req, res) => {
const { botId, channel, userId, action, trigger } = req.params
const { threadId } = req.query
await changePauseState(action === 'pause', { botId, channel, userId, threadId }, trigger)
res.sendStatus(200)
})
router.get('/config/attributes', async (req, res) => {
try {
const config = (await bp.config.getModuleConfigForBot('hitl', req.params.botId)) as Config
res.send(config.attributes)
} catch (err) {
res.status(400).send(`Can't find attributes: ${err.message}`)
}
})
}