This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
125 lines (104 loc) · 2.95 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const smooch = require('./smooch_endpoint');
const PORT = process.env.PORT || 8000;
function processor(name, handlers) {
return async function(req, res) {
const metadata = {};
let stop;
try {
const handler = handlers[req.body.trigger];
if (handler) {
stop = await handler(req.body, (newMetadata) => {
Object.assign(metadata, newMetadata);
});
}
} catch (error) {
console.log(`${name} processor ERROR`, error.message);
}
if (!stop) {
await smooch.continueMessage(metadata, req.body.nonce);
}
res.end();
};
}
const postbackProcessor = processor('postback', {
'postback': async function(body) {
const action = body.postback.action;
const payload = action.payload || action.text;
if (payload) {
await smooch.sendMessage(body.appUser._id, `you chose "${payload}"`);
}
}
});
const skipProcessor = processor('skip', {
'message:appUser': async function(body, setMetadata) {
if (body.message.metadata && body.message.metadata.ignore) {
return;
}
const userProps = await smooch.getUserProps(body.appUser._id);
console.log({
userProps
});
if (userProps.AGENT_SESSION) {
setMetadata({
ignore: true
});
return true;
}
}
});
const echoBotProcessor = processor('echo', {
'message:appUser': async function(body) {
if (body.message.metadata && body.message.metadata.ignore) {
return;
}
await smooch.sendMessage(body.appUser._id, `you said "${body.message.text}"`);
}
});
const sentimentProcessor = processor('sentiment', {
'message:appUser': async function(body, setMetadata) {
setMetadata({
sentimentScore: .5
});
}
});
const dialogProcessor = processor('dialog', {
'message:appUser': async function(body) {
if (body.message.metadata && body.message.metadata.ignore) {
return;
}
if (body.message.text.indexOf('help') !== -1) {
await smooch.setUserProps(body.appUser._id, {
AGENT_SESSION: true
});
await smooch.sendMessage(body.appUser._id, 'Just a moment. Let me get a human');
}
}
});
function agentHandler(req, res) {
// this endpoint simulates final delivery to agent interface
console.log('Delivered', req.body.messages.map((message) => ({
metadata: message.metadata,
text: message.text
})));
res.end();
}
function validateSecret(req, res, next) {
// TODO: validate secret in X-API-Key header
next();
}
express()
.use(morgan('tiny'))
.use(express.static('public'))
.use(bodyParser.json())
.post('/processors/postback', validateSecret, postbackProcessor)
.post('/processors/skip', validateSecret, skipProcessor)
.post('/processors/echo', validateSecret, echoBotProcessor)
.post('/processors/sentiment', validateSecret, sentimentProcessor)
.post('/processors/dialog', validateSecret, dialogProcessor)
.post('/agent', agentHandler)
.listen(PORT, () => console.log(`Listening on port ${PORT}`));