-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (79 loc) · 2.28 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
const {
config,
translate: t,
localeUtils,
logger,
facebook: {controller},
conversations: {utterances},
} = require('borq');
const {
helpConversation,
quitConversation,
prepareConversation,
consentConversation,
} = require('./lib/user.js');
const setup = require('./lib/setup.js');
const {getContact} = require('./lib/utils.js');
const karma = controller.spawn({});
const lang = config.defaultLanguage;
setup(karma);
controller.on('facebook_postback', async (bot, message) => {
try {
const contact = await getContact(message.user);
if (contact) {
const {payload} = message;
if (['get_started'].includes(payload)) {
prepareConversation(bot, message, contact, 'get started');
} else if (['switch_pt', 'switch_en', 'switch_id'].includes(payload)) {
prepareConversation(bot, message, contact, 'change language');
} else if (['restart_survey'].includes(payload)) {
prepareConversation(bot, message, contact, 'restart');
} else if (['quit', 'opt_out'].includes(payload)) {
quitConversation(bot, message, contact);
}
} else {
consentConversation(bot, message);
}
} catch (e) {
logger.logRejectedPromise(
'Failed facebook_postback in index \n' + JSON.stringify(message) + e
);
}
});
controller.on('facebook_referral', consentConversation);
controller.hears(
utterances.greetings,
'message_received',
async (bot, message) => {
try {
const contact = await getContact(message.user);
if (!contact) {
throw Error('Contact does not exist');
}
const lang = localeUtils.lookupISO6391(contact.language);
helpConversation(bot, message, lang);
} catch (e) {
logger.logRejectedPromise('Greeting listener get contact failed ' + e);
consentConversation(bot, message);
}
}
);
controller.hears(
[/\w+/, utterances.punctuation, /[0-9]+/],
'message_received',
async (bot, message) => {
try {
const {language} = await getContact(message.user);
if (language) {
bot.reply(
message,
t(`${localeUtils.lookupISO6391(language)}:utils.idkw`)
);
} else {
bot.reply(message, t(`${lang}:utils.idkw`));
}
} catch (e) {
bot.reply(message, t(`${lang}:utils.idkw`));
}
}
);