/
profile.ts
271 lines (217 loc) · 7.23 KB
/
profile.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable consistent-return */
import chalk from 'chalk';
import invariant from 'invariant';
import { MessengerClient, MessengerTypes } from 'messaging-api-messenger';
import { addedDiff, deletedDiff, diff, updatedDiff } from 'deep-object-diff';
import { omit, pick } from 'lodash';
import { snakecase } from 'messaging-api-common';
import getChannelConfig from '../../../shared/getChannelConfig';
import getSubArgs from '../sh/utils/getSubArgs';
import { Channel } from '../../../types';
import { CliContext } from '../..';
import { bold, error, log, print } from '../../../shared/log';
const FIELDS = [
'account_linking_url',
'persistent_menu',
'get_started',
'greeting',
'ice_breakers',
'whitelisted_domains',
'payment_settings',
'target_audience',
'home_url',
];
export const help = (): void => {
console.log(`
bottender messenger profile <action> [option]
${chalk.dim('Actions:')}
get Get the messenger profile
set Set the messenger profile by diff
del, delete Delete all the messenger profile fields
help Show this help
${chalk.dim('Options:')}
-f, --force Force update the messenger profile by config
${chalk.dim('Examples:')}
${chalk.dim('-')} Set the messenger profile
${chalk.cyan('$ bottender messenger profile set')}
${chalk.dim('-')} Force update the messenger profile
${chalk.cyan('$ bottender messenger profile set --force')}
`);
};
export const trimDomain = (
profile: MessengerTypes.MessengerProfile
): MessengerTypes.MessengerProfile => {
const clone = { ...profile };
if (clone.whitelistedDomains) {
clone.whitelistedDomains = clone.whitelistedDomains.map((domain: string) =>
domain.replace(/\/$/, '')
);
}
return clone;
};
export async function getMessengerProfile(_: CliContext): Promise<void> {
try {
const config = getChannelConfig(Channel.Messenger);
const { accessToken } = config;
invariant(
accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const client = MessengerClient.connect({
accessToken,
});
const profile = await client.getMessengerProfile(FIELDS);
if (profile) {
print('The profile is:');
print(`\n${JSON.stringify(profile, null, 2)}`);
} else {
error(`Failed to find ${bold('messenger_profile')} setting`);
}
} catch (err) {
error(`Failed to get ${bold('messenger_profile')} settings`);
if (err.response) {
error(`status: ${bold(err.response.status)}`);
if (err.response.data) {
error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`);
}
} else {
error(err.message);
}
return process.exit(1);
}
}
export async function setMessengerProfile(ctx: CliContext): Promise<void> {
const argv = getSubArgs(ctx.argv, {
'--force': Boolean,
'-f': '--force',
});
const force = argv['--force'];
try {
const config = getChannelConfig(Channel.Messenger);
const { accessToken } = config;
invariant(
accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const { profile: _profile } = getChannelConfig(Channel.Messenger);
const client = MessengerClient.connect({
accessToken,
});
if (force) {
await client.deleteMessengerProfile(FIELDS);
print(
`Successfully delete all ${bold(
'messenger_profile'
)} settings due to ${bold('--force')} option`
);
if (_profile.whitelistedDomains) {
await client.setMessengerProfile(pick(_profile, 'whitelistedDomains'));
await client.setMessengerProfile(omit(_profile, 'whitelistedDomains'));
} else {
await client.setMessengerProfile(_profile);
}
print(`Successfully set ${bold('messenger_profile')} settings`);
} else {
const [_existedProfile] = await client.getMessengerProfile(FIELDS);
const profile = trimDomain(_profile);
const existedProfile = trimDomain(_existedProfile);
const diffResult = diff(existedProfile, profile);
if (Object.keys(diffResult).length !== 0) {
const shouldDeleteFields = Object.keys(
deletedDiff(existedProfile, profile)
);
const shouldSetFields = [
...Object.keys(addedDiff(existedProfile, profile)),
...Object.keys(updatedDiff(existedProfile, profile)),
];
if (shouldDeleteFields.length > 0) {
await client.deleteMessengerProfile(
shouldDeleteFields.map(field => snakecase(field))
);
const deleteFileds = shouldDeleteFields.join(', ');
print(`Successfully delete ${bold(deleteFileds)} settings`);
}
if (shouldSetFields.length > 0) {
const shouldSetProfile = pick(profile, shouldSetFields);
if (shouldSetFields.includes('whitelistedDomains')) {
await client.setMessengerProfile(
pick(shouldSetProfile, 'whitelistedDomains')
);
await client.setMessengerProfile(
omit(shouldSetProfile, 'whitelistedDomains')
);
} else {
await client.setMessengerProfile(shouldSetProfile);
}
const setFields = Object.keys(shouldSetProfile).join(', ');
print(`Successfully set ${bold(setFields)} settings`);
}
} else {
print(`No change apply because the profile settings is the same.`);
}
}
log(
`You can use ${bold(
'bottender messenger profile get'
)} to see the full profile setting.`
);
} catch (err) {
error(`Failed to set ${bold('messenger_profile')} settings`);
if (err.response) {
error(`status: ${bold(err.response.status)}`);
if (err.response.data) {
error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`);
}
} else {
error(err.message);
}
return process.exit(1);
}
}
export async function deleteMessengerProfile(_: CliContext): Promise<void> {
try {
const config = getChannelConfig(Channel.Messenger);
invariant(
config.accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const accessToken = config.accessToken;
const client = MessengerClient.connect({
accessToken,
});
await client.deleteMessengerProfile(FIELDS);
print(`Successfully delete ${bold('messenger_profile')} settings`);
} catch (err) {
error(`Failed to delete ${bold('messenger_profile')} settings`);
if (err.response) {
error(`status: ${bold(err.response.status)}`);
if (err.response.data) {
error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`);
}
} else {
error(err.message);
}
return process.exit(1);
}
}
export default async function main(ctx: CliContext): Promise<void> {
const subcommand = ctx.argv._[2];
switch (subcommand) {
case 'get':
await getMessengerProfile(ctx);
break;
case 'set':
await setMessengerProfile(ctx);
break;
case 'delete':
case 'del':
await deleteMessengerProfile(ctx);
break;
case 'help':
help();
break;
default:
error(`Please specify a valid subcommand: get, set, delete, help`);
help();
}
}