@ebenos/messenger-adapter
Advanced tools
Comparing version 3.2.7 to 3.2.8
import { GenericAdapter } from '@ebenos/framework'; | ||
import { UserModel } from '@ebenos/framework'; | ||
import { SenderFunction, IMessage, IBaseFbMessageOptions } from './sender'; | ||
import MessengerUser from './MessengerUser'; | ||
import { SendAPIBody, UserDataFields } from './interfaces/messengerAPI'; | ||
import { UserDataFields } from './interfaces/messengerAPI'; | ||
export interface MessengerWebhookOptions<T extends MessengerUser> { | ||
@@ -18,3 +19,2 @@ webhookKey?: string; | ||
private pageId; | ||
sender: (...params: any[]) => Promise<void>; | ||
startsTyping: (id: string) => Promise<void>; | ||
@@ -25,3 +25,4 @@ stopsTyping: (id: string) => Promise<void>; | ||
handover: (id: string) => Promise<void>; | ||
constructor(options: MessengerWebhookOptions<T>, sendFunction?: (body: SendAPIBody, ...params: any[]) => Promise<any>); | ||
sender: (messages: Array<IMessage<IBaseFbMessageOptions>>, type: 'ORDERED' | 'UNORDERED') => Promise<void>; | ||
constructor(options: MessengerWebhookOptions<T>, sendFunction?: SenderFunction<IBaseFbMessageOptions>); | ||
initWebhook(): void; | ||
@@ -28,0 +29,0 @@ private validationEndpoint; |
@@ -10,13 +10,18 @@ /** | ||
*/ | ||
import { ISerializable } from '@ebenos/framework'; | ||
import { IBaseMessage, IBaseMessageOptions } from '@ebenos/framework'; | ||
import { SendAPIBody, UserDataFields } from './interfaces/messengerAPI'; | ||
import { MessagingOptions } from './interfaces/messengerAPI'; | ||
export declare type IBaseFbMessageOptions = MessagingOptions & IBaseMessageOptions; | ||
export declare type IMessage<MessageOptions extends IBaseFbMessageOptions> = IBaseMessage<MessageOptions>; | ||
export declare type SendedMessage<T extends IBaseFbMessageOptions> = { | ||
body: SendAPIBody; | ||
token: string; | ||
} & Partial<Omit<T, 'tag' | 'notification_type' | 'type'>>; | ||
export declare type SenderFunction<T extends IBaseFbMessageOptions> = (messages: Array<SendedMessage<T>>, type: 'ORDERED' | 'UNORDERED') => Promise<any>; | ||
/** | ||
* Creates a sender function | ||
*/ | ||
export declare function senderFactory(pageToken: string, call?: (body: SendAPIBody, ...params: any[]) => Promise<any>): { | ||
send: <T extends MessagingOptions & { | ||
delay: number; | ||
}>(id: string, message: ISerializable, options?: Partial<T>) => Promise<any>; | ||
senderAction: <T_1>(id: string, action: string, other?: Partial<T_1>) => Promise<any>; | ||
export declare function senderFactory<T extends IBaseFbMessageOptions>(pageToken: string, call?: SenderFunction<T>): { | ||
send: (messages: IMessage<T>[], orderType: "ORDERED" | "UNORDERED") => Promise<any>; | ||
senderAction: (id: string, action: string, other?: Partial<T>) => Promise<any>; | ||
getUserData: (id: string, fields: UserDataFields[]) => Promise<any>; | ||
@@ -23,0 +28,0 @@ handover: (id: string, targetAppId?: string, metadata?: string | undefined) => Promise<any>; |
@@ -20,11 +20,14 @@ "use strict"; | ||
const qs = `access_token=${encodeURIComponent(pageToken)}`; | ||
/** | ||
* Sends a message to the user with the id | ||
*/ | ||
function send(id, message, options = {}) { | ||
const { tag = null, notification_type = 'REGULAR', type = 'RESPONSE' } = options, other = __rest(options, ["tag", "notification_type", "type"]); | ||
if (!id) { | ||
function createMessageBody(message) { | ||
if (message.options === undefined) { | ||
throw new Error("Options can't be undefined!"); | ||
} | ||
if (message.message === undefined) { | ||
throw new Error("Message can't be undefined!"); | ||
} | ||
const _a = message.options, { tag = null, notification_type = 'REGULAR', type = 'RESPONSE' } = _a, other = __rest(_a, ["tag", "notification_type", "type"]); | ||
if (!message.id) { | ||
throw new Error('[Error] Send: No user id is specified!'); | ||
} | ||
if (!message) { | ||
if (!message.message) { | ||
throw new Error('[Error] No message passed!'); | ||
@@ -37,9 +40,44 @@ } | ||
const body = { | ||
recipient: { id }, | ||
message: message.serialize(), | ||
recipient: { id: message.id }, | ||
message: message.message.serialize(), | ||
notification_type, | ||
messaging_type | ||
messaging_type, | ||
tag | ||
}; | ||
return Object.assign({ body, token: qs }, other); | ||
} | ||
/** | ||
* Sends a message to the user with the id | ||
*/ | ||
function send(messages, orderType) { | ||
const bodies = messages.map((_a) => { | ||
var { type: messageType } = _a, other = __rest(_a, ["type"]); | ||
switch (messageType) { | ||
case 'message': | ||
return createMessageBody(other); | ||
case 'typing_on': | ||
if (other.options === undefined) { | ||
throw new Error("Options can't be undefined!"); | ||
} | ||
const _b = other.options, { notification_type, tag, type } = _b, options = __rest(_b, ["notification_type", "tag", "type"]); | ||
return Object.assign({ body: { | ||
recipient: { id: other.id }, | ||
sender_action: messageType | ||
}, token: qs }, options); | ||
case 'typing_off': | ||
return Object.assign({ body: { | ||
recipient: { id: other.id }, | ||
sender_action: messageType | ||
}, token: qs }, options); | ||
case 'mark_seen': | ||
return Object.assign({ body: { | ||
recipient: { id: other.id }, | ||
sender_action: messageType | ||
}, token: qs }, options); | ||
default: | ||
throw new Error('Unknown type!'); | ||
} | ||
}); | ||
// TODO implement logger in here. | ||
return call(body, qs, other); | ||
return call(bodies, orderType); | ||
} | ||
@@ -51,3 +89,3 @@ function senderAction(id, action, other = {}) { | ||
}; | ||
return call(body, qs, other); | ||
return call([Object.assign({ body, token: qs }, other)], 'ORDERED'); | ||
} | ||
@@ -54,0 +92,0 @@ function getUserData(id, fields) { |
import { SendAPIBody, UserDataFields } from './adapter/interfaces/messengerAPI'; | ||
export declare function sendAPI(body: SendAPIBody, qs: string, other?: { | ||
delay: number; | ||
}): Promise<any>; | ||
export declare function sendAPI(messages: Array<{ | ||
body: SendAPIBody; | ||
token: string; | ||
delay?: number; | ||
}>, type: 'ORDERED' | 'UNORDERED'): Promise<any>; | ||
export declare function getUserDataCall(id: string, fields: UserDataFields[], qs: string): Promise<any>; | ||
export declare function passThreadControl(id: string, qs: string, targetAppId?: string, metadata?: string): Promise<any>; | ||
//# sourceMappingURL=messengerApi.d.ts.map |
@@ -21,25 +21,28 @@ "use strict"; | ||
} | ||
function sendAPI(body, qs, other = { delay: 0 }) { | ||
function sendAPI(messages, type) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (other.delay > 0) { | ||
yield wait(other.delay); | ||
} | ||
try { | ||
const rsp = yield node_fetch_1.default(`${fbApiUrl}/me/messages?${qs}`, { | ||
body: JSON.stringify(body), | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
for (const message of messages) { | ||
const { delay = 0 } = message; | ||
if (delay > 0) { | ||
yield wait(delay); | ||
} | ||
try { | ||
const rsp = yield node_fetch_1.default(`${fbApiUrl}/me/messages?${message.token}`, { | ||
body: JSON.stringify(message.body), | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
const json = yield rsp.json(); | ||
if (json.error && json.error.message) { | ||
throw new Error(json.error.message); | ||
} | ||
}); | ||
const json = yield rsp.json(); | ||
if (json.error && json.error.message) { | ||
throw new Error(json.error.message); | ||
return json; | ||
} | ||
return json; | ||
catch (err) { | ||
// TODO: Handle errors | ||
throw err; | ||
} | ||
} | ||
catch (err) { | ||
// TODO: Handle errors | ||
throw err; | ||
} | ||
}); | ||
@@ -46,0 +49,0 @@ } |
{ | ||
"name": "@ebenos/messenger-adapter", | ||
"version": "3.2.7", | ||
"version": "3.2.8", | ||
"description": "Facebook SendAPI Library for the Ebony framework.", | ||
@@ -40,3 +40,3 @@ "main": "./build/index.js", | ||
"devDependencies": { | ||
"@ebenos/framework": "^3.2.7", | ||
"@ebenos/framework": "^3.2.8", | ||
"typescript": "^3.8.3" | ||
@@ -50,3 +50,3 @@ }, | ||
], | ||
"gitHead": "1e9239885960ea3b8acb85fe6e0031a5ae6595f2" | ||
"gitHead": "8370fb942c51f1dd5ca3303a72390ef9f770024f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90294
1600