UNPKG

n8n-nodes-walichat

Version:

n8n plugin for WaliChat

122 lines (121 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLabels = exports.getDepartments = exports.getTeamAgents = exports.getFiles = exports.getGroups = exports.getDevices = void 0; const request_1 = require("../request"); async function query(url, apiKey) { const opts = { url, method: 'GET' }; const { data } = await (0, request_1.rawRequest)(opts, apiKey); return data; } async function getDevices() { // Retrieve the credentials object declared on the node const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const apiKey = credentials.walichatApiKey; const data = await query(`/devices?size=100`, apiKey); return data.map((device) => ({ name: `${device.alias} (${device.phone})`, value: device.id, })); } exports.getDevices = getDevices; async function getGroups() { // Retrieve the credentials object declared on the node const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const device = this.getNodeParameter('device', 0); if (!device) { throw new Error('Please select WhatsApp device selected'); } const target = this.getNodeParameter('target'); const apiKey = credentials.walichatApiKey; const kind = target === 'channel' ? 'channel' : 'group'; const data = await query(`/devices/${device}/groups?size=500&kind=${kind}`, apiKey); return data.filter((group) => { return group.isReadOnly !== true; }).map((group) => ({ name: `${group.name} (${group.wid})`, value: group.wid, })); } exports.getGroups = getGroups; async function getFiles() { // Retrieve the credentials object declared on the node const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const apiKey = credentials.walichatApiKey; const search = this.getNodeParameter('file', 0); let url = `/files?size=100`; if (search) { url += `&search=${search}`; } const data = await query(url, apiKey); return data.map((file) => ({ name: file.filename, value: file.id, })); } exports.getFiles = getFiles; async function getTeamAgents() { // Retrieve the credentials object declared on the node const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const device = this.getNodeParameter('device', 0); if (!device) { throw new Error('Please select WhatsApp device selected'); } const apiKey = credentials.walichatApiKey; const data = await query(`/devices/${device}/team?size=100`, apiKey); return data.map((agent) => ({ name: agent.displayName + ' - ' + agent.email, value: agent.id, })); } exports.getTeamAgents = getTeamAgents; async function getDepartments() { const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const device = this.getNodeParameter('device', 0); if (!device) { throw new Error('Please select WhatsApp device selected'); } const apiKey = credentials.walichatApiKey; const data = await query(`/devices/${device}/departments`, apiKey); return data.map((department) => ({ name: department.name, value: department.id })); } exports.getDepartments = getDepartments; async function getLabels() { // Implement the call // Retrieve the credentials object declared on the node const credentials = await this.getCredentials('walichatApiKey'); if (!credentials || !credentials.walichatApiKey) { throw new Error('No WaliChat API Key credentials found!'); } const device = this.getNodeParameter('device', 0); if (!device) { throw new Error('Please select WhatsApp device selected'); } const apiKey = credentials.walichatApiKey; const data = await query(`/devices/${device}/labels?size=100`, apiKey); return data.filter((label) => label.scope === 'chat').map((label) => ({ name: label.name, value: label.name })); } exports.getLabels = getLabels;