n8n-nodes-whatsapp
Version:
Nodes to trigger workflows from whatsapp events or send messages (using whatsapp web).
46 lines • 1.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendMessageToPhoneNumber = exports.getChatByPhoneNumber = void 0;
const debug = require('debug')('n8n-nodes-whatsapp');
const client_store_1 = __importDefault(require("./client-store"));
const util_1 = require("./util");
const getClient = async (credential) => {
if (await client_store_1.default.has(credential)) {
return client_store_1.default.get(credential);
}
return client_store_1.default.create(credential, true);
};
const getChatByPhoneNumber = async (credential, phoneNumber) => {
const client = await getClient(credential);
const chats = await client.getChats();
debug(`sent message to ${(0, util_1.preparePhoneNumber)(phoneNumber)}`);
for (const chat of chats) {
if (chat.isGroup) {
continue;
}
const contact = await chat.getContact();
if ((0, util_1.preparePhoneNumber)(contact.number) == (0, util_1.preparePhoneNumber)(phoneNumber)) {
debug(`Found contact for phone number '${phoneNumber}'!`, contact, chat);
return chat;
}
}
debug(`Chat for phone number '${phoneNumber}' not found!`);
return null;
};
exports.getChatByPhoneNumber = getChatByPhoneNumber;
const sendMessageToPhoneNumber = async (credential, recipientPhoneNumber, message) => {
const chat = await (0, exports.getChatByPhoneNumber)(credential, recipientPhoneNumber);
if (!chat) {
throw new Error(`Could not find chat for phone number '${recipientPhoneNumber}'`);
}
debug(chat);
await chat.sendStateTyping();
const msg = await chat.sendMessage(message);
await (0, util_1.wait)(2000);
return (0, util_1.prepareOutputData)(msg);
};
exports.sendMessageToPhoneNumber = sendMessageToPhoneNumber;
//# sourceMappingURL=client.js.map