n8n-walichat
Version:
n8n plugin for WaliChat
62 lines (61 loc) • 2.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFiles = exports.getGroups = exports.getDevices = void 0;
const axios_1 = __importDefault(require("axios"));
async function getDevices() {
// Retrieve the credentials object declared on the node
const credentials = await this.getCredentials('WaliChatApiKey');
if (!credentials || !credentials.apiKey) {
throw new Error('No WaliChat API Key credentials found!');
}
const apiKey = credentials.apiKey;
const response = await axios_1.default.get(`https://api.wali.chat/v1/devices?token=${apiKey}`);
return response.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.apiKey) {
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.apiKey;
const response = await axios_1.default.get(`https://api.wali.chat/v1/devices/${device}/groups?size=500&kind=${target === 'channel' ? 'channel' : 'group'}&token=${apiKey}`);
return response.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.apiKey) {
throw new Error('No WaliChat API Key credentials found!');
}
const apiKey = credentials.apiKey;
const search = this.getNodeParameter('file', 0);
let url = `https://api.wali.chat/v1/files?size=100&token=${apiKey}`;
if (search) {
url += `&search=${search}`;
}
const response = await axios_1.default.get(url);
return response.data.map((file) => ({
name: file.filename,
value: file.id,
}));
}
exports.getFiles = getFiles;