2024-05-07 14:16:01 +02:00
|
|
|
import * as Hapi from "@hapi/hapi";
|
2024-05-15 14:39:33 +02:00
|
|
|
import Toys from "@hapipal/toys";
|
|
|
|
|
import WhatsappService from "./service";
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
const withDefaults = Toys.withRouteDefaults({
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
2024-05-15 14:39:33 +02:00
|
|
|
cors: true,
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
const getService = (request: Hapi.Request): WhatsappService => {
|
|
|
|
|
const { whatsappService } = request.services();
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return whatsappService as WhatsappService;
|
|
|
|
|
};
|
2024-05-07 14:16:01 +02:00
|
|
|
|
|
|
|
|
interface MessageRequest {
|
|
|
|
|
phoneNumber: string;
|
|
|
|
|
message: string;
|
2025-11-21 14:55:28 +01:00
|
|
|
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
export const SendMessageRoute = withDefaults({
|
2024-05-07 14:16:01 +02:00
|
|
|
method: "post",
|
2024-05-15 14:39:33 +02:00
|
|
|
path: "/api/bots/{id}/send",
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
|
|
|
|
description: "Send a message",
|
|
|
|
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
2024-05-15 14:39:33 +02:00
|
|
|
const { id } = request.params;
|
2025-11-21 14:55:28 +01:00
|
|
|
const { phoneNumber, message, attachments } =
|
|
|
|
|
request.payload as MessageRequest;
|
2024-05-15 14:39:33 +02:00
|
|
|
const whatsappService = getService(request);
|
2025-11-21 14:55:28 +01:00
|
|
|
await whatsappService.send(
|
|
|
|
|
id,
|
|
|
|
|
phoneNumber,
|
|
|
|
|
message as string,
|
|
|
|
|
attachments,
|
|
|
|
|
);
|
|
|
|
|
request.logger.info(
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
attachmentCount: attachments?.length || 0,
|
|
|
|
|
},
|
|
|
|
|
"Sent a message at %s",
|
|
|
|
|
new Date().toISOString(),
|
|
|
|
|
);
|
2024-05-15 14:39:33 +02:00
|
|
|
|
|
|
|
|
return _h
|
|
|
|
|
.response({
|
|
|
|
|
result: {
|
|
|
|
|
recipient: phoneNumber,
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
source: id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.code(200);
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
export const ReceiveMessageRoute = withDefaults({
|
2024-05-07 14:16:01 +02:00
|
|
|
method: "get",
|
2024-05-15 14:39:33 +02:00
|
|
|
path: "/api/bots/{id}/receive",
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
|
|
|
|
description: "Receive messages",
|
|
|
|
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
2024-05-15 14:39:33 +02:00
|
|
|
const { id } = request.params;
|
|
|
|
|
const whatsappService = getService(request);
|
|
|
|
|
const date = new Date();
|
|
|
|
|
const twoDaysAgo = new Date(date.getTime());
|
|
|
|
|
twoDaysAgo.setDate(date.getDate() - 2);
|
2025-11-21 14:55:28 +01:00
|
|
|
request.logger.info({ id }, "Received messages at %s", new Date().toISOString());
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return whatsappService.receive(id, twoDaysAgo);
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
export const RegisterBotRoute = withDefaults({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/api/bots/{id}/register",
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
|
|
|
|
description: "Register a bot",
|
|
|
|
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
|
|
|
|
const { id } = request.params;
|
2024-05-15 14:39:33 +02:00
|
|
|
const whatsappService = getService(request);
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-17 09:20:00 +02:00
|
|
|
await whatsappService.register(id);
|
|
|
|
|
/*
|
|
|
|
|
, (error: string) => {
|
2024-05-15 14:39:33 +02:00
|
|
|
if (error) {
|
|
|
|
|
return _h.response(error).code(500);
|
|
|
|
|
}
|
|
|
|
|
request.logger.info({ id }, "Register bot at %s", new Date());
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return _h.response().code(200);
|
|
|
|
|
});
|
2024-05-17 09:20:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
return _h.response().code(200);
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
export const UnverifyBotRoute = withDefaults({
|
2024-05-07 14:16:01 +02:00
|
|
|
method: "post",
|
2024-05-15 14:39:33 +02:00
|
|
|
path: "/api/bots/{id}/unverify",
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
|
|
|
|
description: "Unverify bot",
|
|
|
|
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
|
|
|
|
const { id } = request.params;
|
2024-05-15 14:39:33 +02:00
|
|
|
const whatsappService = getService(request);
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return whatsappService.unverify(id);
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
export const GetBotRoute = withDefaults({
|
2024-05-07 14:16:01 +02:00
|
|
|
method: "get",
|
2024-05-15 14:39:33 +02:00
|
|
|
path: "/api/bots/{id}",
|
2024-05-07 14:16:01 +02:00
|
|
|
options: {
|
2024-05-15 14:39:33 +02:00
|
|
|
description: "Get bot info",
|
2024-05-07 14:16:01 +02:00
|
|
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
|
|
|
|
const { id } = request.params;
|
2024-05-15 14:39:33 +02:00
|
|
|
const whatsappService = getService(request);
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return whatsappService.getBot(id);
|
2024-05-07 14:16:01 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|