messaging-api-messenger
Advanced tools
Comparing version 0.6.9 to 0.7.0-alpha.1
@@ -1,35 +0,372 @@ | ||
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _warning = require('warning');var _warning2 = _interopRequireDefault(_warning); | ||
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;}; | ||
var _MessengerBatch = require('./MessengerBatch');var _MessengerBatch2 = _interopRequireDefault(_MessengerBatch);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} | ||
var _formData = require('form-data');var _formData2 = _interopRequireDefault(_formData); | ||
var _invariant = require('invariant');var _invariant2 = _interopRequireDefault(_invariant); | ||
var _isPlainObject = require('is-plain-object');var _isPlainObject2 = _interopRequireDefault(_isPlainObject);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} | ||
function createRequest(...args) { | ||
(0, _warning2.default)( | ||
false, | ||
'`Messenger.createRequest` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createRequest` instead.'); | ||
return _MessengerBatch2.default.createRequest(...args); | ||
function validateQuickReplies(quickReplies) { | ||
// quick_replies is limited to 11 | ||
(0, _invariant2.default)( | ||
Array.isArray(quickReplies) && quickReplies.length <= 11, | ||
'quick_replies is an array and limited to 11'); | ||
quickReplies.forEach(quickReply => { | ||
if (quickReply.content_type === 'text') { | ||
// title has a 20 character limit, after that it gets truncated | ||
(0, _invariant2.default)( | ||
quickReply.title.trim().length <= 20, | ||
'title of quick reply has a 20 character limit, after that it gets truncated'); | ||
// payload has a 1000 character limit | ||
(0, _invariant2.default)( | ||
quickReply.payload.length <= 1000, | ||
'payload of quick reply has a 1000 character limit'); | ||
} | ||
}); | ||
} | ||
function createMessage(...args) { | ||
(0, _warning2.default)( | ||
false, | ||
'`Messenger.createMessage` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createMessage` instead.'); | ||
function createMessage( | ||
msg, | ||
options = {}) | ||
{ | ||
const message = _extends({}, | ||
msg); | ||
return _MessengerBatch2.default.createMessage(...args); | ||
if ( | ||
options.quick_replies && | ||
Array.isArray(options.quick_replies) && | ||
options.quick_replies.length >= 1) | ||
{ | ||
validateQuickReplies(options.quick_replies); | ||
message.quick_replies = options.quick_replies; | ||
} | ||
return message; | ||
} | ||
function createText(...args) { | ||
(0, _warning2.default)( | ||
false, | ||
'`Messenger.createText` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createText` instead.'); | ||
function createText( | ||
text, | ||
options = {}) | ||
{ | ||
return createMessage({ text }, options); | ||
} | ||
return _MessengerBatch2.default.createText(...args); | ||
function createMessageFormData( | ||
payload, | ||
filedata, | ||
options = {}) | ||
{ | ||
const message = _extends({}, | ||
payload); | ||
if (options.quick_replies) { | ||
validateQuickReplies(options.quick_replies); | ||
message.quick_replies = options.quick_replies; | ||
} | ||
const formdata = new _formData2.default(); | ||
formdata.append('message', JSON.stringify(message)); | ||
formdata.append('filedata', filedata); | ||
return formdata; | ||
} | ||
function createAttachment( | ||
attachment, | ||
options = {}) | ||
{ | ||
return createMessage( | ||
{ | ||
attachment }, | ||
options); | ||
} | ||
function createAttachmentFormData(attachment, filedata, options) { | ||
return createMessageFormData( | ||
{ | ||
attachment }, | ||
// $FlowFixMe | ||
filedata, | ||
options); | ||
} | ||
function createAudio( | ||
audio, | ||
options = {}) | ||
{ | ||
const attachment = { | ||
type: 'audio', | ||
payload: {} }; | ||
if (typeof audio === 'string') { | ||
attachment.payload.url = audio; | ||
return createAttachment(attachment, options); | ||
} else if (audio && (0, _isPlainObject2.default)(audio)) { | ||
attachment.payload = audio; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, audio, options); | ||
} | ||
function createImage( | ||
image, | ||
options = {}) | ||
{ | ||
const attachment = { | ||
type: 'image', | ||
payload: {} }; | ||
if (typeof image === 'string') { | ||
attachment.payload.url = image; | ||
return createAttachment(attachment, options); | ||
} else if (image && (0, _isPlainObject2.default)(image)) { | ||
attachment.payload = image; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, image, options); | ||
} | ||
function createVideo( | ||
video, | ||
options = {}) | ||
{ | ||
const attachment = { | ||
type: 'video', | ||
payload: {} }; | ||
if (typeof video === 'string') { | ||
attachment.payload.url = video; | ||
return createAttachment(attachment, options); | ||
} else if (video && (0, _isPlainObject2.default)(video)) { | ||
attachment.payload = video; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, video, options); | ||
} | ||
function createFile( | ||
file, | ||
options = {}) | ||
{ | ||
const attachment = { | ||
type: 'file', | ||
payload: {} }; | ||
if (typeof file === 'string') { | ||
attachment.payload.url = file; | ||
return createAttachment(attachment, options); | ||
} else if (file && (0, _isPlainObject2.default)(file)) { | ||
attachment.payload = file; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, file, options); | ||
} | ||
function createTemplate( | ||
payload, | ||
options = {}) | ||
{ | ||
return createAttachment( | ||
{ | ||
type: 'template', | ||
payload }, | ||
options); | ||
} | ||
function createButtonTemplate( | ||
text, | ||
buttons, | ||
options = {}) | ||
{ | ||
return createTemplate( | ||
{ | ||
template_type: 'button', | ||
text, | ||
buttons }, | ||
options); | ||
} | ||
function createGenericTemplate( | ||
elements, | ||
options = | ||
{}) | ||
{ | ||
return createTemplate( | ||
{ | ||
template_type: 'generic', | ||
elements, | ||
image_aspect_ratio: options.image_aspect_ratio || 'horizontal' }, | ||
options); | ||
} | ||
function createListTemplate( | ||
elements, | ||
buttons, | ||
options = | ||
{}) | ||
{ | ||
return createTemplate( | ||
{ | ||
template_type: 'list', | ||
elements, | ||
buttons, | ||
top_element_style: options.top_element_style || 'large' }, | ||
options); | ||
} | ||
function createOpenGraphTemplate( | ||
elements, | ||
options = {}) | ||
{ | ||
return createTemplate( | ||
{ | ||
template_type: 'open_graph', | ||
elements }, | ||
options); | ||
} | ||
function createMediaTemplate( | ||
elements, | ||
options = {}) | ||
{ | ||
return createTemplate( | ||
{ | ||
template_type: 'media', | ||
elements }, | ||
options); | ||
} | ||
function createReceiptTemplate( | ||
attrs, | ||
options = {}) | ||
{ | ||
return createTemplate(_extends({ | ||
template_type: 'receipt' }, | ||
attrs), | ||
options); | ||
} | ||
function createAirlineBoardingPassTemplate( | ||
attrs, | ||
options = {}) | ||
{ | ||
return createTemplate(_extends({ | ||
template_type: 'airline_boardingpass' }, | ||
attrs), | ||
options); | ||
} | ||
function createAirlineCheckinTemplate( | ||
attrs, | ||
options = {}) | ||
{ | ||
return createTemplate(_extends({ | ||
template_type: 'airline_checkin' }, | ||
attrs), | ||
options); | ||
} | ||
function createAirlineItineraryTemplate( | ||
attrs, | ||
options = {}) | ||
{ | ||
return createTemplate(_extends({ | ||
template_type: 'airline_itinerary' }, | ||
attrs), | ||
options); | ||
} | ||
function createAirlineFlightUpdateTemplate( | ||
attrs, | ||
options = {}) | ||
{ | ||
return createTemplate(_extends({ | ||
template_type: 'airline_update' }, | ||
attrs), | ||
options); | ||
} | ||
const Messenger = { | ||
createRequest, | ||
createMessage, | ||
createText };exports.default = | ||
createText, | ||
createAttachment, | ||
createAudio, | ||
createImage, | ||
createVideo, | ||
createFile, | ||
createTemplate, | ||
createButtonTemplate, | ||
createGenericTemplate, | ||
createListTemplate, | ||
createOpenGraphTemplate, | ||
createMediaTemplate, | ||
createReceiptTemplate, | ||
createAirlineBoardingPassTemplate, | ||
createAirlineCheckinTemplate, | ||
createAirlineItineraryTemplate, | ||
createAirlineFlightUpdateTemplate };exports.default = | ||
Messenger; |
@@ -13,4 +13,5 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;}; | ||
var _isPlainObject = require('is-plain-object');var _isPlainObject2 = _interopRequireDefault(_isPlainObject); | ||
var _warning = require('warning');var _warning2 = _interopRequireDefault(_warning);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _objectWithoutProperties(obj, keys) {var target = {};for (var i in obj) {if (keys.indexOf(i) >= 0) continue;if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;target[i] = obj[i];}return target;} /* eslint-disable camelcase */ | ||
var _warning = require('warning');var _warning2 = _interopRequireDefault(_warning); | ||
var _Messenger = require('./Messenger');var _Messenger2 = _interopRequireDefault(_Messenger);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _objectWithoutProperties(obj, keys) {var target = {};for (var i in obj) {if (keys.indexOf(i) >= 0) continue;if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;target[i] = obj[i];}return target;} /* eslint-disable camelcase */ | ||
@@ -322,2 +323,5 @@ | ||
/** | ||
@@ -644,25 +648,2 @@ * Greeting Text | ||
/** | ||
@@ -751,34 +732,2 @@ * Content Types | ||
/** | ||
@@ -796,9 +745,2 @@ * Message Templates | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/button | ||
@@ -817,2 +759,3 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic | ||
@@ -822,3 +765,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic | ||
@@ -840,5 +782,5 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/list | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/list | ||
@@ -863,3 +805,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/open-graph | ||
@@ -877,5 +818,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/receipt | ||
@@ -893,5 +831,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/media | ||
@@ -909,5 +844,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/airline#boarding_pass | ||
@@ -925,5 +857,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/airline#check_in | ||
@@ -941,5 +870,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/airline#itinerary | ||
@@ -957,5 +883,2 @@ | ||
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/airline#update | ||
@@ -973,5 +896,2 @@ | ||
/** | ||
@@ -1140,2 +1060,3 @@ * Quick Replies | ||
/** | ||
@@ -1652,9 +1573,7 @@ * Label API | ||
if (menuItems.some(item => item.locale === 'default')) {return this.setMessengerProfile({ persistent_menu: menuItems }, options);} // menuItems is in type Array<MenuItem> | ||
return this.setMessengerProfile({ persistent_menu: [{ locale: 'default', composer_input_disabled: composerInputDisabled, call_to_actions: menuItems }] }, options);};this.deletePersistentMenu = (options = {}) => this.deleteMessengerProfile(['persistent_menu'], options);this.getGreeting = (options = {}) => this.getMessengerProfile(['greeting'], options).then(res => res[0] ? res[0].greeting : null);this.setGreeting = (greeting, options = {}) => {if (typeof greeting === 'string') {return this.setMessengerProfile({ greeting: [{ locale: 'default', text: greeting }] }, options);}return this.setMessengerProfile({ greeting }, options);};this.deleteGreeting = (options = {}) => this.deleteMessengerProfile(['greeting'], options);this.getWhitelistedDomains = (options = {}) => this.getMessengerProfile(['whitelisted_domains'], options).then(res => res[0] ? res[0].whitelisted_domains : null);this.setWhitelistedDomains = (domains, options = {}) => this.setMessengerProfile({ whitelisted_domains: domains }, options);this.deleteWhitelistedDomains = (options = {}) => this.deleteMessengerProfile(['whitelisted_domains'], options);this.getAccountLinkingURL = (options = {}) => this.getMessengerProfile(['account_linking_url'], options).then(res => res[0] ? res[0] : null);this.setAccountLinkingURL = (url, options = {}) => this.setMessengerProfile({ account_linking_url: url }, options);this.deleteAccountLinkingURL = (options = {}) => this.deleteMessengerProfile(['account_linking_url'], options);this.getPaymentSettings = (options = {}) => this.getMessengerProfile(['payment_settings'], options).then(res => res[0] ? res[0] : null);this.setPaymentPrivacyPolicyURL = (url, options = {}) => this.setMessengerProfile({ payment_settings: { privacy_url: url } }, options);this.setPaymentPublicKey = (key, options = {}) => this.setMessengerProfile({ payment_settings: { public_key: key } }, options);this.setPaymentTestUsers = (users, options = {}) => this.setMessengerProfile({ payment_settings: { test_users: users } }, options);this.deletePaymentSettings = (options = {}) => this.deleteMessengerProfile(['payment_settings'], options);this.getTargetAudience = (options = {}) => this.getMessengerProfile(['target_audience'], options).then(res => res[0] ? res[0] : null);this.setTargetAudience = (type, whitelist = [], blacklist = [], options = {}) => this.setMessengerProfile({ target_audience: { audience_type: type, countries: { whitelist, blacklist } } }, options);this.deleteTargetAudience = (options = {}) => this.deleteMessengerProfile(['target_audience'], options);this.getHomeURL = (options = {}) => this.getMessengerProfile(['home_url'], options).then(res => res[0] ? res[0] : null);this.setHomeURL = (url, { webview_share_button, in_test }, options = {}) => this.setMessengerProfile({ home_url: { url, webview_height_ratio: 'tall', in_test, webview_share_button } }, options);this.deleteHomeURL = (options = {}) => this.deleteMessengerProfile(['home_url'], options);this.getMessageTags = ({ access_token: customAccessToken } = {}) => this._axios.get(`/page_message_tags?access_token=${customAccessToken || this._accessToken}`).then(res => res.data.data, handleError);this.sendRawBody = body => {const customAccessToken = body.access_token;return this._axios.post(`/me/messages?access_token=${customAccessToken || this._accessToken}`, body).then(res => res.data, handleError);};this.sendMessage = (idOrRecipient, message, options = {}) => {const recipient = typeof idOrRecipient === 'string' ? { id: idOrRecipient } : idOrRecipient;let messageType = 'UPDATE';if (options.messaging_type) {messageType = options.messaging_type;} else if (options.tag) {messageType = 'MESSAGE_TAG';}const quickReplies = options.quick_replies,otherOptions = _objectWithoutProperties(options, ['quick_replies']);if (quickReplies && Array.isArray(quickReplies) && quickReplies.length >= 1) {validateQuickReplies(quickReplies);message.quick_replies = quickReplies; // eslint-disable-line no-param-reassign | ||
}return this.sendRawBody(_extends({ messaging_type: messageType, recipient, message }, otherOptions));};this.sendMessageFormData = (recipient, message, filedata, options = {}) => {const form = new _formData2.default();const recipientObject = typeof recipient === 'string' ? { id: recipient } : recipient;let messageType = 'UPDATE';if (options.messaging_type) {messageType = options.messaging_type;} else if (options.tag) {messageType = 'MESSAGE_TAG';}if (options.quick_replies && Array.isArray(options.quick_replies) && options.quick_replies.length >= 1) {validateQuickReplies(options.quick_replies);message.quick_replies = options.quick_replies; // eslint-disable-line no-param-reassign | ||
}form.append('messaging_type', messageType);form.append('recipient', JSON.stringify(recipientObject));form.append('message', JSON.stringify(message));form.append('filedata', filedata);return this._axios.post(`/me/messages?access_token=${options.access_token || this._accessToken}`, form, { headers: form.getHeaders() }).then(res => res.data, handleError);};this.sendAttachment = (recipient, attachment, options) => this.sendMessage(recipient, { attachment }, options);this.sendAttachmentFormData = (recipient, attachment, filedata, option) => this.sendMessageFormData(recipient, { attachment }, filedata, option);this.sendText = (recipient, text, options) => this.sendMessage(recipient, { text }, options);this.sendAudio = (recipient, audio, options) => {const attachment = { type: 'audio', payload: {} };if (typeof audio === 'string') {attachment.payload.url = audio;return this.sendAttachment(recipient, attachment, options);} else if (audio && (0, _isPlainObject2.default)(audio)) {attachment.payload = audio;return this.sendAttachment(recipient, attachment, options);} // $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, audio, options);};this.sendImage = (recipient, image, options) => {const attachment = { type: 'image', payload: {} };if (typeof image === 'string') {attachment.payload.url = image;return this.sendAttachment(recipient, attachment, options);} else if (image && (0, _isPlainObject2.default)(image)) {attachment.payload = image;return this.sendAttachment(recipient, attachment, options);} // $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, image, options);};this.sendVideo = (recipient, video, options) => {const attachment = { type: 'video', payload: {} };if (typeof video === 'string') {attachment.payload.url = video;return this.sendAttachment(recipient, attachment, options);} else if (video && (0, _isPlainObject2.default)(video)) {attachment.payload = video;return this.sendAttachment(recipient, attachment, options);} // $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, video, options);};this.sendFile = (recipient, file, options) => {const attachment = { type: 'file', payload: {} };if (typeof file === 'string') {attachment.payload.url = file;return this.sendAttachment(recipient, attachment, options);} else if (file && (0, _isPlainObject2.default)(file)) {attachment.payload = file;return this.sendAttachment(recipient, attachment, options);} // $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, file, options);};this.sendTemplate = (recipient, payload, options) => this.sendAttachment(recipient, { type: 'template', payload }, options);this.sendButtonTemplate = (recipient, text, buttons, options) => this.sendTemplate(recipient, { template_type: 'button', text, buttons }, options);this.sendGenericTemplate = (recipient, elements, options = {}) => this.sendTemplate(recipient, { template_type: 'generic', elements, image_aspect_ratio: options.image_aspect_ratio || 'horizontal' }, (0, _lodash2.default)(options, ['image_aspect_ratio']));this.sendListTemplate = (recipient, elements, buttons, options = {}) => this.sendTemplate(recipient, { template_type: 'list', elements, buttons, top_element_style: options.top_element_style || 'large' }, (0, _lodash2.default)(options, ['top_element_style']));this.sendOpenGraphTemplate = (recipient, elements, options) => this.sendTemplate(recipient, { template_type: 'open_graph', elements }, options);this.sendReceiptTemplate = (recipient, attrs, options) => this.sendTemplate(recipient, _extends({ template_type: 'receipt' }, attrs), options);this.sendMediaTemplate = (recipient, elements, options) => this.sendTemplate(recipient, { template_type: 'media', elements }, options);this.sendAirlineBoardingPassTemplate = (recipient, attrs, options) => this.sendTemplate(recipient, _extends({ template_type: 'airline_boardingpass' }, attrs), options);this.sendAirlineCheckinTemplate = (recipient, attrs, options) => this.sendTemplate(recipient, _extends({ template_type: 'airline_checkin' }, attrs), options);this.sendAirlineItineraryTemplate = (recipient, attrs, options) => this.sendTemplate(recipient, _extends({ template_type: 'airline_itinerary' }, attrs), options);this.sendAirlineFlightUpdateTemplate = (recipient, attrs, options) => this.sendTemplate(recipient, _extends({ template_type: 'airline_update' }, attrs), options);this.sendQuickReplies = (recipient, textOrAttachment, quickReplies, options) => {(0, _warning2.default)(false, '`sendQuickReplies` is deprecated. Use send message methods with `options.quick_replies` instead.');validateQuickReplies(quickReplies);return this.sendMessage(recipient, _extends({}, textOrAttachment, { quick_replies: quickReplies }), options);};this.sendSenderAction = (idOrRecipient, action, { access_token: customAccessToken } = {}) => {const recipient = typeof idOrRecipient === 'string' ? { id: idOrRecipient } : idOrRecipient;return this.sendRawBody({ recipient, sender_action: action, access_token: customAccessToken });};this.markSeen = (recipient, options = {}) => this.sendSenderAction(recipient, 'mark_seen', options);this.typingOn = (recipient, options = {}) => this.sendSenderAction(recipient, 'typing_on', options);this.typingOff = (recipient, options = {}) => this.sendSenderAction(recipient, 'typing_off', options);this.sendBatch = (batch, { access_token: customAccessToken } = {}) => {(0, _invariant2.default)(batch.length <= 50, 'limit the number of requests which can be in a batch to 50');const bodyEncodedbatch = batch.map(item => {if (item.body) {return _extends({}, item, { body: Object.keys(item.body).map(key => {// $FlowFixMe item.body should not possible as undefined. | ||
return this.setMessengerProfile({ persistent_menu: [{ locale: 'default', composer_input_disabled: composerInputDisabled, call_to_actions: menuItems }] }, options);};this.deletePersistentMenu = (options = {}) => this.deleteMessengerProfile(['persistent_menu'], options);this.getGreeting = (options = {}) => this.getMessengerProfile(['greeting'], options).then(res => res[0] ? res[0].greeting : null);this.setGreeting = (greeting, options = {}) => {if (typeof greeting === 'string') {return this.setMessengerProfile({ greeting: [{ locale: 'default', text: greeting }] }, options);}return this.setMessengerProfile({ greeting }, options);};this.deleteGreeting = (options = {}) => this.deleteMessengerProfile(['greeting'], options);this.getWhitelistedDomains = (options = {}) => this.getMessengerProfile(['whitelisted_domains'], options).then(res => res[0] ? res[0].whitelisted_domains : null);this.setWhitelistedDomains = (domains, options = {}) => this.setMessengerProfile({ whitelisted_domains: domains }, options);this.deleteWhitelistedDomains = (options = {}) => this.deleteMessengerProfile(['whitelisted_domains'], options);this.getAccountLinkingURL = (options = {}) => this.getMessengerProfile(['account_linking_url'], options).then(res => res[0] ? res[0] : null);this.setAccountLinkingURL = (url, options = {}) => this.setMessengerProfile({ account_linking_url: url }, options);this.deleteAccountLinkingURL = (options = {}) => this.deleteMessengerProfile(['account_linking_url'], options);this.getPaymentSettings = (options = {}) => this.getMessengerProfile(['payment_settings'], options).then(res => res[0] ? res[0] : null);this.setPaymentPrivacyPolicyURL = (url, options = {}) => this.setMessengerProfile({ payment_settings: { privacy_url: url } }, options);this.setPaymentPublicKey = (key, options = {}) => this.setMessengerProfile({ payment_settings: { public_key: key } }, options);this.setPaymentTestUsers = (users, options = {}) => this.setMessengerProfile({ payment_settings: { test_users: users } }, options);this.deletePaymentSettings = (options = {}) => this.deleteMessengerProfile(['payment_settings'], options);this.getTargetAudience = (options = {}) => this.getMessengerProfile(['target_audience'], options).then(res => res[0] ? res[0] : null);this.setTargetAudience = (type, whitelist = [], blacklist = [], options = {}) => this.setMessengerProfile({ target_audience: { audience_type: type, countries: { whitelist, blacklist } } }, options);this.deleteTargetAudience = (options = {}) => this.deleteMessengerProfile(['target_audience'], options);this.getHomeURL = (options = {}) => this.getMessengerProfile(['home_url'], options).then(res => res[0] ? res[0] : null);this.setHomeURL = (url, { webview_share_button, in_test }, options = {}) => this.setMessengerProfile({ home_url: { url, webview_height_ratio: 'tall', in_test, webview_share_button } }, options);this.deleteHomeURL = (options = {}) => this.deleteMessengerProfile(['home_url'], options);this.getMessageTags = ({ access_token: customAccessToken } = {}) => this._axios.get(`/page_message_tags?access_token=${customAccessToken || this._accessToken}`).then(res => res.data.data, handleError);this.sendRawBody = body => {const customAccessToken = body.access_token;return this._axios.post(`/me/messages?access_token=${customAccessToken || this._accessToken}`, body).then(res => res.data, handleError);};this.sendMessage = (idOrRecipient, message, options = {}) => {const recipient = typeof idOrRecipient === 'string' ? { id: idOrRecipient } : idOrRecipient;let messageType = 'UPDATE';if (options.messaging_type) {messageType = options.messaging_type;} else if (options.tag) {messageType = 'MESSAGE_TAG';}return this.sendRawBody(_extends({ messaging_type: messageType, recipient, message: _Messenger2.default.createMessage(message, options) }, (0, _lodash2.default)(options, 'quick_replies')));};this.sendMessageFormData = (recipient, formdata, options = {}) => {const recipientObject = typeof recipient === 'string' ? { id: recipient } : recipient;let messageType = 'UPDATE';if (options.messaging_type) {messageType = options.messaging_type;} else if (options.tag) {messageType = 'MESSAGE_TAG';}formdata.append('messaging_type', messageType);formdata.append('recipient', JSON.stringify(recipientObject));return this._axios.post(`/me/messages?access_token=${options.access_token || this._accessToken}`, formdata, { headers: formdata.getHeaders() }).then(res => res.data, handleError);};this.sendAttachment = (recipient, attachment, options) => this.sendMessage(recipient, _Messenger2.default.createAttachment(attachment), options);this.sendText = (recipient, text, options) => this.sendMessage(recipient, _Messenger2.default.createText(text), options);this.sendAudio = (recipient, audio, options) => {const message = _Messenger2.default.createAudio(audio, options);if (message && (0, _isPlainObject2.default)(message)) {return this.sendMessage(recipient, message, options);} // $FlowFixMe | ||
return this.sendMessageFormData(recipient, message, options);};this.sendImage = (recipient, image, options) => {const message = _Messenger2.default.createImage(image, options);if (message && (0, _isPlainObject2.default)(message)) {return this.sendMessage(recipient, message, options);} // $FlowFixMe | ||
return this.sendMessageFormData(recipient, message, options);};this.sendVideo = (recipient, video, options) => {const message = _Messenger2.default.createVideo(video, options);if (message && (0, _isPlainObject2.default)(message)) {return this.sendMessage(recipient, message, options);} // $FlowFixMe | ||
return this.sendMessageFormData(recipient, message, options);};this.sendFile = (recipient, file, options) => {const message = _Messenger2.default.createFile(file, options);if (message && (0, _isPlainObject2.default)(message)) {return this.sendMessage(recipient, message, options);} // $FlowFixMe | ||
return this.sendMessageFormData(recipient, message, options);};this.sendTemplate = (recipient, payload, options) => this.sendMessage(recipient, _Messenger2.default.createTemplate(payload), options);this.sendButtonTemplate = (recipient, text, buttons, options) => this.sendMessage(recipient, _Messenger2.default.createButtonTemplate(text, buttons), options);this.sendGenericTemplate = (recipient, elements, _ref2 = {}) => {var _ref2$image_aspect_ra = _ref2.image_aspect_ratio;let image_aspect_ratio = _ref2$image_aspect_ra === undefined ? 'horizontal' : _ref2$image_aspect_ra,options = _objectWithoutProperties(_ref2, ['image_aspect_ratio']);return this.sendMessage(recipient, _Messenger2.default.createGenericTemplate(elements, { image_aspect_ratio }), options);};this.sendListTemplate = (recipient, elements, buttons, _ref3 = {}) => {var _ref3$top_element_sty = _ref3.top_element_style;let top_element_style = _ref3$top_element_sty === undefined ? 'large' : _ref3$top_element_sty,options = _objectWithoutProperties(_ref3, ['top_element_style']);return this.sendMessage(recipient, _Messenger2.default.createListTemplate(elements, buttons, { top_element_style }), options);};this.sendOpenGraphTemplate = (recipient, elements, options) => this.sendMessage(recipient, _Messenger2.default.createOpenGraphTemplate(elements), options);this.sendReceiptTemplate = (recipient, attrs, options) => this.sendMessage(recipient, _Messenger2.default.createReceiptTemplate(attrs), options);this.sendMediaTemplate = (recipient, elements, options) => this.sendMessage(recipient, _Messenger2.default.createMediaTemplate(elements), options);this.sendAirlineBoardingPassTemplate = (recipient, attrs, options) => this.sendMessage(recipient, _Messenger2.default.createAirlineBoardingPassTemplate(attrs), options);this.sendAirlineCheckinTemplate = (recipient, attrs, options) => this.sendMessage(recipient, _Messenger2.default.createAirlineCheckinTemplate(attrs), options);this.sendAirlineItineraryTemplate = (recipient, attrs, options) => this.sendMessage(recipient, _Messenger2.default.createAirlineItineraryTemplate(attrs), options);this.sendAirlineFlightUpdateTemplate = (recipient, attrs, options) => this.sendMessage(recipient, _Messenger2.default.createAirlineFlightUpdateTemplate(attrs), options);this.sendQuickReplies = (recipient, textOrAttachment, quickReplies, options) => {(0, _warning2.default)(false, '`sendQuickReplies` is deprecated. Use send message methods with `options.quick_replies` instead.');validateQuickReplies(quickReplies);return this.sendMessage(recipient, _extends({}, textOrAttachment, { quick_replies: quickReplies }), options);};this.sendSenderAction = (idOrRecipient, action, { access_token: customAccessToken } = {}) => {const recipient = typeof idOrRecipient === 'string' ? { id: idOrRecipient } : idOrRecipient;return this.sendRawBody({ recipient, sender_action: action, access_token: customAccessToken });};this.markSeen = (recipient, options = {}) => this.sendSenderAction(recipient, 'mark_seen', options);this.typingOn = (recipient, options = {}) => this.sendSenderAction(recipient, 'typing_on', options);this.typingOff = (recipient, options = {}) => this.sendSenderAction(recipient, 'typing_off', options);this.sendBatch = (batch, { access_token: customAccessToken } = {}) => {(0, _invariant2.default)(batch.length <= 50, 'limit the number of requests which can be in a batch to 50');const bodyEncodedbatch = batch.map(item => {if (item.body) {return _extends({}, item, { body: Object.keys(item.body).map(key => {// $FlowFixMe item.body should not possible as undefined. | ||
const val = item.body[key];return `${encodeURIComponent(key)}=${encodeURIComponent(typeof val === 'object' ? JSON.stringify(val) : val)}`;}).join('&') });}return item;});return _axios2.default.post('https://graph.facebook.com/', { access_token: customAccessToken || this._accessToken, batch: bodyEncodedbatch }).then(res => res.data);};this.createMessageCreative = (messages = [], { access_token: customAccessToken } = {}) => this._axios.post(`/me/message_creatives?access_token=${customAccessToken || this._accessToken}`, { messages }).then(res => res.data, handleError);this.sendBroadcastMessage = (messageCreativeId, options = {}) => this._axios.post(`/me/broadcast_messages?access_token=${options.access_token || this._accessToken}`, _extends({ message_creative_id: messageCreativeId }, options)).then(res => res.data, handleError);this.sendSponsoredMessage = (adAccountId, message) => this._axios.post(`/act_${adAccountId}/sponsored_message_ads?access_token=${this._accessToken}`, message).then(res => res.data, handleError);this.createLabel = (name, { access_token: customAccessToken } = {}) => this._axios.post(`/me/custom_labels?access_token=${customAccessToken || this._accessToken}`, { name }).then(res => res.data, handleError);this.associateLabel = (userId, labelId, { access_token: customAccessToken } = {}) => this._axios.post(`/${labelId}/label?access_token=${customAccessToken || this._accessToken}`, { user: userId }).then(res => res.data, handleError);this.dissociateLabel = (userId, labelId, { access_token: customAccessToken } = {}) => this._axios.delete(`/${labelId}/label?access_token=${customAccessToken || this._accessToken}`, { data: { user: userId } }).then(res => res.data, handleError);this.getAssociatedLabels = (userId, { access_token: customAccessToken } = {}) => this._axios.get(`/${userId}/custom_labels?access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError);this.getLabelDetails = (labelId, options = {}) => {const fields = options.fields ? options.fields.join(',') : 'name';return this._axios.get(`/${labelId}?fields=${fields}&access_token=${options.access_token || this._accessToken}`).then(res => res.data, handleError);};this.getLabelList = (options = {}) => {const fields = options.fields ? options.fields.join(',') : 'name';return this._axios.get(`/me/custom_labels?fields=${fields}&access_token=${options.access_token || this._accessToken}`).then(res => res.data, handleError);};this.deleteLabel = (labelId, { access_token: customAccessToken } = {}) => this._axios.delete(`/${labelId}?access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError);this.startReachEstimation = (customLabelId, { access_token: customAccessToken } = {}) => this._axios.post(`/broadcast_reach_estimations?access_token=${customAccessToken || this._accessToken}`, { custom_label_id: customLabelId }).then(res => res.data, handleError);this.getReachEstimate = (reachEstimationId, { access_token: customAccessToken } = {}) => this._axios.post(`/${reachEstimationId}?access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError);this.getBroadcastMessagesSent = (broadcastId, { access_token: customAccessToken } = {}) => this._axios.post(`/${broadcastId}/insights/messages_sent?access_token=${customAccessToken || this._accessToken}`).then(res => res.data.data, handleError);this.uploadAttachment = (type, attachment, options = {}) => {const args = [];const isReusable = options.is_reusable || false;if (typeof attachment === 'string') {args.push({ message: { attachment: { type, payload: { url: attachment, is_reusable: isReusable } } } });} else {const form = new _formData2.default();form.append('message', JSON.stringify({ attachment: { type, payload: { is_reusable: isReusable } } }));form.append('filedata', attachment, (0, _lodash2.default)(options, ['is_reusable']));args.push(form, { headers: form.getHeaders() });}return this._axios.post(`/me/message_attachments?access_token=${options.access_token || this._accessToken}`, ...args).then(res => res.data, handleError);};this.uploadAudio = (attachment, options) => this.uploadAttachment('audio', attachment, options);this.uploadImage = (attachment, options) => this.uploadAttachment('image', attachment, options);this.uploadVideo = (attachment, options) => this.uploadAttachment('video', attachment, options);this.uploadFile = (attachment, options) => this.uploadAttachment('file', attachment, options);this.generateMessengerCode = (options = {}) => this._axios.post(`/me/messenger_codes?access_token=${options.access_token || this._accessToken}`, _extends({ type: 'standard' }, options)).then(res => res.data, handleError);this.passThreadControl = (recipientId, targetAppId, metadata, { access_token: customAccessToken } = {}) => this._axios.post(`/me/pass_thread_control?access_token=${customAccessToken || this._accessToken}`, { recipient: { id: recipientId }, target_app_id: targetAppId, metadata }).then(res => res.data, handleError);this.passThreadControlToPageInbox = (recipientId, metadata, options = {}) => this.passThreadControl(recipientId, 263902037430900, metadata, options);this.takeThreadControl = (recipientId, metadata, { access_token: customAccessToken } = {}) => this._axios.post(`/me/take_thread_control?access_token=${customAccessToken || this._accessToken}`, { recipient: { id: recipientId }, metadata }).then(res => res.data, handleError);this.getSecondaryReceivers = ({ access_token: customAccessToken } = {}) => this._axios.get(`/me/secondary_receivers?fields=id,name&access_token=${customAccessToken || this._accessToken}`).then(res => res.data.data, handleError);this.getInsights = (metrics, options = {}) => this._axios.get(`/me/insights/?${_querystring2.default.stringify(_extends({ metric: metrics.join(','), access_token: options.access_token || this._accessToken }, options))}`).then(res => res.data.data, handleError);this.getDailyUniqueActiveThreadCounts = (options = {}) => this.getInsights(['page_messages_active_threads_unique'], options);this.getBlockedConversations = (options = {}) => this.getInsights(['page_messages_blocked_conversations_unique'], options);this.getReportedConversations = (options = {}) => this.getInsights(['page_messages_reported_conversations_unique'], options);this.getReportedConversationsByReportType = (options = {}) => this.getInsights(['page_messages_blocked_conversations_unique'], options);this.getDailyUniqueConversationCounts = () => {(0, _warning2.default)(false, 'page_messages_feedback_by_action_unique is deprecated as of November 7, 2017.\nThis metric will be removed in Graph API v2.12.');return this.getInsights(['page_messages_feedback_by_action_unique']);};this.setNLPConfigs = (config = {}, { access_token: customAccessToken } = {}) => this._axios.post(`/me/nlp_configs?${_querystring2.default.stringify(config)}`, { access_token: customAccessToken || this._accessToken }).then(res => res.data, handleError);this.enableNLP = (options = {}) => this.setNLPConfigs({ nlp_enabled: true }, options);this.disableNLP = (options = {}) => this.setNLPConfigs({ nlp_enabled: false }, options);this.logCustomEvents = ({ app_id, appId, page_id, pageId, page_scoped_user_id, userId, events, access_token: customAccessToken }) => {// FIXME: remove in v0.7 | ||
@@ -1665,2 +1584,4 @@ (0, _warning2.default)(!appId, '`appId` is deprecated. Use `app_id` instead.');(0, _warning2.default)(!pageId, '`pageId` is deprecated. Use `page_id` instead.');(0, _warning2.default)(!userId, '`userId` is deprecated. Use `page_scoped_user_id` instead.'); /* eslint-disable no-param-reassign */app_id = app_id || appId;page_id = page_id || pageId;page_scoped_user_id = page_scoped_user_id || userId; /* eslint-enable no-param-reassign */return this._axios.post(`/${app_id}/activities?access_token=${customAccessToken || this._accessToken}`, { event: 'CUSTOM_APP_EVENTS', custom_events: JSON.stringify(events), advertiser_tracking_enabled: 0, application_tracking_enabled: 0, extinfo: JSON.stringify(['mb1']), page_id, page_scoped_user_id }).then(res => res.data, handleError);};this.getUserField = ({ field, user_id, app_secret, app, page, access_token: customAccessToken }) => {const accessToken = customAccessToken || this._accessToken; // $appsecret_proof= hash_hmac('sha256', $access_token, $app_secret); | ||
this.getUserField({ | ||
@@ -1667,0 +1588,0 @@ field: 'ids_for_pages', |
{ | ||
"name": "messaging-api-messenger", | ||
"description": "Messaging API client for Messenger", | ||
"version": "0.6.9", | ||
"version": "0.7.0-alpha.1", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=6" |
@@ -1,35 +0,372 @@ | ||
import warning from 'warning'; | ||
/* @flow */ | ||
import MessengerBatch from './MessengerBatch'; | ||
import FormData from 'form-data'; | ||
import invariant from 'invariant'; | ||
import isPlainObject from 'is-plain-object'; | ||
function createRequest(...args) { | ||
warning( | ||
false, | ||
'`Messenger.createRequest` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createRequest` instead.' | ||
import type { | ||
AttachmentPayload, | ||
Attachment, | ||
FileData, | ||
Message, | ||
TemplateButton, | ||
TemplateElement, | ||
QuickReply, | ||
OpenGraphElement, | ||
MediaElement, | ||
ReceiptAttributes, | ||
AirlineBoardingPassAttributes, | ||
AirlineCheckinAttributes, | ||
AirlineItineraryAttributes, | ||
AirlineFlightUpdateAttributes, | ||
} from './MessengerTypes'; | ||
function validateQuickReplies(quickReplies: Array<QuickReply>): void { | ||
// quick_replies is limited to 11 | ||
invariant( | ||
Array.isArray(quickReplies) && quickReplies.length <= 11, | ||
'quick_replies is an array and limited to 11' | ||
); | ||
return MessengerBatch.createRequest(...args); | ||
quickReplies.forEach(quickReply => { | ||
if (quickReply.content_type === 'text') { | ||
// title has a 20 character limit, after that it gets truncated | ||
invariant( | ||
(quickReply.title: any).trim().length <= 20, | ||
'title of quick reply has a 20 character limit, after that it gets truncated' | ||
); | ||
// payload has a 1000 character limit | ||
invariant( | ||
(quickReply.payload: any).length <= 1000, | ||
'payload of quick reply has a 1000 character limit' | ||
); | ||
} | ||
}); | ||
} | ||
function createMessage(...args) { | ||
warning( | ||
false, | ||
'`Messenger.createMessage` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createMessage` instead.' | ||
function createMessage( | ||
msg: Message, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
): Message { | ||
const message = { | ||
...msg, | ||
}; | ||
if ( | ||
options.quick_replies && | ||
Array.isArray(options.quick_replies) && | ||
options.quick_replies.length >= 1 | ||
) { | ||
validateQuickReplies(options.quick_replies); | ||
message.quick_replies = options.quick_replies; | ||
} | ||
return message; | ||
} | ||
function createText( | ||
text: string, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
): Message { | ||
return createMessage({ text }, options); | ||
} | ||
function createMessageFormData( | ||
payload: AttachmentPayload, | ||
filedata: FileData, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
const message = { | ||
...payload, | ||
}; | ||
if (options.quick_replies) { | ||
validateQuickReplies(options.quick_replies); | ||
message.quick_replies = options.quick_replies; | ||
} | ||
const formdata = new FormData(); | ||
formdata.append('message', JSON.stringify(message)); | ||
formdata.append('filedata', filedata); | ||
return formdata; | ||
} | ||
function createAttachment( | ||
attachment: Attachment, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createMessage( | ||
{ | ||
attachment, | ||
}, | ||
options | ||
); | ||
return MessengerBatch.createMessage(...args); | ||
} | ||
function createText(...args) { | ||
warning( | ||
false, | ||
'`Messenger.createText` will breaking and become part of message creation API in v0.7. For batch usage, use `MessengerBatch.createText` instead.' | ||
function createAttachmentFormData(attachment, filedata, options) { | ||
return createMessageFormData( | ||
{ | ||
attachment, | ||
}, | ||
// $FlowFixMe | ||
filedata, | ||
options | ||
); | ||
return MessengerBatch.createText(...args); | ||
} | ||
function createAudio( | ||
audio: string | FileData | AttachmentPayload, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
const attachment = { | ||
type: 'audio', | ||
payload: {}, | ||
}; | ||
if (typeof audio === 'string') { | ||
attachment.payload.url = audio; | ||
return createAttachment(attachment, options); | ||
} else if (audio && isPlainObject(audio)) { | ||
attachment.payload = audio; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, audio, options); | ||
} | ||
function createImage( | ||
image: string | FileData | AttachmentPayload, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
const attachment = { | ||
type: 'image', | ||
payload: {}, | ||
}; | ||
if (typeof image === 'string') { | ||
attachment.payload.url = image; | ||
return createAttachment(attachment, options); | ||
} else if (image && isPlainObject(image)) { | ||
attachment.payload = image; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, image, options); | ||
} | ||
function createVideo( | ||
video: string | FileData | AttachmentPayload, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
const attachment = { | ||
type: 'video', | ||
payload: {}, | ||
}; | ||
if (typeof video === 'string') { | ||
attachment.payload.url = video; | ||
return createAttachment(attachment, options); | ||
} else if (video && isPlainObject(video)) { | ||
attachment.payload = video; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, video, options); | ||
} | ||
function createFile( | ||
file: string | FileData | AttachmentPayload, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
const attachment = { | ||
type: 'file', | ||
payload: {}, | ||
}; | ||
if (typeof file === 'string') { | ||
attachment.payload.url = file; | ||
return createAttachment(attachment, options); | ||
} else if (file && isPlainObject(file)) { | ||
attachment.payload = file; | ||
return createAttachment(attachment, options); | ||
} | ||
return createAttachmentFormData(attachment, file, options); | ||
} | ||
function createTemplate( | ||
payload: AttachmentPayload, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createAttachment( | ||
{ | ||
type: 'template', | ||
payload, | ||
}, | ||
options | ||
); | ||
} | ||
function createButtonTemplate( | ||
text: string, | ||
buttons: Array<TemplateButton>, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'button', | ||
text, | ||
buttons, | ||
}, | ||
options | ||
); | ||
} | ||
function createGenericTemplate( | ||
elements: Array<TemplateElement>, | ||
options?: { | ||
image_aspect_ratio?: 'horizontal' | 'square', | ||
quick_replies?: Array<QuickReply>, | ||
} = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'generic', | ||
elements, | ||
image_aspect_ratio: options.image_aspect_ratio || 'horizontal', | ||
}, | ||
options | ||
); | ||
} | ||
function createListTemplate( | ||
elements: Array<TemplateElement>, | ||
buttons: Array<TemplateButton>, | ||
options?: { | ||
top_element_style?: 'large' | 'compact', | ||
quick_replies?: Array<QuickReply>, | ||
} = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'list', | ||
elements, | ||
buttons, | ||
top_element_style: options.top_element_style || 'large', | ||
}, | ||
options | ||
); | ||
} | ||
function createOpenGraphTemplate( | ||
elements: Array<OpenGraphElement>, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'open_graph', | ||
elements, | ||
}, | ||
options | ||
); | ||
} | ||
function createMediaTemplate( | ||
elements: Array<MediaElement>, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'media', | ||
elements, | ||
}, | ||
options | ||
); | ||
} | ||
function createReceiptTemplate( | ||
attrs: ReceiptAttributes, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'receipt', | ||
...attrs, | ||
}, | ||
options | ||
); | ||
} | ||
function createAirlineBoardingPassTemplate( | ||
attrs: AirlineBoardingPassAttributes, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'airline_boardingpass', | ||
...attrs, | ||
}, | ||
options | ||
); | ||
} | ||
function createAirlineCheckinTemplate( | ||
attrs: AirlineCheckinAttributes, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'airline_checkin', | ||
...attrs, | ||
}, | ||
options | ||
); | ||
} | ||
function createAirlineItineraryTemplate( | ||
attrs: AirlineItineraryAttributes, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'airline_itinerary', | ||
...attrs, | ||
}, | ||
options | ||
); | ||
} | ||
function createAirlineFlightUpdateTemplate( | ||
attrs: AirlineFlightUpdateAttributes, | ||
options?: { quick_replies?: Array<QuickReply> } = {} | ||
) { | ||
return createTemplate( | ||
{ | ||
template_type: 'airline_update', | ||
...attrs, | ||
}, | ||
options | ||
); | ||
} | ||
const Messenger = { | ||
createRequest, | ||
createMessage, | ||
createText, | ||
createAttachment, | ||
createAudio, | ||
createImage, | ||
createVideo, | ||
createFile, | ||
createTemplate, | ||
createButtonTemplate, | ||
createGenericTemplate, | ||
createListTemplate, | ||
createOpenGraphTemplate, | ||
createMediaTemplate, | ||
createReceiptTemplate, | ||
createAirlineBoardingPassTemplate, | ||
createAirlineCheckinTemplate, | ||
createAirlineItineraryTemplate, | ||
createAirlineFlightUpdateTemplate, | ||
}; | ||
export default Messenger; |
@@ -15,2 +15,3 @@ /* @flow */ | ||
import Messenger from './Messenger'; | ||
import type { | ||
@@ -138,5 +139,5 @@ UserID, | ||
*/ | ||
getPageInfo = ( | ||
{ access_token: customAccessToken }: { access_token?: string } = {} | ||
): Promise<PageInfo> => | ||
getPageInfo = ({ | ||
access_token: customAccessToken, | ||
}: { access_token?: string } = {}): Promise<PageInfo> => | ||
this._axios | ||
@@ -291,3 +292,6 @@ .get(`/me?access_token=${customAccessToken || this._accessToken}`) | ||
...options | ||
}: { composer_input_disabled: boolean } = {} | ||
}: { | ||
composer_input_disabled: boolean, | ||
access_token?: string, | ||
} = {} | ||
): Promise<MutationSuccessResponse> => { | ||
@@ -556,5 +560,5 @@ // menuItems is in type PersistentMenu | ||
*/ | ||
getMessageTags = ( | ||
{ access_token: customAccessToken }: { access_token?: string } = {} | ||
): Promise<MessageTagResponse> => | ||
getMessageTags = ({ | ||
access_token: customAccessToken, | ||
}: { access_token?: string } = {}): Promise<MessageTagResponse> => | ||
this._axios | ||
@@ -604,18 +608,7 @@ .get( | ||
const { quick_replies: quickReplies, ...otherOptions } = options; | ||
if ( | ||
quickReplies && | ||
Array.isArray(quickReplies) && | ||
quickReplies.length >= 1 | ||
) { | ||
validateQuickReplies(quickReplies); | ||
message.quick_replies = quickReplies; // eslint-disable-line no-param-reassign | ||
} | ||
return this.sendRawBody({ | ||
messaging_type: messageType, | ||
recipient, | ||
message, | ||
...otherOptions, | ||
message: Messenger.createMessage(message, options), | ||
...omit(options, 'quick_replies'), | ||
}); | ||
@@ -626,7 +619,5 @@ }; | ||
recipient: UserID | Recipient, | ||
message: Message, | ||
filedata: FileData, | ||
formdata: FormData, | ||
options?: SendOption = {} | ||
) => { | ||
const form = new FormData(); | ||
const recipientObject = | ||
@@ -646,15 +637,5 @@ typeof recipient === 'string' | ||
if ( | ||
options.quick_replies && | ||
Array.isArray(options.quick_replies) && | ||
options.quick_replies.length >= 1 | ||
) { | ||
validateQuickReplies(options.quick_replies); | ||
message.quick_replies = options.quick_replies; // eslint-disable-line no-param-reassign | ||
} | ||
formdata.append('messaging_type', messageType); | ||
formdata.append('recipient', JSON.stringify(recipientObject)); | ||
form.append('messaging_type', messageType); | ||
form.append('recipient', JSON.stringify(recipientObject)); | ||
form.append('message', JSON.stringify(message)); | ||
form.append('filedata', filedata); | ||
return this._axios | ||
@@ -664,5 +645,5 @@ .post( | ||
this._accessToken}`, | ||
form, | ||
formdata, | ||
{ | ||
headers: form.getHeaders(), | ||
headers: formdata.getHeaders(), | ||
} | ||
@@ -683,12 +664,8 @@ ) | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendMessage(recipient, { attachment }, options); | ||
this.sendMessage( | ||
recipient, | ||
Messenger.createAttachment(attachment), | ||
options | ||
); | ||
sendAttachmentFormData = ( | ||
recipient: UserID | Recipient, | ||
attachment: Attachment, | ||
filedata: FileData, | ||
option?: SendOption | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendMessageFormData(recipient, { attachment }, filedata, option); | ||
sendText = ( | ||
@@ -699,3 +676,3 @@ recipient: UserID | Recipient, | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendMessage(recipient, { text }, options); | ||
this.sendMessage(recipient, Messenger.createText(text), options); | ||
@@ -707,17 +684,10 @@ sendAudio = ( | ||
): Promise<SendMessageSucessResponse> => { | ||
const attachment = { | ||
type: 'audio', | ||
payload: {}, | ||
}; | ||
const message = Messenger.createAudio(audio, options); | ||
if (typeof audio === 'string') { | ||
attachment.payload.url = audio; | ||
return this.sendAttachment(recipient, attachment, options); | ||
} else if (audio && isPlainObject(audio)) { | ||
attachment.payload = audio; | ||
return this.sendAttachment(recipient, attachment, options); | ||
if (message && isPlainObject(message)) { | ||
return this.sendMessage(recipient, message, options); | ||
} | ||
// $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, audio, options); | ||
return this.sendMessageFormData(recipient, message, options); | ||
}; | ||
@@ -730,17 +700,10 @@ | ||
): Promise<SendMessageSucessResponse> => { | ||
const attachment = { | ||
type: 'image', | ||
payload: {}, | ||
}; | ||
const message = Messenger.createImage(image, options); | ||
if (typeof image === 'string') { | ||
attachment.payload.url = image; | ||
return this.sendAttachment(recipient, attachment, options); | ||
} else if (image && isPlainObject(image)) { | ||
attachment.payload = image; | ||
return this.sendAttachment(recipient, attachment, options); | ||
if (message && isPlainObject(message)) { | ||
return this.sendMessage(recipient, message, options); | ||
} | ||
// $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, image, options); | ||
return this.sendMessageFormData(recipient, message, options); | ||
}; | ||
@@ -753,17 +716,10 @@ | ||
): Promise<SendMessageSucessResponse> => { | ||
const attachment = { | ||
type: 'video', | ||
payload: {}, | ||
}; | ||
const message = Messenger.createVideo(video, options); | ||
if (typeof video === 'string') { | ||
attachment.payload.url = video; | ||
return this.sendAttachment(recipient, attachment, options); | ||
} else if (video && isPlainObject(video)) { | ||
attachment.payload = video; | ||
return this.sendAttachment(recipient, attachment, options); | ||
if (message && isPlainObject(message)) { | ||
return this.sendMessage(recipient, message, options); | ||
} | ||
// $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, video, options); | ||
return this.sendMessageFormData(recipient, message, options); | ||
}; | ||
@@ -776,17 +732,10 @@ | ||
): Promise<SendMessageSucessResponse> => { | ||
const attachment = { | ||
type: 'file', | ||
payload: {}, | ||
}; | ||
const message = Messenger.createFile(file, options); | ||
if (typeof file === 'string') { | ||
attachment.payload.url = file; | ||
return this.sendAttachment(recipient, attachment, options); | ||
} else if (file && isPlainObject(file)) { | ||
attachment.payload = file; | ||
return this.sendAttachment(recipient, attachment, options); | ||
if (message && isPlainObject(message)) { | ||
return this.sendMessage(recipient, message, options); | ||
} | ||
// $FlowFixMe | ||
return this.sendAttachmentFormData(recipient, attachment, file, options); | ||
return this.sendMessageFormData(recipient, message, options); | ||
}; | ||
@@ -804,10 +753,3 @@ | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendAttachment( | ||
recipient, | ||
{ | ||
type: 'template', | ||
payload, | ||
}, | ||
options | ||
); | ||
this.sendMessage(recipient, Messenger.createTemplate(payload), options); | ||
@@ -821,9 +763,5 @@ // https://developers.facebook.com/docs/messenger-platform/send-messages/template/button | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'button', | ||
text, | ||
buttons, | ||
}, | ||
Messenger.createButtonTemplate(text, buttons), | ||
options | ||
@@ -836,15 +774,17 @@ ); | ||
elements: Array<TemplateElement>, | ||
options?: { | ||
{ | ||
// $FlowFixMe | ||
image_aspect_ratio = 'horizontal', | ||
...options | ||
}: { | ||
image_aspect_ratio: 'horizontal' | 'square', | ||
...SendOption, | ||
image_aspect_ratio?: 'horizontal' | 'square', | ||
} = {} | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'generic', | ||
elements, | ||
image_aspect_ratio: options.image_aspect_ratio || 'horizontal', | ||
}, | ||
omit(options, ['image_aspect_ratio']) | ||
Messenger.createGenericTemplate(elements, { | ||
image_aspect_ratio, | ||
}), | ||
options | ||
); | ||
@@ -857,16 +797,17 @@ | ||
buttons: Array<TemplateButton>, | ||
options?: { | ||
{ | ||
// $FlowFixMe | ||
top_element_style = 'large', | ||
...options | ||
}: { | ||
top_element_style: 'large' | 'compact', | ||
...SendOption, | ||
top_element_style?: 'large' | 'compact', | ||
} = {} | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'list', | ||
elements, | ||
buttons, | ||
top_element_style: options.top_element_style || 'large', | ||
}, | ||
omit(options, ['top_element_style']) | ||
Messenger.createListTemplate(elements, buttons, { | ||
top_element_style, | ||
}), | ||
options | ||
); | ||
@@ -880,8 +821,5 @@ | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'open_graph', | ||
elements, | ||
}, | ||
Messenger.createOpenGraphTemplate(elements), | ||
options | ||
@@ -896,8 +834,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'receipt', | ||
...attrs, | ||
}, | ||
Messenger.createReceiptTemplate(attrs), | ||
options | ||
@@ -912,8 +847,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'media', | ||
elements, | ||
}, | ||
Messenger.createMediaTemplate(elements), | ||
options | ||
@@ -928,8 +860,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'airline_boardingpass', | ||
...attrs, | ||
}, | ||
Messenger.createAirlineBoardingPassTemplate(attrs), | ||
options | ||
@@ -944,8 +873,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'airline_checkin', | ||
...attrs, | ||
}, | ||
Messenger.createAirlineCheckinTemplate(attrs), | ||
options | ||
@@ -960,8 +886,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'airline_itinerary', | ||
...attrs, | ||
}, | ||
Messenger.createAirlineItineraryTemplate(attrs), | ||
options | ||
@@ -976,8 +899,5 @@ ); | ||
): Promise<SendMessageSucessResponse> => | ||
this.sendTemplate( | ||
this.sendMessage( | ||
recipient, | ||
{ | ||
template_type: 'airline_update', | ||
...attrs, | ||
}, | ||
Messenger.createAirlineFlightUpdateTemplate(attrs), | ||
options | ||
@@ -1143,4 +1063,5 @@ ); | ||
.post( | ||
`/act_${adAccountId}/sponsored_message_ads?access_token=${this | ||
._accessToken}`, | ||
`/act_${adAccountId}/sponsored_message_ads?access_token=${ | ||
this._accessToken | ||
}`, | ||
message | ||
@@ -1472,5 +1393,5 @@ ) | ||
*/ | ||
getSecondaryReceivers = ( | ||
{ access_token: customAccessToken }: { access_token: ?string } = {} | ||
) => | ||
getSecondaryReceivers = ({ | ||
access_token: customAccessToken, | ||
}: { access_token: ?string } = {}) => | ||
this._axios | ||
@@ -1477,0 +1398,0 @@ .get( |
Sorry, the diff of this file is too big to display
359586
8013