bottender
Version:
A framework for building conversational user interfaces.
380 lines • 11.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const messaging_api_common_1 = require("messaging-api-common");
class MessengerEvent {
constructor(rawEvent, options = {}) {
this._rawEvent = rawEvent;
this._isStandby = options.isStandby || false;
this._pageId = options.pageId || null;
}
get rawEvent() {
return this._rawEvent;
}
get timestamp() {
return this._rawEvent.timestamp;
}
get isMessage() {
return ('message' in this._rawEvent && typeof this._rawEvent.message === 'object');
}
get message() {
if ('message' in this._rawEvent) {
return this._rawEvent.message;
}
return null;
}
get isText() {
return this.isMessage && typeof this.message.text === 'string';
}
get text() {
if (this.isText) {
return this.message.text || null;
}
return null;
}
get hasAttachment() {
return (this.isMessage &&
!!this.message.attachments &&
this.message.attachments.length > 0);
}
get attachments() {
if (this.message && this.message.attachments) {
return this.message.attachments;
}
return null;
}
get isImage() {
return this.hasAttachment && this.attachments[0].type === 'image';
}
get image() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'image') {
return attachment.payload;
}
return null;
}
get isAudio() {
return this.hasAttachment && this.attachments[0].type === 'audio';
}
get audio() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'audio') {
return attachment.payload;
}
return null;
}
get isVideo() {
return this.hasAttachment && this.attachments[0].type === 'video';
}
get video() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'video') {
return attachment.payload;
}
return null;
}
get isLocation() {
return this.hasAttachment && this.attachments[0].type === 'location';
}
get location() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'location') {
return attachment.payload;
}
return null;
}
get isFile() {
return this.hasAttachment && this.attachments[0].type === 'file';
}
get file() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'file') {
return attachment.payload;
}
return null;
}
get isFallback() {
return this.hasAttachment && this.attachments[0].type === 'fallback';
}
get fallback() {
if (!this.hasAttachment) {
return null;
}
const attachment = this.attachments[0];
if (attachment.type === 'fallback') {
return attachment;
}
return null;
}
get isSticker() {
return this.isMessage && typeof this.message.stickerId === 'number';
}
get sticker() {
if (this.isSticker) {
return this.message.stickerId || null;
}
return null;
}
get isLikeSticker() {
return (this.isSticker &&
(this.message.stickerId === 369239263222822 ||
this.message.stickerId === 369239343222814 ||
this.message.stickerId === 369239383222810));
}
get isQuickReply() {
return (this.isMessage &&
!!this.message.quickReply &&
typeof this.message.quickReply === 'object');
}
get quickReply() {
if (this.message && this.message.quickReply) {
return this.message.quickReply;
}
return null;
}
get isEcho() {
return this.isMessage && !!this.message.isEcho;
}
get isPostback() {
return ('postback' in this._rawEvent &&
typeof this._rawEvent.postback === 'object');
}
get postback() {
if ('postback' in this._rawEvent) {
return this._rawEvent.postback;
}
return null;
}
get isGamePlay() {
return ('gamePlay' in this._rawEvent &&
typeof this._rawEvent.gamePlay === 'object');
}
get gamePlay() {
if (!('gamePlay' in this._rawEvent)) {
return null;
}
const rawGamePlay = this._rawEvent.gamePlay;
let payload;
try {
const parsed = JSON.parse(rawGamePlay.payload);
payload =
parsed && typeof parsed === 'object'
? messaging_api_common_1.camelcaseKeysDeep(parsed)
: parsed;
}
catch (err) {
payload = rawGamePlay.payload;
}
return Object.assign(Object.assign({}, rawGamePlay), { payload });
}
get isOptin() {
return ('optin' in this._rawEvent && typeof this._rawEvent.optin === 'object');
}
get optin() {
if ('optin' in this._rawEvent) {
return this._rawEvent.optin;
}
return null;
}
get isPayment() {
return ('payment' in this._rawEvent && typeof this._rawEvent.payment === 'object');
}
get payment() {
if ('payment' in this._rawEvent) {
return this._rawEvent.payment;
}
return null;
}
get isCheckoutUpdate() {
return ('checkoutUpdate' in this._rawEvent &&
typeof this._rawEvent.checkoutUpdate === 'object');
}
get checkoutUpdate() {
if ('checkoutUpdate' in this._rawEvent) {
return this._rawEvent.checkoutUpdate;
}
return null;
}
get isPreCheckout() {
return ('preCheckout' in this._rawEvent &&
typeof this._rawEvent.preCheckout === 'object');
}
get preCheckout() {
if ('preCheckout' in this._rawEvent) {
return this._rawEvent.preCheckout;
}
return null;
}
get isRead() {
return 'read' in this._rawEvent && typeof this._rawEvent.read === 'object';
}
get read() {
if ('read' in this._rawEvent) {
return this._rawEvent.read;
}
return null;
}
get isDelivery() {
return ('delivery' in this._rawEvent &&
typeof this._rawEvent.delivery === 'object');
}
get delivery() {
if ('delivery' in this._rawEvent) {
return this._rawEvent.delivery;
}
return null;
}
get isPayload() {
return ((!!this.postback && typeof this.postback.payload === 'string') ||
(!!this.quickReply && typeof this.quickReply.payload === 'string'));
}
get payload() {
if (!!this.postback && this.isPayload) {
return this.postback.payload || null;
}
if (!!this.quickReply && this.isPayload) {
return this.quickReply.payload || null;
}
return null;
}
get isPolicyEnforcement() {
return ('policy-enforcement' in this._rawEvent &&
typeof this._rawEvent['policy-enforcement'] === 'object');
}
get policyEnforcement() {
if ('policy-enforcement' in this._rawEvent) {
return this._rawEvent['policy-enforcement'];
}
return null;
}
get isAppRoles() {
return ('appRoles' in this._rawEvent &&
typeof this._rawEvent.appRoles === 'object');
}
get appRoles() {
if ('appRoles' in this._rawEvent) {
return this._rawEvent.appRoles;
}
return null;
}
get isStandby() {
return this._isStandby;
}
get isPassThreadControl() {
return ('passThreadControl' in this._rawEvent &&
typeof this._rawEvent.passThreadControl === 'object');
}
get passThreadControl() {
if ('passThreadControl' in this._rawEvent) {
return this._rawEvent.passThreadControl;
}
return null;
}
get isTakeThreadControl() {
return ('takeThreadControl' in this._rawEvent &&
typeof this._rawEvent.takeThreadControl === 'object');
}
get takeThreadControl() {
if ('takeThreadControl' in this._rawEvent) {
return this._rawEvent.takeThreadControl;
}
return null;
}
get isRequestThreadControl() {
return ('requestThreadControl' in this._rawEvent &&
typeof this._rawEvent.requestThreadControl === 'object');
}
get isRequestThreadControlFromPageInbox() {
return ('requestThreadControl' in this._rawEvent &&
typeof this._rawEvent.requestThreadControl === 'object' &&
this._rawEvent.requestThreadControl.requestedOwnerAppId ===
263902037430900);
}
get requestThreadControl() {
if ('requestThreadControl' in this._rawEvent) {
return this._rawEvent.requestThreadControl;
}
return null;
}
get isFromCustomerChatPlugin() {
const isMessageFromCustomerChatPlugin = !!(this.isMessage &&
'tags' in this.message &&
this.message.tags.length !== 0 &&
this.message.tags.some((tag) => tag.source === 'customer_chat_plugin'));
const isReferralFromCustomerChatPlugin = !!(this.isReferral &&
this.referral &&
this.referral.source === 'CUSTOMER_CHAT_PLUGIN');
return isMessageFromCustomerChatPlugin || isReferralFromCustomerChatPlugin;
}
get isReferral() {
return !!('referral' in this._rawEvent ||
('postback' in this._rawEvent && this._rawEvent.postback.referral));
}
get referral() {
if (!this.isReferral) {
return null;
}
if ('referral' in this._rawEvent) {
return this._rawEvent.referral;
}
if ('postback' in this._rawEvent && 'referral' in this._rawEvent.postback) {
return this._rawEvent.postback.referral || null;
}
return null;
}
get ref() {
if (!this.isReferral) {
return null;
}
return this.referral && this.referral.ref;
}
get pageId() {
return this._pageId || null;
}
get isBrandedCamera() {
return ('brandedCamera' in this._rawEvent &&
typeof this._rawEvent.brandedCamera === 'object');
}
get brandedCamera() {
if ('brandedCamera' in this._rawEvent) {
return this._rawEvent.brandedCamera;
}
return null;
}
get isAccountLinking() {
return ('accountLinking' in this._rawEvent &&
typeof this._rawEvent.accountLinking === 'object');
}
get accountLinking() {
if ('accountLinking' in this._rawEvent) {
return this._rawEvent.accountLinking;
}
return null;
}
get isReaction() {
return ('reaction' in this._rawEvent &&
typeof this._rawEvent.reaction === 'object');
}
get reaction() {
if ('reaction' in this._rawEvent) {
return this._rawEvent.reaction;
}
return null;
}
}
exports.default = MessengerEvent;
//# sourceMappingURL=MessengerEvent.js.map