UNPKG

@zenvia/sdk

Version:

This SDK for [Node.js](https://nodejs.org/) was created based on the [Zenvia](https://www.zenvia.com/) [API](https://zenvia.github.io/zenvia-openapi-spec/).

57 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractChannel = void 0; const logger_1 = require("../../utils/logger"); const request = require("../../utils/request"); /** * Implementation of base channel. */ class AbstractChannel { constructor(token, channel, loggerInstance, options) { this.options = options; this.token = token; this.channel = channel; this.logger = new logger_1.Logger(loggerInstance); } /** * This method sends the message to the channel. * * @param from The sender identifier of the message. * @param to The recipient identifier of the message. * @param contents An array of [[IContent]] object that will be sent. * @returns A promise that resolves to an [[IMessage]] object. */ async sendMessage(from, to, ...contents) { contents.forEach(content => this.contentSupportValidation(content)); const message = this.createMessage(from, to, contents); this.logger.debug(`Sending message to ${to} on channel ${this.channel}`); return this.request(message); } /** * This method creates a message. * * @param from The sender identifier of the message. * @param to The recipient identifier of the message. * @param contents An array of [[IContent]] object that will be sent. * @returns An [[IMessageRequest]] object. */ createMessage(from, to, contents) { return { from, to, contents, }; } /** * This method requests to the endpoint. * * @param message An [[IMessageRequest]] object. * @returns A promise that resolves to an [[IMessage]] object. */ async request(message) { const path = `/v2/channels/${this.channel}/messages`; return request.post(this.token, path, message, this.logger, this.options); } } exports.AbstractChannel = AbstractChannel; //# sourceMappingURL=abstract-channel.js.map