UNPKG

@vonage/messages

Version:

Multi-channel messaging that integrates WhatsApp, Facebook, Viber, SMS, and MMS

119 lines (118 loc) 4.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Messages = void 0; const server_client_1 = require("@vonage/server-client"); const debug_1 = __importDefault(require("debug")); const log = (0, debug_1.default)('vonage:messages'); /** * Client class to interact with the Messages API which enables users to manage * send messages through various channels programmatically. * @see {@link https://developer.nexmo.com/en/messages/overview} * @group Client * * @example * Create a standalone Messages client * * ```ts * import { Messages } from '@vonage/messages'; * * const messagesClient = new Messages({ * apiKey: VONAGE_API_KEY, * apiSecret: VONAGE_API_SECRET * }); * ``` * * @example * Create an Messages client from the Vonage client * * ```ts * import { Vonage } from '@vonage/server-client'; * * const vonage = new Vonage({ * apiKey: VONAGE_API_KEY, * apiSecret: VONAGE_API_SECRET * }); * * const messagesClient = vonage.messages; * ``` */ class Messages extends server_client_1.Client { /** * Adds authentication details to the given request based on the configured * authentication type. Handle various ways the Messages API handles auth * The Messages API handles both JWT (preferred) as well as Basic so we * cannot just set a local authType * * @param {VetchOptions} request - The request to which authentication should be added. * @return {Promise<VetchOptions>} A promise that resolves to the request with added authentication. */ async addAuthenticationToRequest(request) { log('Auth config', this.auth); this.authType = server_client_1.AuthenticationType.BASIC; if (this.auth.applicationId && this.auth.privateKey) { log('Adding JWT token to request'); this.authType = server_client_1.AuthenticationType.JWT; } if (this.auth.signature) { log('Signing the request'); this.authType = server_client_1.AuthenticationType.SIGNATURE; } return super.addAuthenticationToRequest(request); } /** * Sends a message using the Vonage API. * * @param {SendMessageParams} message - The message to be sent. * @return {Promise<MessageSuccess>} A promise that resolves to a success response with a message UUID. */ async send(message) { const data = server_client_1.Client.transformers.snakeCaseObjectKeys(message, true); if ('custom' in message) { data.custom = message.custom; } const resp = await this.sendPostRequest(`${this.config.apiHost}/v1/messages`, data); return { messageUUID: resp.data.message_uuid, }; } /** * Update the status of outbound and/or inbound messages for certain * channels. For example, you can revoke outbound messages or mark inbound * messages as read. * * Please not that this endpoint is region specifc. You will need to set the * region when you create the client. * * @example * Update the status of a WhatsApp message to "read" * ```ts * const vonage = new Vonage( * { * applicationId: myAppId, * privateKey: myPrivateKey * }, * { * apiHost: 'https://api-eu.vonage.com' * } * ) * * await vonage.messages.updateMessage(messageId, UpdateMessageStatus.READ); * ``` * * @param {string} messageId - The ID of the message to update. * @param {UpdateMessageStatus | string} status - The status to update the message to. * * @return {Promise<true>} A promise that resolves to true if the message was * updated successfully. */ async updateMessage(messageId, status) { // This endpoint returns a 202 so we don't need to parse the response await this.sendPatchRequest(`${this.config.apiHost}/v1/messages/${messageId}`, { status: status }); return true; } } exports.Messages = Messages; //# sourceMappingURL=messages.js.map