@meltwater/conversations-api-services 1.0.19 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +2 -0
- package/babel.config.js +18 -0
- package/dist/cjs/data-access/http/InstagramVideoClient.js +42 -0
- package/dist/cjs/data-access/http/WarpZoneApi.client.js +32 -0
- package/dist/cjs/data-access/http/amazonS3.js +44 -0
- package/dist/cjs/data-access/http/asset-manager-tvm.client.js +35 -0
- package/dist/cjs/data-access/http/companiesApi.client.js +38 -0
- package/dist/cjs/data-access/http/credentialsApi.client.js +102 -0
- package/dist/cjs/data-access/http/entitlementsApi.client.js +40 -0
- package/dist/cjs/data-access/http/facebook.native.js +344 -0
- package/dist/cjs/data-access/http/facebookApi.client.js +631 -0
- package/dist/cjs/data-access/http/featureToggleApi.client.js +31 -0
- package/dist/cjs/data-access/http/identityServices.client.js +97 -0
- package/dist/cjs/data-access/http/instagramApi.client.js +428 -0
- package/dist/cjs/data-access/http/ir.client.js +242 -0
- package/dist/cjs/data-access/http/linkedInApi.client.js +491 -0
- package/dist/cjs/data-access/http/masf.client.js +101 -0
- package/dist/cjs/data-access/http/tiktok.native.js +162 -0
- package/dist/cjs/data-access/http/tiktokApi.client.js +441 -0
- package/dist/cjs/data-access/index.js +132 -0
- package/dist/cjs/errors/engage-error.js +16 -0
- package/dist/cjs/errors/http-error.js +23 -0
- package/dist/cjs/lib/applicationTags.helpers.js +30 -0
- package/dist/cjs/lib/configuration.js +14 -0
- package/dist/cjs/lib/document-action-events.js +12 -0
- package/dist/cjs/lib/externalId.helpers.js +19 -0
- package/dist/cjs/lib/hidden.helpers.js +13 -0
- package/dist/cjs/lib/hiddenComment.helper.js +119 -0
- package/dist/cjs/lib/logger.helpers.js +68 -0
- package/dist/cjs/lib/logger.js +23 -0
- package/dist/cjs/lib/message.helpers.js +58 -0
- package/dist/cjs/lib/metrics.helper.js +97 -0
- package/dist/esm/data-access/http/InstagramVideoClient.js +34 -0
- package/dist/esm/data-access/http/WarpZoneApi.client.js +24 -0
- package/dist/esm/data-access/http/amazonS3.js +37 -0
- package/dist/esm/data-access/http/asset-manager-tvm.client.js +28 -0
- package/dist/esm/data-access/http/companiesApi.client.js +30 -0
- package/dist/esm/data-access/http/credentialsApi.client.js +92 -0
- package/dist/esm/data-access/http/entitlementsApi.client.js +32 -0
- package/dist/esm/data-access/http/facebook.native.js +325 -0
- package/dist/esm/data-access/http/facebookApi.client.js +621 -0
- package/dist/esm/data-access/http/featureToggleApi.client.js +23 -0
- package/dist/esm/data-access/http/identityServices.client.js +89 -0
- package/dist/esm/data-access/http/instagramApi.client.js +420 -0
- package/dist/esm/data-access/http/ir.client.js +234 -0
- package/dist/esm/data-access/http/linkedInApi.client.js +481 -0
- package/dist/esm/data-access/http/masf.client.js +93 -0
- package/dist/esm/data-access/http/tiktok.native.js +146 -0
- package/dist/esm/data-access/http/tiktokApi.client.js +433 -0
- package/dist/esm/data-access/index.js +30 -0
- package/dist/esm/errors/engage-error.js +9 -0
- package/dist/esm/errors/http-error.js +16 -0
- package/dist/esm/lib/applicationTags.helpers.js +22 -0
- package/dist/esm/lib/configuration.js +8 -0
- package/dist/esm/lib/document-action-events.js +6 -0
- package/dist/esm/lib/externalId.helpers.js +12 -0
- package/dist/esm/lib/hidden.helpers.js +6 -0
- package/dist/esm/lib/hiddenComment.helper.js +112 -0
- package/dist/esm/lib/logger.helpers.js +60 -0
- package/dist/esm/lib/logger.js +16 -0
- package/dist/esm/lib/message.helpers.js +52 -0
- package/dist/esm/lib/metrics.helper.js +90 -0
- package/package.json +14 -4
- package/src/data-access/http/facebook.native.js +542 -0
- package/src/data-access/http/tiktok.native.js +248 -0
- package/src/data-access/index.js +4 -0
- package/src/errors/engage-error.js +11 -0
- package/src/errors/http-error.js +19 -0
- package/src/lib/logger.helpers.js +15 -0
- package/src/lib/message.helpers.js +7 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.IdentityServicesClient = void 0;
|
|
7
|
+
var _superagent = _interopRequireDefault(require("superagent"));
|
|
8
|
+
var _logger = _interopRequireDefault(require("../../lib/logger.js"));
|
|
9
|
+
var _configuration = _interopRequireDefault(require("../../lib/configuration.js"));
|
|
10
|
+
var _loggerHelpers = require("../../lib/logger.helpers.js");
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
class IdentityServicesClient {
|
|
13
|
+
constructor(_ref) {
|
|
14
|
+
let {
|
|
15
|
+
companyId,
|
|
16
|
+
services
|
|
17
|
+
} = _ref;
|
|
18
|
+
this.companyId = companyId;
|
|
19
|
+
this.identityServicesUrl = _configuration.default.get('IDENTITY_SERVICES_URL');
|
|
20
|
+
this.authService = services.auth;
|
|
21
|
+
}
|
|
22
|
+
async getById(userId, jwt) {
|
|
23
|
+
let retries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|
24
|
+
let user;
|
|
25
|
+
try {
|
|
26
|
+
let resignedToken = await this.authService.getResignedToken(jwt);
|
|
27
|
+
const loggerChild = _logger.default.child({
|
|
28
|
+
[_loggerHelpers.MeltwaterAttributes.USERID]: userId,
|
|
29
|
+
[_loggerHelpers.MeltwaterAttributes.COMPANYID]: this.companyId
|
|
30
|
+
});
|
|
31
|
+
user = await _superagent.default.get(`${this.identityServicesUrl}/users/${userId}`).set('Authorization', resignedToken).set('x-client-name', _configuration.default.get('CLIENT_NAME_HEADER')).then(result => result.body.user);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (retries <= 3) {
|
|
34
|
+
loggerChild.error(`getById Failed requesting Identity Services for userId ${userId} retry ${retries}`, error);
|
|
35
|
+
return this.getById(userId, jwt, ++retries);
|
|
36
|
+
} else {
|
|
37
|
+
loggerChild.error(`getById Failed requesting Identity Services app settings for userId ${userId} complete failure`, error);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return user;
|
|
42
|
+
}
|
|
43
|
+
async getAllUsers(jwt) {
|
|
44
|
+
let retries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
45
|
+
let users;
|
|
46
|
+
const loggerChild = _logger.default.child({
|
|
47
|
+
[_loggerHelpers.MeltwaterAttributes.COMPANYID]: this.companyId
|
|
48
|
+
});
|
|
49
|
+
try {
|
|
50
|
+
let resignedToken = await this.authService.getResignedToken(jwt);
|
|
51
|
+
users = await _superagent.default.get(`${this.identityServicesUrl}/companies/${this.companyId}/users`).set('Authorization', resignedToken).set('x-client-name', _configuration.default.get('CLIENT_NAME_HEADER')).then(result => result.body.map(record => record.user));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (retries <= 3) {
|
|
54
|
+
loggerChild.error(`getAllUsers Failed requesting Identity Services for ${this.companyId} retrying ${retries}`, error);
|
|
55
|
+
return await this.getAllUsers(jwt, ++retries);
|
|
56
|
+
} else {
|
|
57
|
+
loggerChild.error(`getAllUsers Failed requesting Identity Services app settings for companyId ${this.companyId} complete failure`, error);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
return users;
|
|
62
|
+
}
|
|
63
|
+
async getUsersAppSettings(_ref2) {
|
|
64
|
+
let {
|
|
65
|
+
userIds,
|
|
66
|
+
settingNames,
|
|
67
|
+
jwt
|
|
68
|
+
} = _ref2;
|
|
69
|
+
let retries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
70
|
+
if (!settingNames) {
|
|
71
|
+
return {};
|
|
72
|
+
} else {
|
|
73
|
+
let appSettings;
|
|
74
|
+
try {
|
|
75
|
+
let resignedToken = await this.authService.getResignedToken(jwt);
|
|
76
|
+
appSettings = await _superagent.default.post(`${this.identityServicesUrl}/users/app_settings/batch_get`).set('Authorization', resignedToken).set('x-client-name', _configuration.default.get('CLIENT_NAME_HEADER')).send({
|
|
77
|
+
userIds,
|
|
78
|
+
settingNames: settingNames
|
|
79
|
+
}).then(result => result.body);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (retries <= 3) {
|
|
82
|
+
_logger.default.error(`getUsersAppSettings Failed requesting Identity Services app settings for userIds ${userIds} retrying ${retries}`, error);
|
|
83
|
+
return await this.getUsersAppSettings({
|
|
84
|
+
userIds,
|
|
85
|
+
settingNames,
|
|
86
|
+
jwt
|
|
87
|
+
}, ++retries);
|
|
88
|
+
} else {
|
|
89
|
+
_logger.default.error(`getUsersAppSettings Failed requesting Identity Services app settings for userIds ${userIds} complete failure`, error);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
return appSettings;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.IdentityServicesClient = IdentityServicesClient;
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.InstagramApiClient = void 0;
|
|
7
|
+
var _superagent = _interopRequireDefault(require("superagent"));
|
|
8
|
+
var _logger = _interopRequireDefault(require("../../lib/logger.js"));
|
|
9
|
+
var _externalIdHelpers = require("../../lib/externalId.helpers.js");
|
|
10
|
+
var _assert = _interopRequireDefault(require("assert"));
|
|
11
|
+
var _credentialsApiClient = require("../http/credentialsApi.client.js");
|
|
12
|
+
var _loggerHelpers = require("../../lib/logger.helpers.js");
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
const INSTAGRAM_URL = 'https://graph.facebook.com';
|
|
15
|
+
class InstagramApiClient {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.credentialsAPI = new _credentialsApiClient.CredentialsApiClient();
|
|
18
|
+
}
|
|
19
|
+
async validate(accessToken, payload) {
|
|
20
|
+
const {
|
|
21
|
+
inReplyToId,
|
|
22
|
+
text
|
|
23
|
+
} = payload;
|
|
24
|
+
(0, _assert.default)(accessToken, 'AccessToken is required.');
|
|
25
|
+
(0, _assert.default)(inReplyToId, 'inReplyToId is required.');
|
|
26
|
+
(0, _assert.default)(text, 'text is required.');
|
|
27
|
+
}
|
|
28
|
+
async requestApi(apiUrl, accessToken, text, documentId, data) {
|
|
29
|
+
let discussionType = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 're';
|
|
30
|
+
let adCampaign = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : undefined;
|
|
31
|
+
let response = {};
|
|
32
|
+
const loggerChild = _logger.default.child({
|
|
33
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
34
|
+
});
|
|
35
|
+
try {
|
|
36
|
+
let queryStringArgs = {
|
|
37
|
+
access_token: accessToken
|
|
38
|
+
};
|
|
39
|
+
if (adCampaign) {
|
|
40
|
+
queryStringArgs.ad_id = adCampaign.adAccountId;
|
|
41
|
+
}
|
|
42
|
+
if (text) {
|
|
43
|
+
queryStringArgs[discussionType === 'dm' ? 'text' : 'message'] = text;
|
|
44
|
+
}
|
|
45
|
+
response = await _superagent.default.post(apiUrl).set('Accept', 'application/json').set('Content-Type', 'application/json').query(queryStringArgs).send(data);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
let errorText = '';
|
|
48
|
+
if (err && err.response && err.response.body && err.response.body.error) {
|
|
49
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
50
|
+
loggerChild.error(errorText);
|
|
51
|
+
} else {
|
|
52
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
53
|
+
loggerChild.error(errorText, err);
|
|
54
|
+
}
|
|
55
|
+
throw new Error(errorText);
|
|
56
|
+
}
|
|
57
|
+
if (response.status !== 200) {
|
|
58
|
+
loggerChild.error(`Failed to call instagram api for documentId ${documentId}`, {
|
|
59
|
+
response: JSON.stringify(response.body)
|
|
60
|
+
});
|
|
61
|
+
let error = new Error(`Failed to call instagram api for documentId ${documentId}`);
|
|
62
|
+
error.code = response.status;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
loggerChild.debug('Instagram Response status', response.status);
|
|
66
|
+
loggerChild.debug('Instagram Response body', JSON.stringify(response.body));
|
|
67
|
+
loggerChild.debug('Instagram Response body.id', response.body.id);
|
|
68
|
+
return response;
|
|
69
|
+
}
|
|
70
|
+
async getLikes(accessToken, payload) {
|
|
71
|
+
let response;
|
|
72
|
+
const {
|
|
73
|
+
externalId,
|
|
74
|
+
documentId
|
|
75
|
+
} = payload;
|
|
76
|
+
const loggerChild = _logger.default.child({
|
|
77
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
78
|
+
});
|
|
79
|
+
loggerChild.debug(`Starting to call instagram getPost api for documentId ${documentId}`, {
|
|
80
|
+
payload: JSON.stringify(payload)
|
|
81
|
+
});
|
|
82
|
+
try {
|
|
83
|
+
response = await this.getPost(`${INSTAGRAM_URL}/${(0, _externalIdHelpers.removePrefix)(externalId)}`, accessToken, 'like_count', documentId);
|
|
84
|
+
loggerChild.info(`Native Instagram API getPost Response for documentId ${documentId}`, {
|
|
85
|
+
status: response.status,
|
|
86
|
+
ok: response.ok
|
|
87
|
+
});
|
|
88
|
+
return response.body;
|
|
89
|
+
} catch (err) {
|
|
90
|
+
loggerChild.error('Error getLikes Instagram', {
|
|
91
|
+
payload: JSON.stringify(payload)
|
|
92
|
+
});
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async mentionRequestApi(apiUrl, accessToken, replayObject, text, documentId) {
|
|
97
|
+
let response = {};
|
|
98
|
+
const loggerChild = _logger.default.child({
|
|
99
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
100
|
+
});
|
|
101
|
+
try {
|
|
102
|
+
response = await _superagent.default.post(apiUrl).set('Accept', 'application/json').set('Content-Type', 'application/json').query({
|
|
103
|
+
access_token: accessToken
|
|
104
|
+
}).query(replayObject).query({
|
|
105
|
+
message: text
|
|
106
|
+
}).send();
|
|
107
|
+
} catch (err) {
|
|
108
|
+
let errorText = '';
|
|
109
|
+
if (err && err.response && err.response.body && err.response.body.error) {
|
|
110
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
111
|
+
loggerChild.error(errorText);
|
|
112
|
+
} else {
|
|
113
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
114
|
+
loggerChild.error(errorText, err);
|
|
115
|
+
}
|
|
116
|
+
throw new Error(errorText);
|
|
117
|
+
}
|
|
118
|
+
if (response.status !== 200) {
|
|
119
|
+
loggerChild.error(`Failed to call instagram api for documentId ${documentId}`, {
|
|
120
|
+
response: JSON.stringify(response.body)
|
|
121
|
+
});
|
|
122
|
+
let error = new Error(`Failed to call instagram api for documentId ${documentId}`);
|
|
123
|
+
error.code = response.status;
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
loggerChild.debug('Instagram Response status', response.status);
|
|
127
|
+
loggerChild.debug('Instagram Response body', {
|
|
128
|
+
response: JSON.stringify(response.body)
|
|
129
|
+
});
|
|
130
|
+
loggerChild.debug('Instagram Response body.id', response.body.id);
|
|
131
|
+
return response;
|
|
132
|
+
}
|
|
133
|
+
async hideApi(apiUrl, accessToken, hideStatus, documentId) {
|
|
134
|
+
let response = {};
|
|
135
|
+
const loggerChild = _logger.default.child({
|
|
136
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
response = await _superagent.default.post(apiUrl).set('Accept', 'application/json').set('Content-Type', 'application/json').query({
|
|
140
|
+
access_token: accessToken
|
|
141
|
+
}).query({
|
|
142
|
+
hide: hideStatus
|
|
143
|
+
}).send();
|
|
144
|
+
} catch (err) {
|
|
145
|
+
let errorText = '';
|
|
146
|
+
if (err && err.response && err.response.body) {
|
|
147
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
148
|
+
loggerChild.error(errorText, err.response.body.error);
|
|
149
|
+
} else {
|
|
150
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
151
|
+
loggerChild.error(errorText, err);
|
|
152
|
+
}
|
|
153
|
+
throw new Error(errorText);
|
|
154
|
+
}
|
|
155
|
+
if (response.status !== 200) {
|
|
156
|
+
loggerChild.error(`Failed to call instagram api for documentId ${documentId}`, {
|
|
157
|
+
response: JSON.stringify(response.body)
|
|
158
|
+
});
|
|
159
|
+
let error = new Error(`Failed to call instagram api for documentId ${documentId}`);
|
|
160
|
+
error.code = response.status;
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
loggerChild.debug('Instagram Response status', response.status);
|
|
164
|
+
loggerChild.debug('Instagram Response body', {
|
|
165
|
+
response: JSON.stringify(response.body)
|
|
166
|
+
});
|
|
167
|
+
loggerChild.debug('Instagram Response body.id', response.body.id);
|
|
168
|
+
return response;
|
|
169
|
+
}
|
|
170
|
+
async getPost(apiUrl, accessToken, fields, documentId) {
|
|
171
|
+
let response = {};
|
|
172
|
+
const loggerChild = _logger.default.child({
|
|
173
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
174
|
+
});
|
|
175
|
+
try {
|
|
176
|
+
response = await _superagent.default.get(apiUrl).set('Accept', 'application/json').set('Content-Type', 'application/json').query({
|
|
177
|
+
access_token: accessToken
|
|
178
|
+
}).query({
|
|
179
|
+
fields: fields
|
|
180
|
+
}).send();
|
|
181
|
+
} catch (err) {
|
|
182
|
+
let errorText = '';
|
|
183
|
+
if (err && err.response && err.response.body) {
|
|
184
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
185
|
+
loggerChild.error(errorText, err.response.body.error);
|
|
186
|
+
} else {
|
|
187
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
188
|
+
loggerChild.error(errorText, err);
|
|
189
|
+
}
|
|
190
|
+
throw new Error(errorText);
|
|
191
|
+
}
|
|
192
|
+
if (response.status !== 200) {
|
|
193
|
+
loggerChild.error(`Failed to call instagram api for documentId ${documentId}`, {
|
|
194
|
+
response: JSON.stringify(response.body)
|
|
195
|
+
});
|
|
196
|
+
let error = new Error(`Failed to call instagram api for documentId ${documentId}`);
|
|
197
|
+
error.code = response.status;
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
loggerChild.debug('Instagram Response status', response.status);
|
|
201
|
+
loggerChild.debug('Instagram Response body', {
|
|
202
|
+
response: JSON.stringify(response.body)
|
|
203
|
+
});
|
|
204
|
+
loggerChild.debug('Instagram Response body.id', response.body.id);
|
|
205
|
+
return response;
|
|
206
|
+
}
|
|
207
|
+
async comment(accessToken, payload) {
|
|
208
|
+
let response;
|
|
209
|
+
const {
|
|
210
|
+
inReplyToId,
|
|
211
|
+
text,
|
|
212
|
+
documentId,
|
|
213
|
+
isMention,
|
|
214
|
+
sourceId
|
|
215
|
+
} = payload;
|
|
216
|
+
const loggerChild = _logger.default.child({
|
|
217
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
218
|
+
});
|
|
219
|
+
loggerChild.debug(`Starting to call instagram comment api for documentId ${documentId}`, {
|
|
220
|
+
payload: JSON.stringify(payload)
|
|
221
|
+
});
|
|
222
|
+
this.validate(accessToken, payload);
|
|
223
|
+
if (isMention) {
|
|
224
|
+
response = await this.mentionRequestApi(`${INSTAGRAM_URL}/${sourceId}/mentions`, accessToken, {
|
|
225
|
+
media_id: (0, _externalIdHelpers.removePrefix)(inReplyToId)
|
|
226
|
+
}, text, documentId);
|
|
227
|
+
} else {
|
|
228
|
+
response = await this.requestApi(`${INSTAGRAM_URL}/${(0, _externalIdHelpers.removePrefix)(inReplyToId)}/comments`, accessToken, text, documentId, undefined, 'qt');
|
|
229
|
+
}
|
|
230
|
+
loggerChild.info(`Native Intagram API Publish Comment Response for documentId ${documentId}`, {
|
|
231
|
+
status: response.status,
|
|
232
|
+
ok: response.ok
|
|
233
|
+
});
|
|
234
|
+
return response.body;
|
|
235
|
+
}
|
|
236
|
+
async reply(accessToken, payload) {
|
|
237
|
+
let response;
|
|
238
|
+
const {
|
|
239
|
+
inReplyToId,
|
|
240
|
+
text,
|
|
241
|
+
documentId,
|
|
242
|
+
isMention,
|
|
243
|
+
sourceId,
|
|
244
|
+
threadId,
|
|
245
|
+
adCampaign
|
|
246
|
+
} = payload;
|
|
247
|
+
const loggerChild = _logger.default.child({
|
|
248
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
249
|
+
});
|
|
250
|
+
loggerChild.debug(`Starting to call instagram comment api for documentId ${documentId}`, {
|
|
251
|
+
payload: JSON.stringify(payload)
|
|
252
|
+
});
|
|
253
|
+
this.validate(accessToken, payload);
|
|
254
|
+
if (isMention) {
|
|
255
|
+
response = await this.mentionRequestApi(`${INSTAGRAM_URL}/${sourceId}/mentions`, accessToken, {
|
|
256
|
+
media_id: (0, _externalIdHelpers.removePrefix)(threadId),
|
|
257
|
+
comment_id: (0, _externalIdHelpers.removePrefix)(inReplyToId)
|
|
258
|
+
}, text, documentId);
|
|
259
|
+
} else {
|
|
260
|
+
response = await this.requestApi(`${INSTAGRAM_URL}/${(0, _externalIdHelpers.removePrefix)(inReplyToId)}/replies`, accessToken, text, documentId, undefined, 're', adCampaign);
|
|
261
|
+
}
|
|
262
|
+
loggerChild.info(`Native Instagram API Publish Reply Response for documentId ${documentId}`, {
|
|
263
|
+
status: response.status,
|
|
264
|
+
ok: response.ok
|
|
265
|
+
});
|
|
266
|
+
return response.body;
|
|
267
|
+
}
|
|
268
|
+
async publish(document) {
|
|
269
|
+
const {
|
|
270
|
+
documentId,
|
|
271
|
+
body: {
|
|
272
|
+
content: {
|
|
273
|
+
text
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
appData: {
|
|
277
|
+
isMention
|
|
278
|
+
},
|
|
279
|
+
metaData: {
|
|
280
|
+
discussionType,
|
|
281
|
+
source: {
|
|
282
|
+
id: sourceId
|
|
283
|
+
},
|
|
284
|
+
inReplyTo: {
|
|
285
|
+
id: inReplyToId,
|
|
286
|
+
profileId,
|
|
287
|
+
threadId
|
|
288
|
+
},
|
|
289
|
+
adCampaign
|
|
290
|
+
},
|
|
291
|
+
systemData: {
|
|
292
|
+
connectionsCredential: credentialId,
|
|
293
|
+
policies: {
|
|
294
|
+
storage: {
|
|
295
|
+
privateTo: companyId
|
|
296
|
+
} = {}
|
|
297
|
+
} = {}
|
|
298
|
+
} = {}
|
|
299
|
+
} = document;
|
|
300
|
+
let payload = {
|
|
301
|
+
documentId,
|
|
302
|
+
credentialId,
|
|
303
|
+
text,
|
|
304
|
+
companyId,
|
|
305
|
+
inReplyToId,
|
|
306
|
+
profileId,
|
|
307
|
+
isMention,
|
|
308
|
+
sourceId,
|
|
309
|
+
threadId,
|
|
310
|
+
adCampaign
|
|
311
|
+
};
|
|
312
|
+
const loggerChild = _logger.default.child({
|
|
313
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId,
|
|
314
|
+
[_loggerHelpers.MeltwaterAttributes.COMPANYID]: companyId,
|
|
315
|
+
[_loggerHelpers.MeltwaterAttributes.CREDENTIALID]: credentialId
|
|
316
|
+
});
|
|
317
|
+
let updatedDocument;
|
|
318
|
+
let apiResponse;
|
|
319
|
+
try {
|
|
320
|
+
const token = await this.credentialsAPI.getToken(payload.credentialId, payload.companyId);
|
|
321
|
+
loggerChild.debug(`finished fetching token for instagram for ${documentId} on company ${companyId} `, {
|
|
322
|
+
payload: JSON.stringify(payload)
|
|
323
|
+
});
|
|
324
|
+
switch (discussionType) {
|
|
325
|
+
case 'qt':
|
|
326
|
+
apiResponse = await this.comment(token.token, payload);
|
|
327
|
+
break;
|
|
328
|
+
case 're':
|
|
329
|
+
apiResponse = await this.reply(token.token, payload);
|
|
330
|
+
break;
|
|
331
|
+
case 'dm':
|
|
332
|
+
// get parent account
|
|
333
|
+
const parentCredentialId = await this.credentialsAPI.getCredentialIdByAccountId(token.parentAccountId, 'facebook');
|
|
334
|
+
if (!parentCredentialId) {
|
|
335
|
+
throw 'Parent account id not found';
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// get parent
|
|
339
|
+
const parentCredential = await this.credentialsAPI.getToken(parentCredentialId, payload.companyId);
|
|
340
|
+
if (!parentCredential) {
|
|
341
|
+
throw 'Parent account not found';
|
|
342
|
+
}
|
|
343
|
+
apiResponse = await this.privateMessage(parentCredential.token, payload);
|
|
344
|
+
break;
|
|
345
|
+
default:
|
|
346
|
+
throw new Error('Unsupported discussion type');
|
|
347
|
+
}
|
|
348
|
+
} catch (err) {
|
|
349
|
+
loggerChild.error(`${documentId} - exception details`, err);
|
|
350
|
+
}
|
|
351
|
+
return apiResponse;
|
|
352
|
+
}
|
|
353
|
+
async hide(accessToken, payload) {
|
|
354
|
+
let response;
|
|
355
|
+
const {
|
|
356
|
+
externalId,
|
|
357
|
+
documentId
|
|
358
|
+
} = payload;
|
|
359
|
+
const loggerChild = _logger.default.child({
|
|
360
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
361
|
+
});
|
|
362
|
+
loggerChild.debug(`Starting to call instagram hide api for documentId ${documentId}`, {
|
|
363
|
+
payload: JSON.stringify(payload)
|
|
364
|
+
});
|
|
365
|
+
response = await this.hideApi(`${INSTAGRAM_URL}/${(0, _externalIdHelpers.removePrefix)(externalId)}`, accessToken, true, documentId);
|
|
366
|
+
loggerChild.info(`Native Instagram API Hide Response for documentId ${documentId}`, {
|
|
367
|
+
status: response.status,
|
|
368
|
+
ok: response.ok
|
|
369
|
+
});
|
|
370
|
+
return response.body;
|
|
371
|
+
}
|
|
372
|
+
async unhide(accessToken, payload) {
|
|
373
|
+
let response;
|
|
374
|
+
const {
|
|
375
|
+
externalId,
|
|
376
|
+
documentId
|
|
377
|
+
} = payload;
|
|
378
|
+
const loggerChild = _logger.default.child({
|
|
379
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
380
|
+
});
|
|
381
|
+
loggerChild.debug(`Starting to call instagram unhide api for documentId ${documentId}`, {
|
|
382
|
+
payload: JSON.stringify(payload)
|
|
383
|
+
});
|
|
384
|
+
response = await this.hideApi(`${INSTAGRAM_URL}/${(0, _externalIdHelpers.removePrefix)(externalId)}`, accessToken, false, documentId);
|
|
385
|
+
loggerChild.info(`Native Instagram API Unhide Response for documentId ${documentId}`, {
|
|
386
|
+
status: response.status,
|
|
387
|
+
ok: response.ok
|
|
388
|
+
});
|
|
389
|
+
return response.body;
|
|
390
|
+
}
|
|
391
|
+
async privateMessage(accessToken, payload) {
|
|
392
|
+
let response;
|
|
393
|
+
const {
|
|
394
|
+
profileId,
|
|
395
|
+
text,
|
|
396
|
+
documentId,
|
|
397
|
+
isMention,
|
|
398
|
+
sourceId,
|
|
399
|
+
threadId
|
|
400
|
+
} = payload;
|
|
401
|
+
const loggerChild = _logger.default.child({
|
|
402
|
+
[_loggerHelpers.MeltwaterAttributes.DOCUMENTID]: documentId
|
|
403
|
+
});
|
|
404
|
+
loggerChild.debug(`Starting to call instagram private message api for documentId ${documentId}`, {
|
|
405
|
+
payload: JSON.stringify(payload)
|
|
406
|
+
});
|
|
407
|
+
this.validate(accessToken, payload);
|
|
408
|
+
response = await this.requestApi(`${INSTAGRAM_URL}/v10.0/me/messages`, accessToken,
|
|
409
|
+
// this sends in query string leaving out :shrug:
|
|
410
|
+
undefined, documentId, {
|
|
411
|
+
recipient: {
|
|
412
|
+
// not :100: this is right
|
|
413
|
+
id: (0, _externalIdHelpers.removePrefix)(profileId)
|
|
414
|
+
},
|
|
415
|
+
message: {
|
|
416
|
+
text
|
|
417
|
+
},
|
|
418
|
+
// this apparently allows up to 7 days after message 🤔
|
|
419
|
+
tag: 'HUMAN_AGENT'
|
|
420
|
+
}, 'dm');
|
|
421
|
+
loggerChild.info(`Native Instagram API Publish Private Message Response for documentId ${documentId}`, {
|
|
422
|
+
status: response.status,
|
|
423
|
+
ok: response.ok
|
|
424
|
+
});
|
|
425
|
+
return response.body;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
exports.InstagramApiClient = InstagramApiClient;
|