facebook-send-api
Advanced tools
Comparing version 2.7.0 to 2.8.0
@@ -0,72 +1,4 @@ | ||
/// <reference types="bluebird" /> | ||
import * as Promise from 'bluebird'; | ||
export interface MessengerQuickReply { | ||
content_type: string; | ||
title: string; | ||
payload: string; | ||
} | ||
export interface MessengerButton { | ||
type: string; | ||
title: string; | ||
payload?: string; | ||
url?: string; | ||
} | ||
export interface MessengerItem { | ||
title: string; | ||
subtitle?: string; | ||
image_url?: string; | ||
buttons?: Array<MessengerButton>; | ||
} | ||
export interface MessengerTextMessage { | ||
text: string; | ||
} | ||
export interface MessengerGenericPayload { | ||
template_type: string; | ||
elements: Array<MessengerItem>; | ||
} | ||
export interface MessengerButtonPayload { | ||
template_type: string; | ||
text: string; | ||
buttons: Array<MessengerButton>; | ||
} | ||
export interface MessengerAttachement { | ||
type: string; | ||
payload: MessengerGenericPayload | MessengerButtonPayload; | ||
} | ||
export interface MessengerMessage { | ||
attachment?: MessengerAttachement; | ||
text?: string; | ||
quick_replies?: Array<MessengerQuickReply>; | ||
metadata?: string; | ||
} | ||
export declare type NotificationType = 'REGULAR' | 'SILENT_PUSH' | 'NO_PUSH'; | ||
export declare type SenderAction = 'mark_seen' | 'typing_on' | 'typing_off'; | ||
export interface MessengerPayload { | ||
recipient: { | ||
id?: string; | ||
phone_number?: string; | ||
}; | ||
message?: MessengerMessage; | ||
sender_action?: SenderAction; | ||
notification_type?: NotificationType; | ||
} | ||
export interface MessengerResponse { | ||
recipient_id: string; | ||
message_id: string; | ||
} | ||
export interface MessengerError { | ||
error: { | ||
message: string; | ||
type: string; | ||
code: Number; | ||
fbtrace_id: string; | ||
}; | ||
} | ||
export interface FacebookUser { | ||
first_name: string; | ||
last_name: string; | ||
profile_pic: string; | ||
locale: string; | ||
timezone: number; | ||
gender: string; | ||
} | ||
import * as FBTypes from 'facebook-sendapi-types'; | ||
export declare class FBMessage { | ||
@@ -77,5 +9,5 @@ protected platform: FBPlatform; | ||
protected messageSubTitle: string; | ||
protected buttons: Array<MessengerButton>; | ||
protected buttons: Array<FBTypes.MessengerButton>; | ||
protected image_url: string; | ||
protected elements: Array<MessengerItem>; | ||
protected elements: Array<FBTypes.MessengerItem>; | ||
constructor(platform: FBPlatform, id: string); | ||
@@ -88,25 +20,25 @@ title(title: string): this; | ||
image(url: string): this; | ||
element(anElement: MessengerItem | FBElement): this; | ||
element(anElement: FBTypes.MessengerItem | FBElement): this; | ||
} | ||
export declare class FBElement extends FBMessage { | ||
constructor(platform?: FBPlatform); | ||
create(): MessengerItem; | ||
create(): FBTypes.MessengerItem; | ||
} | ||
export declare class FBButtonMessage extends FBMessage { | ||
send(): Promise<MessengerResponse>; | ||
send(): Promise<FBTypes.MessengerResponse>; | ||
} | ||
export declare class FBGenericMessage extends FBMessage { | ||
send(): Promise<MessengerResponse>; | ||
send(): Promise<FBTypes.MessengerResponse>; | ||
} | ||
export declare class FBTextMessage extends FBMessage { | ||
send(): Promise<MessengerResponse>; | ||
export(): MessengerPayload; | ||
send(): Promise<FBTypes.MessengerResponse>; | ||
export(): FBTypes.MessengerPayload; | ||
} | ||
export declare class FBButton extends FBMessage { | ||
create(): Array<MessengerButton>; | ||
create(): Array<FBTypes.MessengerButton>; | ||
} | ||
export declare class FBQuickReplies extends FBMessage { | ||
send(): Promise<MessengerResponse>; | ||
send(): Promise<FBTypes.MessengerResponse>; | ||
} | ||
export declare type LoggerFunction = (payload: MessengerPayload) => Promise<void>; | ||
export declare type LoggerFunction = (payload: FBTypes.MessengerPayload) => Promise<void>; | ||
export default class FBPlatform { | ||
@@ -125,29 +57,29 @@ protected token: string; | ||
turnOnValidation(state?: boolean): this; | ||
wrapMessage(id: string, message: MessengerMessage, notification_type: NotificationType): MessengerPayload; | ||
wrapMessage(id: string, message: FBTypes.MessengerMessage, notification_type: FBTypes.NotificationType): FBTypes.MessengerPayload; | ||
private sendToFB(payload, path); | ||
sendMessageToFB(id: string, message: MessengerMessage, notification_type?: NotificationType): Promise<MessengerResponse>; | ||
sendMessageToFB(id: string, message: FBTypes.MessengerMessage, notification_type?: FBTypes.NotificationType): Promise<FBTypes.MessengerResponse>; | ||
createGenericMessage(id: string): FBGenericMessage; | ||
exportGenericMessage(elements: Array<MessengerItem>): MessengerMessage; | ||
sendGenericMessage(id: string, elements: Array<MessengerItem>): Promise<MessengerResponse>; | ||
static exportGenericMessage(elements: Array<FBTypes.MessengerItem>, maxElements?: number): FBTypes.MessengerMessage; | ||
sendGenericMessage(id: string, elements: Array<FBTypes.MessengerItem>): Promise<FBTypes.MessengerResponse>; | ||
createButtonMessage(id: string): FBButtonMessage; | ||
exportButtonMessage(text: string, buttons: Array<MessengerButton> | FBButton): MessengerMessage; | ||
sendButtonMessage(id: string, text: string, buttons: Array<MessengerButton> | FBButton): Promise<MessengerResponse>; | ||
static exportButtonMessage(text: string, buttons: Array<FBTypes.MessengerButton> | FBButton, maxButtons?: number): FBTypes.MessengerMessage; | ||
sendButtonMessage(id: string, text: string, buttons: Array<FBTypes.MessengerButton> | FBButton): Promise<FBTypes.MessengerResponse>; | ||
createTextMessage(id: string): FBTextMessage; | ||
exportTextMessage(text: string): MessengerMessage; | ||
sendTextMessage(id: string, text: string): Promise<MessengerResponse>; | ||
static exportTextMessage(text: string): FBTypes.MessengerMessage; | ||
sendTextMessage(id: string, text: string): Promise<FBTypes.MessengerResponse>; | ||
createQuickReplies(id: string): FBQuickReplies; | ||
exportQuickReplies(text: string, quickReplies: Array<MessengerQuickReply>): MessengerMessage; | ||
sendQuickReplies(id: string, text: string, quickReplies: Array<MessengerQuickReply>): Promise<MessengerResponse>; | ||
sendSenderAction(id: string, senderAction: SenderAction): Promise<MessengerResponse>; | ||
sendTypingIndicators(id: string): Promise<MessengerResponse>; | ||
sendCancelTypingIndicators(id: string): Promise<MessengerResponse>; | ||
sendReadReceipt(id: string): Promise<MessengerResponse>; | ||
static exportQuickReplies(text: string, quickReplies: Array<FBTypes.MessengerQuickReply>, maxQuickReplies?: number): FBTypes.MessengerMessage; | ||
sendQuickReplies(id: string, text: string, quickReplies: Array<FBTypes.MessengerQuickReply>): Promise<FBTypes.MessengerResponse>; | ||
sendSenderAction(id: string, senderAction: FBTypes.SenderAction): Promise<FBTypes.MessengerResponse>; | ||
sendTypingIndicators(id: string): Promise<FBTypes.MessengerResponse>; | ||
sendCancelTypingIndicators(id: string): Promise<FBTypes.MessengerResponse>; | ||
sendReadReceipt(id: string): Promise<FBTypes.MessengerResponse>; | ||
private sendSettingsToFB(payload); | ||
setGetStartedPostback(payload: string): Promise<MessengerResponse>; | ||
setPersistentMenu(buttons: Array<MessengerButton>): Promise<MessengerResponse>; | ||
setGreetingText(text: string): Promise<MessengerResponse>; | ||
createPostbackButton(title: string, payload: string): MessengerButton; | ||
createWebButton(title: string, url: string): MessengerButton; | ||
createQuickReply(title: string, payload: string): MessengerQuickReply; | ||
getUserProfile(id: string): Promise<FacebookUser>; | ||
setGetStartedPostback(payload: string): Promise<FBTypes.MessengerResponse>; | ||
setPersistentMenu(buttons: Array<FBTypes.MessengerButton>): Promise<FBTypes.MessengerResponse>; | ||
setGreetingText(text: string): Promise<FBTypes.MessengerResponse>; | ||
createPostbackButton(title: string, payload: string): FBTypes.MessengerButton; | ||
createWebButton(title: string, url: string): FBTypes.MessengerButton; | ||
createQuickReply(title: string, payload: string): FBTypes.MessengerQuickReply; | ||
getUserProfile(id: string): Promise<FBTypes.FacebookUser>; | ||
} |
@@ -7,5 +7,5 @@ "use strict"; | ||
}; | ||
var Promise = require('bluebird'); | ||
var rp = require('request-promise'); | ||
var util = require('util'); | ||
var Promise = require("bluebird"); | ||
var rp = require("request-promise"); | ||
var util = require("util"); | ||
var DefaultFBGraphURL = process.env.FBGRAPHURL || 'https://graph.facebook.com/v2.6'; | ||
@@ -60,4 +60,4 @@ var FBMessage = (function () { | ||
if (platform === void 0) { platform = new FBPlatform(null); } | ||
_super.call(this, platform, null); | ||
return this; | ||
var _this = _super.call(this, platform, null) || this; | ||
return _this; | ||
} | ||
@@ -82,3 +82,3 @@ FBElement.prototype.create = function () { | ||
function FBButtonMessage() { | ||
_super.apply(this, arguments); | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -94,3 +94,3 @@ FBButtonMessage.prototype.send = function () { | ||
function FBGenericMessage() { | ||
_super.apply(this, arguments); | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -106,3 +106,3 @@ FBGenericMessage.prototype.send = function () { | ||
function FBTextMessage() { | ||
_super.apply(this, arguments); | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -128,3 +128,3 @@ FBTextMessage.prototype.send = function () { | ||
function FBButton() { | ||
_super.apply(this, arguments); | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -140,3 +140,3 @@ FBButton.prototype.create = function () { | ||
function FBQuickReplies() { | ||
_super.apply(this, arguments); | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -201,2 +201,3 @@ FBQuickReplies.prototype.send = function () { | ||
}; | ||
// console.log('requstPayload', util.inspect(requstPayload, { depth: null })); | ||
return rp(requstPayload) | ||
@@ -228,3 +229,4 @@ .then(function (body) { | ||
}; | ||
FBPlatform.prototype.exportGenericMessage = function (elements) { | ||
FBPlatform.exportGenericMessage = function (elements, maxElements) { | ||
if (maxElements === void 0) { maxElements = 10; } | ||
var messageData = { | ||
@@ -235,3 +237,3 @@ 'attachment': { | ||
'template_type': 'generic', | ||
elements: elements.slice(0, this.maxElements), | ||
elements: elements.slice(0, maxElements), | ||
}, | ||
@@ -249,3 +251,3 @@ }, | ||
//buttons is limited to 3 | ||
return this.sendMessageToFB(id, this.exportGenericMessage(elements)); | ||
return this.sendMessageToFB(id, FBPlatform.exportGenericMessage(elements, this.maxElements)); | ||
}; | ||
@@ -255,5 +257,7 @@ FBPlatform.prototype.createButtonMessage = function (id) { | ||
}; | ||
FBPlatform.prototype.exportButtonMessage = function (text, buttons) { | ||
FBPlatform.exportButtonMessage = function (text, buttons, maxButtons) { | ||
if (maxButtons === void 0) { maxButtons = 3; } | ||
var theButtons = null; | ||
if (buttons instanceof FBButton) { | ||
console.log('buttons:', typeof buttons); | ||
if (typeof buttons === typeof FBButton) { | ||
var asAButton = buttons; | ||
@@ -265,4 +269,4 @@ theButtons = asAButton.create(); | ||
} | ||
if (theButtons.length > this.maxButtons && this.validateLimits) { | ||
throw new Error("Sending too many buttons, max is " + this.maxButtons + ", tried sending " + theButtons.length); | ||
if (theButtons.length > maxButtons) { | ||
throw new Error("Sending too many buttons, max is $maxButtons}, tried sending " + theButtons.length); | ||
} | ||
@@ -275,3 +279,3 @@ var messageData = { | ||
text: text, | ||
buttons: theButtons.slice(0, this.maxButtons), | ||
buttons: theButtons.slice(0, maxButtons), | ||
}, | ||
@@ -283,3 +287,3 @@ }, | ||
FBPlatform.prototype.sendButtonMessage = function (id, text, buttons) { | ||
return this.sendMessageToFB(id, this.exportButtonMessage(text, buttons)); | ||
return this.sendMessageToFB(id, FBPlatform.exportButtonMessage(text, buttons, this.maxButtons)); | ||
}; | ||
@@ -289,3 +293,3 @@ FBPlatform.prototype.createTextMessage = function (id) { | ||
}; | ||
FBPlatform.prototype.exportTextMessage = function (text) { | ||
FBPlatform.exportTextMessage = function (text) { | ||
var messageData = { | ||
@@ -297,3 +301,3 @@ text: text, | ||
FBPlatform.prototype.sendTextMessage = function (id, text) { | ||
return this.sendMessageToFB(id, this.exportTextMessage(text)); | ||
return this.sendMessageToFB(id, FBPlatform.exportTextMessage(text)); | ||
}; | ||
@@ -303,6 +307,7 @@ FBPlatform.prototype.createQuickReplies = function (id) { | ||
}; | ||
FBPlatform.prototype.exportQuickReplies = function (text, quickReplies) { | ||
FBPlatform.exportQuickReplies = function (text, quickReplies, maxQuickReplies) { | ||
if (maxQuickReplies === void 0) { maxQuickReplies = 10; } | ||
var messageData = { | ||
text: text, | ||
quick_replies: quickReplies.slice(0, this.maxQuickReplies), | ||
quick_replies: quickReplies.slice(0, maxQuickReplies), | ||
}; | ||
@@ -315,3 +320,3 @@ return messageData; | ||
} | ||
return this.sendMessageToFB(id, this.exportQuickReplies(text, quickReplies)); | ||
return this.sendMessageToFB(id, FBPlatform.exportQuickReplies(text, quickReplies, this.maxQuickReplies)); | ||
}; | ||
@@ -340,3 +345,3 @@ FBPlatform.prototype.sendSenderAction = function (id, senderAction) { | ||
FBPlatform.prototype.setGetStartedPostback = function (payload) { | ||
var messengerpayload = { | ||
var Messengerpayload = { | ||
setting_type: 'call_to_actions', | ||
@@ -348,6 +353,6 @@ thread_state: 'new_thread', | ||
}; | ||
return this.sendSettingsToFB(messengerpayload); | ||
return this.sendSettingsToFB(Messengerpayload); | ||
}; | ||
FBPlatform.prototype.setPersistentMenu = function (buttons) { | ||
var messengerPayload = { | ||
var MessengerPayload = { | ||
setting_type: 'call_to_actions', | ||
@@ -357,6 +362,6 @@ thread_state: 'existing_thread', | ||
}; | ||
return this.sendSettingsToFB(messengerPayload); | ||
return this.sendSettingsToFB(MessengerPayload); | ||
}; | ||
FBPlatform.prototype.setGreetingText = function (text) { | ||
var messengerPayload = { | ||
var MessengerPayload = { | ||
setting_type: 'greeting', | ||
@@ -367,3 +372,3 @@ greeting: { | ||
}; | ||
return this.sendSettingsToFB(messengerPayload); | ||
return this.sendSettingsToFB(MessengerPayload); | ||
}; | ||
@@ -370,0 +375,0 @@ FBPlatform.prototype.createPostbackButton = function (title, payload) { |
{ | ||
"name": "facebook-send-api", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "Promise based module to speak to the facebook send api for the messenger platform", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prepublish": "tsc --project src" | ||
"prepublish": "tsc" | ||
}, | ||
@@ -30,8 +30,11 @@ "typings": "lib/index", | ||
"bluebird": "^3.3.5", | ||
"facebook-sendapi-types": "github:fyndme/facebook-sendapi-types", | ||
"request-promise": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^1.8.10", | ||
"typings": "^1.0.4" | ||
"@types/bluebird": "^3.0.37", | ||
"@types/request": "0.0.39", | ||
"@types/request-promise": "^4.1.33", | ||
"typescript": "^2.1.5" | ||
} | ||
} |
210
src/index.ts
import * as Promise from 'bluebird'; | ||
import * as rp from 'request-promise'; | ||
import * as util from 'util'; | ||
export interface MessengerQuickReply { | ||
content_type: string, | ||
title: string, | ||
payload: string, | ||
} | ||
export interface MessengerButton { | ||
type: string, | ||
title: string, | ||
payload?: string, | ||
url?: string, | ||
} | ||
export interface MessengerItem { | ||
title: string, | ||
subtitle?: string, | ||
image_url?: string, | ||
buttons?: Array<MessengerButton>, | ||
} | ||
export interface MessengerTextMessage { | ||
text: string, | ||
} | ||
export interface MessengerGenericPayload { | ||
template_type: string, | ||
elements: Array<MessengerItem>, | ||
} | ||
export interface MessengerButtonPayload { | ||
template_type: string, | ||
text: string, | ||
buttons: Array<MessengerButton>, | ||
} | ||
export interface MessengerAttachement { | ||
type: string, | ||
payload: MessengerGenericPayload | MessengerButtonPayload, | ||
} | ||
export interface MessengerMessage { | ||
attachment?: MessengerAttachement, | ||
text?: string, | ||
quick_replies?: Array<MessengerQuickReply>, | ||
metadata?: string, | ||
} | ||
export type NotificationType = 'REGULAR' | 'SILENT_PUSH' | 'NO_PUSH'; | ||
export type SenderAction = 'mark_seen' | 'typing_on' | 'typing_off'; | ||
export interface MessengerPayload { | ||
recipient: { | ||
id?: string; | ||
phone_number?: string; | ||
}; | ||
message?: MessengerMessage; | ||
sender_action?: SenderAction; | ||
notification_type?: NotificationType; | ||
} | ||
export interface MessengerResponse { | ||
recipient_id: string, | ||
message_id: string, | ||
} | ||
export interface MessengerError { | ||
error: { | ||
message: string, | ||
type: string, | ||
code: Number, | ||
fbtrace_id: string, | ||
}, | ||
} | ||
export interface FacebookUser { | ||
first_name: string; | ||
last_name: string; | ||
profile_pic: string; | ||
locale: string; | ||
timezone: number; | ||
gender: string; | ||
} | ||
interface MessengerPostback { | ||
payload: string, | ||
} | ||
interface MessengerSettings { | ||
setting_type: string, | ||
thread_state?: string, | ||
call_to_actions?: Array<MessengerPostback> | Array<MessengerButton>, | ||
greeting?: { | ||
text: string, | ||
}, | ||
} | ||
import * as FBTypes from 'facebook-sendapi-types'; | ||
const DefaultFBGraphURL = process.env.FBGRAPHURL || 'https://graph.facebook.com/v2.6'; | ||
@@ -107,5 +12,5 @@ | ||
protected messageSubTitle: string; | ||
protected buttons: Array<MessengerButton>; | ||
protected buttons: Array<FBTypes.MessengerButton>; | ||
protected image_url: string; | ||
protected elements: Array<MessengerItem>; | ||
protected elements: Array<FBTypes.MessengerItem>; | ||
@@ -150,4 +55,4 @@ constructor(platform: FBPlatform, id: string) { | ||
public element(anElement: MessengerItem | FBElement) { | ||
let theElement:MessengerItem = anElement as MessengerItem; | ||
public element(anElement: FBTypes.MessengerItem | FBElement) { | ||
let theElement:FBTypes.MessengerItem = anElement as FBTypes.MessengerItem; | ||
if (typeof anElement === 'FBElement') { | ||
@@ -167,3 +72,3 @@ const elementAsClass = anElement as FBElement; | ||
} | ||
public create():MessengerItem { | ||
public create():FBTypes.MessengerItem { | ||
let element:any = {}; | ||
@@ -194,3 +99,3 @@ if (this.messageTitle) element.title = this.messageTitle; | ||
} | ||
public export(): MessengerPayload { | ||
public export(): FBTypes.MessengerPayload { | ||
return { | ||
@@ -208,3 +113,3 @@ recipient: { | ||
export class FBButton extends FBMessage { | ||
public create():Array<MessengerButton> { | ||
public create():Array<FBTypes.MessengerButton> { | ||
return this.buttons; | ||
@@ -216,4 +121,4 @@ } | ||
public send() { | ||
const postbackButtons: Array<MessengerButton> = this.buttons.filter(button => button.type === 'postback'); | ||
const quickReplies: Array<MessengerQuickReply> = postbackButtons.map(button => { | ||
const postbackButtons: Array<FBTypes.MessengerButton> = this.buttons.filter(button => button.type === 'postback'); | ||
const quickReplies: Array<FBTypes.MessengerQuickReply> = postbackButtons.map(button => { | ||
return this.platform.createQuickReply(button.title, button.payload); | ||
@@ -225,3 +130,3 @@ }); | ||
export type LoggerFunction = (payload: MessengerPayload) => Promise<void>; | ||
export type LoggerFunction = (payload: FBTypes.MessengerPayload) => Promise<void>; | ||
@@ -257,4 +162,4 @@ export default class FBPlatform { | ||
public wrapMessage(id: string, message: MessengerMessage, notification_type: NotificationType): MessengerPayload { | ||
const mesengerPayload: MessengerPayload = { | ||
public wrapMessage(id: string, message: FBTypes.MessengerMessage, notification_type: FBTypes.NotificationType): FBTypes.MessengerPayload { | ||
const mesengerPayload: FBTypes.MessengerPayload = { | ||
recipient: { id: id.toString() }, | ||
@@ -267,3 +172,3 @@ message, | ||
private sendToFB(payload: MessengerPayload | MessengerSettings, path: string): Promise<MessengerResponse> { | ||
private sendToFB(payload: FBTypes.MessengerPayload | FBTypes.MessengerSettings, path: string): Promise<FBTypes.MessengerResponse> { | ||
if (process.env.NODE_ENV === 'development' && this.sendInDevelopment === false) { | ||
@@ -284,2 +189,4 @@ console.log(`${JSON.stringify(payload)}`); | ||
// console.log('requstPayload', util.inspect(requstPayload, { depth: null })); | ||
return rp(requstPayload) | ||
@@ -299,3 +206,3 @@ .then((body) => { | ||
public sendMessageToFB(id: string, message: MessengerMessage, notification_type: NotificationType = 'REGULAR') { | ||
public sendMessageToFB(id: string, message: FBTypes.MessengerMessage, notification_type: FBTypes.NotificationType = 'REGULAR') { | ||
const mesengerPayload = this.wrapMessage(id, message, notification_type); | ||
@@ -314,4 +221,4 @@ let promise = Promise.resolve(null); | ||
public exportGenericMessage(elements: Array<MessengerItem>): MessengerMessage { | ||
const messageData: MessengerMessage = { | ||
static exportGenericMessage(elements: Array<FBTypes.MessengerItem>, maxElements: number = 10): FBTypes.MessengerMessage { | ||
const messageData: FBTypes.MessengerMessage = { | ||
'attachment': { | ||
@@ -321,3 +228,3 @@ 'type': 'template', | ||
'template_type': 'generic', | ||
elements: elements.slice(0, this.maxElements), | ||
elements: elements.slice(0, maxElements), | ||
}, | ||
@@ -329,3 +236,3 @@ }, | ||
public sendGenericMessage(id: string, elements: Array<MessengerItem>) { | ||
public sendGenericMessage(id: string, elements: Array<FBTypes.MessengerItem>) { | ||
if (elements.length > this.maxElements && this.validateLimits) { | ||
@@ -338,3 +245,3 @@ throw new Error(`Sending too many elements, max is ${this.maxElements}, tried sending ${elements.length}`); | ||
return this.sendMessageToFB(id, this.exportGenericMessage(elements)); | ||
return this.sendMessageToFB(id, FBPlatform.exportGenericMessage(elements, this.maxElements)); | ||
} | ||
@@ -346,17 +253,18 @@ | ||
public exportButtonMessage(text: string, buttons: Array<MessengerButton> | FBButton): MessengerMessage { | ||
let theButtons:Array<MessengerButton> = null; | ||
static exportButtonMessage(text: string, buttons: Array<FBTypes.MessengerButton> | FBButton, maxButtons: number = 3): FBTypes.MessengerMessage { | ||
let theButtons:Array<FBTypes.MessengerButton> = null; | ||
if (buttons instanceof FBButton) { | ||
console.log('buttons:', typeof buttons); | ||
if (typeof buttons === typeof FBButton) { | ||
const asAButton: FBButton = buttons as FBButton; | ||
theButtons = asAButton.create(); | ||
} else { | ||
theButtons = buttons as Array<MessengerButton>; | ||
theButtons = buttons as Array<FBTypes.MessengerButton>; | ||
} | ||
if (theButtons.length > this.maxButtons && this.validateLimits) { | ||
throw new Error(`Sending too many buttons, max is ${this.maxButtons}, tried sending ${theButtons.length}`); | ||
if (theButtons.length > maxButtons) { | ||
throw new Error(`Sending too many buttons, max is $maxButtons}, tried sending ${theButtons.length}`); | ||
} | ||
const messageData: MessengerMessage = { | ||
const messageData: FBTypes.MessengerMessage = { | ||
'attachment': { | ||
@@ -367,3 +275,3 @@ 'type': 'template', | ||
text, | ||
buttons: theButtons.slice(0, this.maxButtons), | ||
buttons: theButtons.slice(0, maxButtons), | ||
}, | ||
@@ -375,4 +283,4 @@ }, | ||
public sendButtonMessage(id: string, text: string, buttons: Array<MessengerButton> | FBButton) { | ||
return this.sendMessageToFB(id, this.exportButtonMessage(text, buttons)); | ||
public sendButtonMessage(id: string, text: string, buttons: Array<FBTypes.MessengerButton> | FBButton) { | ||
return this.sendMessageToFB(id, FBPlatform.exportButtonMessage(text, buttons, this.maxButtons)); | ||
} | ||
@@ -384,4 +292,4 @@ | ||
public exportTextMessage(text: string): MessengerMessage { | ||
const messageData: MessengerMessage = { | ||
static exportTextMessage(text: string): FBTypes.MessengerMessage { | ||
const messageData: FBTypes.MessengerMessage = { | ||
text, | ||
@@ -393,3 +301,3 @@ }; | ||
public sendTextMessage(id: string, text: string) { | ||
return this.sendMessageToFB(id, this.exportTextMessage(text)); | ||
return this.sendMessageToFB(id, FBPlatform.exportTextMessage(text)); | ||
} | ||
@@ -401,6 +309,6 @@ | ||
public exportQuickReplies(text: string, quickReplies: Array<MessengerQuickReply>): MessengerMessage { | ||
const messageData: MessengerMessage = { | ||
static exportQuickReplies(text: string, quickReplies: Array<FBTypes.MessengerQuickReply>, maxQuickReplies: number = 10): FBTypes.MessengerMessage { | ||
const messageData: FBTypes.MessengerMessage = { | ||
text, | ||
quick_replies: quickReplies.slice(0, this.maxQuickReplies), | ||
quick_replies: quickReplies.slice(0, maxQuickReplies), | ||
}; | ||
@@ -410,3 +318,3 @@ return messageData; | ||
public sendQuickReplies(id: string, text: string, quickReplies: Array<MessengerQuickReply>) { | ||
public sendQuickReplies(id: string, text: string, quickReplies: Array<FBTypes.MessengerQuickReply>) { | ||
if (quickReplies.length > this.maxQuickReplies && this.validateLimits) { | ||
@@ -416,7 +324,7 @@ throw new Error(`Quick replies limited to ${this.maxQuickReplies}, tried sending ${quickReplies.length}`); | ||
return this.sendMessageToFB(id, this.exportQuickReplies(text, quickReplies)); | ||
return this.sendMessageToFB(id, FBPlatform.exportQuickReplies(text, quickReplies, this.maxQuickReplies)); | ||
} | ||
public sendSenderAction(id: string, senderAction: SenderAction) { | ||
const payload: MessengerPayload = { | ||
public sendSenderAction(id: string, senderAction: FBTypes.SenderAction) { | ||
const payload: FBTypes.MessengerPayload = { | ||
recipient: { | ||
@@ -443,3 +351,3 @@ id: id.toString(), | ||
private sendSettingsToFB(payload: MessengerSettings) { | ||
private sendSettingsToFB(payload: FBTypes.MessengerSettings) { | ||
return this.sendToFB(payload, '/thread_settings'); | ||
@@ -449,3 +357,3 @@ } | ||
public setGetStartedPostback(payload: string) { | ||
const messengerpayload: MessengerSettings = { | ||
const Messengerpayload: FBTypes.MessengerSettings = { | ||
setting_type: 'call_to_actions', | ||
@@ -458,7 +366,7 @@ thread_state: 'new_thread', | ||
return this.sendSettingsToFB(messengerpayload); | ||
return this.sendSettingsToFB(Messengerpayload); | ||
} | ||
public setPersistentMenu(buttons: Array<MessengerButton>) { | ||
const messengerPayload: MessengerSettings = { | ||
public setPersistentMenu(buttons: Array<FBTypes.MessengerButton>) { | ||
const MessengerPayload: FBTypes.GeneralThreadSettings = { | ||
setting_type: 'call_to_actions', | ||
@@ -469,7 +377,7 @@ thread_state: 'existing_thread', | ||
return this.sendSettingsToFB(messengerPayload); | ||
return this.sendSettingsToFB(MessengerPayload); | ||
} | ||
public setGreetingText(text: string) { | ||
const messengerPayload: MessengerSettings = { | ||
const MessengerPayload: FBTypes.GeneralThreadSettings = { | ||
setting_type: 'greeting', | ||
@@ -481,7 +389,7 @@ greeting: { | ||
return this.sendSettingsToFB(messengerPayload); | ||
return this.sendSettingsToFB(MessengerPayload); | ||
} | ||
public createPostbackButton(title: string, payload: string): MessengerButton { | ||
const button: MessengerButton = { | ||
public createPostbackButton(title: string, payload: string): FBTypes.MessengerButton { | ||
const button: FBTypes.MessengerButton = { | ||
type: 'postback', | ||
@@ -494,4 +402,4 @@ title, | ||
public createWebButton(title: string, url: string): MessengerButton { | ||
const button: MessengerButton = { | ||
public createWebButton(title: string, url: string): FBTypes.MessengerButton { | ||
const button: FBTypes.MessengerButton = { | ||
type: 'web_url', | ||
@@ -504,4 +412,4 @@ title, | ||
public createQuickReply(title: string, payload: string): MessengerQuickReply { | ||
const button: MessengerQuickReply = { | ||
public createQuickReply(title: string, payload: string): FBTypes.MessengerQuickReply { | ||
const button: FBTypes.MessengerQuickReply = { | ||
content_type: 'text', | ||
@@ -514,6 +422,6 @@ title, | ||
public getUserProfile(id: string): Promise<FacebookUser> { | ||
public getUserProfile(id: string): Promise<FBTypes.FacebookUser> { | ||
return rp(`${this.FBGraphURL}/${id}?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=${this.token}`) | ||
.then((response: string) => JSON.parse(response) as FacebookUser); | ||
.then((response: string) => JSON.parse(response) as FBTypes.FacebookUser); | ||
} | ||
} |
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
36352
3
4
8
818
1
1