trysoftwacloudapi
Version:
TrySoft Whatsapp Cloud Api Wrapper
222 lines • 8.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBot = void 0;
const isURL_1 = __importDefault(require("validator/lib/isURL"));
const pubsub_js_1 = __importDefault(require("pubsub-js"));
const sendRequestHelper_1 = require("./sendRequestHelper");
const startExpressServer_1 = require("./startExpressServer");
const payloadBase = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
};
const createBot = (fromPhoneNumberId, accessToken, opts) => {
let expressServer;
const sendRequest = (0, sendRequestHelper_1.sendRequestHelper)(fromPhoneNumberId, accessToken, (opts === null || opts === void 0 ? void 0 : opts.version) ? opts.version : process.env.VERSION);
const sendMedia = (0, sendRequestHelper_1.getMediaDownload)(fromPhoneNumberId, accessToken, opts === null || opts === void 0 ? void 0 : opts.version);
const getMediaPayload = (urlOrObjectId, options) => ({
...((0, isURL_1.default)(urlOrObjectId) ? { link: urlOrObjectId } : { id: urlOrObjectId }),
caption: options === null || options === void 0 ? void 0 : options.caption,
filename: options === null || options === void 0 ? void 0 : options.filename,
});
return {
startExpressServer: async (options) => {
if (!expressServer) {
expressServer = await (0, startExpressServer_1.startExpressServer)(options);
}
return expressServer;
},
on: (event, cb) => {
const token = pubsub_js_1.default.subscribe(`bot-${fromPhoneNumberId}-${event}`, function (_, data) {
cb(data);
});
return token;
},
unsubscribe: (token) => pubsub_js_1.default.unsubscribe(token),
markRead: (id) => sendRequest({
messaging_product: 'whatsapp',
status: 'read',
message_id: id,
typing_indicator: {
"type": "text"
}
}),
getMediaDownload: (id, save_path) => sendMedia(id, save_path),
sendText: (to, text, options) => sendRequest({
...payloadBase,
to,
type: 'text',
context: options === null || options === void 0 ? void 0 : options.context,
text: {
body: text,
preview_url: options === null || options === void 0 ? void 0 : options.preview_url,
},
}),
sendMessage(to, text, options) {
return this.sendText(to, text, options);
},
sendImage: (to, urlOrObjectId, options) => sendRequest({
...payloadBase,
to,
type: 'image',
context: options === null || options === void 0 ? void 0 : options.context,
image: getMediaPayload(urlOrObjectId, options),
}),
sendDocument: (to, urlOrObjectId, options) => sendRequest({
...payloadBase,
to,
type: 'document',
context: options === null || options === void 0 ? void 0 : options.context,
document: getMediaPayload(urlOrObjectId, options),
}),
sendAudio: (to, urlOrObjectId) => sendRequest({
...payloadBase,
to,
type: 'audio',
audio: getMediaPayload(urlOrObjectId),
}),
sendVideo: (to, urlOrObjectId, options) => sendRequest({
...payloadBase,
to,
type: 'video',
context: options === null || options === void 0 ? void 0 : options.context,
video: getMediaPayload(urlOrObjectId, options),
}),
sendSticker: (to, urlOrObjectId) => sendRequest({
...payloadBase,
to,
type: 'sticker',
sticker: getMediaPayload(urlOrObjectId),
}),
sendLocation: (to, latitude, longitude, options) => sendRequest({
...payloadBase,
to,
type: 'location',
context: options === null || options === void 0 ? void 0 : options.context,
location: {
latitude,
longitude,
name: options === null || options === void 0 ? void 0 : options.name,
address: options === null || options === void 0 ? void 0 : options.address,
},
}),
sendTemplate: (to, name, languageCode, components) => sendRequest({
...payloadBase,
to,
type: 'template',
template: {
name,
language: {
code: languageCode,
},
components,
},
}),
sendContacts: (to, contacts) => sendRequest({
...payloadBase,
to,
type: 'contacts',
contacts,
}),
sendReplyButtons: (to, bodyText, buttons, options) => sendRequest({
...payloadBase,
to,
type: 'interactive',
context: options === null || options === void 0 ? void 0 : options.context,
interactive: {
body: {
text: bodyText,
},
...((options === null || options === void 0 ? void 0 : options.footerText)
? {
footer: { text: options === null || options === void 0 ? void 0 : options.footerText },
}
: {}),
header: options === null || options === void 0 ? void 0 : options.header,
type: 'button',
action: {
buttons: Object.entries(buttons).map(([key, value]) => ({
type: 'reply',
reply: {
title: value,
id: key,
},
})),
},
},
}),
sendList: (to, buttonName, bodyText, sections, options) => sendRequest({
...payloadBase,
to,
type: 'interactive',
context: options === null || options === void 0 ? void 0 : options.context,
interactive: {
body: {
text: bodyText,
},
...((options === null || options === void 0 ? void 0 : options.footerText)
? {
footer: { text: options === null || options === void 0 ? void 0 : options.footerText },
}
: {}),
header: options === null || options === void 0 ? void 0 : options.header,
type: 'list',
action: {
button: buttonName,
sections: Object.entries(sections).map(([key, value]) => ({
title: key,
rows: value,
})),
},
},
}),
sendFlow: (to, bodyText, buttonName, options) => sendRequest({
...payloadBase,
to,
type: 'interactive',
context: options === null || options === void 0 ? void 0 : options.context,
interactive: {
body: {
text: bodyText,
},
...((options === null || options === void 0 ? void 0 : options.footerText)
? {
footer: { text: options === null || options === void 0 ? void 0 : options.footerText },
}
: {}),
header: options === null || options === void 0 ? void 0 : options.header,
type: 'flow',
action: {
name: "flow",
parameters: {
flow_message_version: "3",
flow_token: options.flow_token,
flow_id: options.flow_id,
flow_cta: buttonName,
flow_action: "navigate",
flow_action_payload: options.payload
}
},
},
}),
sendLocationRequest: (to, bodyText, options) => sendRequest({
...payloadBase,
to,
type: 'interactive',
context: options === null || options === void 0 ? void 0 : options.context,
interactive: {
type: "location_request_message",
body: {
text: bodyText,
},
action: {
name: "send_location"
}
},
}),
};
};
exports.createBot = createBot;
//# sourceMappingURL=createBot.js.map