-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (90 loc) · 2.45 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
// https://m.me/testvirtuallab?ref=form.LDfNCy
const uuid = require('uuid');
const PAGE_ID = '935593143497601';
const {translator, addCustomType} = require('@vlab-research/translate-typeform')
const fs = require('fs')
function getFields(path) {
const form = JSON.parse(fs.readFileSync(path, 'utf-8'))
return form.fields.map(addCustomType).map(translator)
}
function makeReferral(userId, formId, time=Date.now(), pageId=PAGE_ID) {
return {
id: uuid(),
time: time,
messaging:[
{ recipient: { id: pageId },
timestamp: Date.now(),
sender: { id: userId },
referral: {
ref: `form.${formId}`,
source: 'SHORTLINK',
type: 'OPEN_THREAD' } } ]
}
}
function _baseMessage(userId, extra, time=Date.now(), pageId=PAGE_ID) {
return {
id: uuid(),
time,
messaging: [{
sender: { id: userId } ,
recipient: { id: pageId },
timestamp: time,
...extra
}]
}
}
function makeEcho(message, userId, time=Date.now(), pageId=PAGE_ID) {
const extra = {
sender: { id: pageId } ,
recipient: { id: userId },
message: {
is_echo: true,
metadata: message.metadata,
text: message.text || message.attachment.payload.text,
}
}
return _baseMessage(userId, extra, time)
}
function makePostback(message, userId, idx, time=Date.now(), pageId=PAGE_ID) {
const button = message.attachment.payload.buttons[idx]
const postback = {payload: button.payload, title: button.title }
return _baseMessage(userId, {postback}, time, pageId)
}
function makeQR(message, userId, idx, time=Date.now(), pageId=PAGE_ID) {
const payload = message.quick_replies[idx].payload
const qr = { quick_reply: { payload }}
return _baseMessage(userId, { message: qr }, time, pageId)
}
function makeTextResponse(userId, text, time=Date.now(), pageId=PAGE_ID) {
return _baseMessage(userId, { message: { text }}, time, pageId)
}
function makeSynthetic(userId, event, pageId=PAGE_ID) {
return {
user: userId,
source: 'synthetic',
page: pageId,
event
}
}
function makeNotify(userId, payload, time=Date.now(), pageId=PAGE_ID) {
const extra = {
optin: {
type: 'one_time_notif_req',
payload: payload,
one_time_notif_token: 'FOOBAR'
}
}
return _baseMessage(userId, extra, time, pageId)
}
module.exports = {
makeReferral,
makeEcho,
makePostback,
makeQR,
makeTextResponse,
getFields,
makeSynthetic,
makeNotify,
_baseMessage,
PAGE_ID
}