UNPKG

@codebucket/whatsapp

Version:

A reusable WhatsApp Business API client with template and non-template support, webhook handling, pluggable storage, and text sanitization

55 lines (54 loc) 1.61 kB
"use strict"; // src/mysql-store.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.MySQLMessageStore = void 0; class MySQLMessageStore { constructor(pool) { this.pool = pool; } async saveIncomingMessage(accountId, msg) { const sql = ` INSERT INTO messages (account_id, message_id, \`from\`, \`to\`, direction, timestamp, content, raw) VALUES (?, ?, ?, ?, 'in', FROM_UNIXTIME(?), ?, ?) `; const content = msg.text?.body || JSON.stringify(msg); await this.pool.execute(sql, [ accountId, msg.id, msg.from, msg.to, msg.timestamp, content, JSON.stringify(msg) ]); } async saveOutgoingMessage(accountId, opts, response) { const sql = ` INSERT INTO messages (account_id, \`to\`, direction, timestamp, content, raw) VALUES (?, ?, 'out', NOW(), ?, ?) `; await this.pool.execute(sql, [ accountId, opts.to, JSON.stringify(opts.messagePayload), JSON.stringify(response) ]); } async saveMessageStatus(accountId, status) { const sql = ` INSERT INTO message_statuses (account_id, message_id, status, timestamp, raw) VALUES (?, ?, ?, FROM_UNIXTIME(?), ?) `; await this.pool.execute(sql, [ accountId, status.id, status.status, status.timestamp, JSON.stringify(status) ]); } } exports.MySQLMessageStore = MySQLMessageStore;