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
/
smooch_endpoint.js
130 lines (111 loc) · 3.09 KB
/
smooch_endpoint.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
126
127
128
129
130
'use strict';
require('dotenv').config();
const jwt = require('jsonwebtoken');
const fetch = require('node-fetch');
const SMOOCH_ROOT = process.env.SMOOCH_ROOT || 'https://api.smooch.io';
const ACCOUNT_KEY_ID = process.env.SMOOCH_ACCOUNT_KEY_ID;
const ACCOUNT_SECRET = process.env.SMOOCH_ACCOUNT_SECRET;
const APP_ID = process.env.SMOOCH_APP_ID;
const ACCOUNT_JWT = jwt.sign({
scope: 'account'
}, ACCOUNT_SECRET, {
header: {
kid: ACCOUNT_KEY_ID
}
});
async function request(method, endpoint, data, token) {
const url = `${SMOOCH_ROOT}/v1/apps/${APP_ID}/${endpoint}`;
token = token || ACCOUNT_JWT;
const options = {
method,
headers: {
'Authorization': `Bearer ${token}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
if (data) {
options.body = JSON.stringify(data);
}
const response = await fetch(url, options);
if (response.status >= 400) {
let message = `${method} ${url} failed: ${response.statusText}`;
if (response.headers.get('content-type').startsWith('application/json')) {
const json = await response.json();
if (json.error && json.error.description) {
message += `: ${json.error.description}`;
}
}
throw new Error(message);
}
if (method === 'delete') {
return;
}
return response.json();
}
// continueMessage :: (metadata, temporaryToken) -> Promise()
function continueMessage(metadata, temporaryToken) {
return request('post', 'middleware/continue', {
metadata
}, temporaryToken);
}
// getUserProps :: (userId) -> Promise({ properties })
async function getUserProps(userId) {
const data = await request('get', `appusers/${userId}`);
return data.appUser.properties;
}
// setUserProps :: (userId, properties) -> Promise()
function setUserProps(userId, properties) {
return request('put', `appusers/${userId}`, {
properties
});
}
// sendMessage :: (userId, text) -> Promise()
function sendMessage(userId, text) {
return request('post', `appusers/${userId}/messages`, {
text,
type: 'text',
role: 'appMaker',
name: 'bot'
});
}
// listProcessors :: () -> Promise([ processorIds ])
async function listProcessors() {
const data = await request('get', 'middleware/processors');
return data.processors;
}
// deleteProcessor :: (processorId) -> Promise()
async function deleteProcessor(processorId) {
await request('delete', `middleware/processors/${processorId}`);
}
// createProcessor :: (target, triggers) -> Promise(processorSecret)
async function createProcessor(target, triggers) {
const data = await request('post', 'middleware/processors', {
target,
triggers
});
return {
id: data.processor._id,
secret: data.processor.secret
};
}
// getPipeline :: () -> Promise([ processors ])
async function getPipeline() {
const data = await request('get', 'middleware/pipelines');
return data.pipelines['appUser'];
}
// setPipeline :: (processors) -> Promise()
async function setPipeline(processors) {
await request('put', 'middleware/pipelines/appuser', processors);
}
module.exports = {
createProcessor,
getPipeline,
setPipeline,
listProcessors,
deleteProcessor,
continueMessage,
getUserProps,
setUserProps,
sendMessage
};