whatsapp-api-client
Version:
Whatsapp Business API Official Client
62 lines (61 loc) • 2.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendMessage = exports.sendMessageWithTemplate = void 0;
/**
* Send a message with a template to a WhatsApp number.
*/
const sendMessageWithTemplate = (options) => (props) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield options.axios.post(`/${props.phoneNumberId}/messages`, {
messaging_product: "whatsapp",
to: props.to,
type: "template",
template: {
name: props.templateName,
language: {
code: props.templateLanguageCode,
},
components: props.templateParameters
? props.templateParameters.map((component) => ({
type: component.type,
sub_type: component.sub_type,
index: component.index,
parameters: component.parameters.map((parameter) => ({
type: parameter.type,
text: parameter.text,
payload: parameter.payload,
})),
}))
: undefined,
},
});
return response.data;
});
exports.sendMessageWithTemplate = sendMessageWithTemplate;
/**
* Send a message - only work if receiver has already messaged the sender first for last 24 hours
*/
const sendMessage = (options) => (props) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield options.axios.post(`/${props.phoneNumberId}/messages`, {
messaging_product: "whatsapp",
to: props.to,
text: {
body: props.text,
},
context: props.context_message_id
? {
message_id: props.context_message_id,
}
: undefined,
});
return response.data;
});
exports.sendMessage = sendMessage;