159 lines
4.4 KiB
TypeScript
159 lines
4.4 KiB
TypeScript
import {
|
|
phoneNumberOnly,
|
|
toTime,
|
|
} from 'src/modules/queue/domain/helpers/time.helper';
|
|
import { WhatsappQueue } from './entity/whatsapp-queue.entity';
|
|
import {
|
|
WHATSAPP_BUSINESS_ACCESS_TOKEN,
|
|
WHATSAPP_BUSINESS_ACCOUNT_NUMBER_ID,
|
|
WHATSAPP_BUSINESS_API_URL,
|
|
WHATSAPP_BUSINESS_QUEUE_URL,
|
|
WHATSAPP_BUSINESS_VERSION,
|
|
} from './whatsapp.constant';
|
|
import axios from 'axios';
|
|
import { Logger } from '@nestjs/common';
|
|
|
|
export class WhatsappService {
|
|
async sendMessage(data) {
|
|
const config = {
|
|
method: 'post',
|
|
url: `${WHATSAPP_BUSINESS_API_URL}/${WHATSAPP_BUSINESS_VERSION}/${WHATSAPP_BUSINESS_ACCOUNT_NUMBER_ID}/messages`,
|
|
headers: {
|
|
Authorization: `Bearer ${WHATSAPP_BUSINESS_ACCESS_TOKEN}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
data: data,
|
|
};
|
|
|
|
const response = await axios(config);
|
|
return response.data;
|
|
}
|
|
|
|
async queueRegister(data: WhatsappQueue) {
|
|
const queueUrl = `${WHATSAPP_BUSINESS_QUEUE_URL}?id=${data.id}`;
|
|
const payload = {
|
|
messaging_product: 'whatsapp',
|
|
to: phoneNumberOnly(data.phone), // recipient's phone number
|
|
type: 'template',
|
|
template: {
|
|
name: 'queue_created',
|
|
language: {
|
|
code: 'id', // language code
|
|
},
|
|
components: [
|
|
{
|
|
type: 'header',
|
|
parameters: [
|
|
{
|
|
parameter_name: 'queue_code',
|
|
type: 'text',
|
|
text: data.code, // replace with queue_code variable
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'body',
|
|
parameters: [
|
|
{
|
|
parameter_name: 'name',
|
|
type: 'text',
|
|
text: data.name, // replace with name variable
|
|
},
|
|
{
|
|
parameter_name: 'item_name',
|
|
type: 'text',
|
|
text: data.item_name, // replace with item_name variable
|
|
},
|
|
{
|
|
parameter_name: 'queue_code',
|
|
type: 'text',
|
|
text: data.code, // replace with queue_code variable
|
|
},
|
|
{
|
|
parameter_name: 'queue_time',
|
|
type: 'text',
|
|
text: toTime(data.time), // replace with queue_time variable
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'button',
|
|
sub_type: 'url',
|
|
index: '0',
|
|
parameters: [
|
|
{
|
|
type: 'text',
|
|
text: queueUrl, // replace with dynamic URL
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
await this.sendMessage(payload);
|
|
Logger.log(`Notification register for ${data.code} send to ${data.phone}`);
|
|
}
|
|
|
|
async queueProcess(data: WhatsappQueue) {
|
|
const queueUrl = `${WHATSAPP_BUSINESS_QUEUE_URL}?id=${data.id}`;
|
|
const payload = {
|
|
messaging_product: 'whatsapp',
|
|
to: data.phone, // recipient's phone number
|
|
type: 'template',
|
|
template: {
|
|
name: 'queue_process',
|
|
language: {
|
|
code: 'id', // language code
|
|
},
|
|
components: [
|
|
{
|
|
type: 'header',
|
|
parameters: [
|
|
{
|
|
parameter_name: 'queue_code',
|
|
type: 'text',
|
|
text: data.item_name, // replace with queue_code variable
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'body',
|
|
parameters: [
|
|
{
|
|
parameter_name: 'name',
|
|
type: 'text',
|
|
text: data.name, // replace with name variable
|
|
},
|
|
{
|
|
parameter_name: 'queue_code',
|
|
type: 'text',
|
|
text: data.code, // replace with queue_code variable
|
|
},
|
|
{
|
|
parameter_name: 'queue_time',
|
|
type: 'text',
|
|
text: toTime(data.time), // replace with queue_time variable
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'button',
|
|
sub_type: 'url',
|
|
index: '0',
|
|
parameters: [
|
|
{
|
|
type: 'text',
|
|
text: queueUrl, // replace with dynamic URL
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
await this.sendMessage(payload);
|
|
Logger.log(`Notification process for ${data.code} send to ${data.phone}`);
|
|
}
|
|
}
|