Bridge whatsapp simplification

This commit is contained in:
Darren Clarke 2024-05-15 14:39:33 +02:00
parent 6305a8b0bc
commit f6dc60eb08
8 changed files with 142 additions and 333 deletions

View file

@ -1,217 +1,112 @@
import * as Hapi from "@hapi/hapi";
import Boom from "@hapi/boom";
import * as Helpers from "./helpers.js";
import Toys from "@hapipal/toys";
import WhatsappService from "./service";
export const GetAllWhatsappBotsRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots",
const withDefaults = Toys.withRouteDefaults({
options: {
description: "Get all bots",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const whatsappService = request.services("whatsapp");
const bots = await whatsappService.findAll();
if (bots) {
// @ts-ignore
request.logger.info({ bots }, "Retrieved bot(s) at %s", new Date());
return { bots };
}
return _h.response().code(204);
},
cors: true,
},
});
export const GetBotsRoute = Helpers.noAuth({
method: "get",
path: "/api/whatsapp/bots/{token}",
options: {
description: "Get one bot",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { token } = request.params;
const whatsappService = request.services("whatsapp");
const getService = (request: Hapi.Request): WhatsappService => {
const { whatsappService } = request.services();
const bot = await whatsappService.findByToken(token);
if (bot) {
// @ts-ignore
request.logger.info({ bot }, "Retrieved bot(s) at %s", new Date());
return bot;
}
throw Boom.notFound("Bot not found");
},
},
});
return whatsappService as WhatsappService;
};
interface MessageRequest {
phoneNumber: string;
message: string;
}
export const SendBotRoute = Helpers.noAuth({
export const SendMessageRoute = withDefaults({
method: "post",
path: "/api/whatsapp/bots/{token}/send",
path: "/api/bots/{id}/send",
options: {
description: "Send a message",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { token } = request.params;
const { id } = request.params;
const { phoneNumber, message } = request.payload as MessageRequest;
const whatsappService = request.services("whatsapp");
const whatsappService = getService(request);
await whatsappService.send(id, phoneNumber, message as string);
request.logger.info({ id }, "Sent a message at %s", new Date());
const bot = await whatsappService.findByToken(token);
if (bot) {
// @ts-ignore
request.logger.info({ bot }, "Sent a message at %s", new Date());
await whatsappService.send(bot, phoneNumber, message as string);
return _h
.response({
result: {
recipient: phoneNumber,
timestamp: new Date().toISOString(),
source: bot.phoneNumber,
},
})
.code(200); // temp
}
throw Boom.notFound("Bot not found");
return _h
.response({
result: {
recipient: phoneNumber,
timestamp: new Date().toISOString(),
source: id,
},
})
.code(200);
},
},
});
export const ReceiveBotRoute = Helpers.withDefaults({
export const ReceiveMessageRoute = withDefaults({
method: "get",
path: "/api/whatsapp/bots/{token}/receive",
path: "/api/bots/{id}/receive",
options: {
description: "Receive messages",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { token } = request.params;
const whatsappService = request.services("whatsapp");
const { id } = request.params;
const whatsappService = getService(request);
const date = new Date();
const twoDaysAgo = new Date(date.getTime());
twoDaysAgo.setDate(date.getDate() - 2);
request.logger.info({ id }, "Received messages at %s", new Date());
const bot = await whatsappService.findByToken(token);
if (bot) {
// @ts-ignore
request.logger.info({ bot }, "Received messages at %s", new Date());
// temp
const date = new Date();
const twoDaysAgo = new Date(date.getTime());
twoDaysAgo.setDate(date.getDate() - 2);
return whatsappService.receive(bot, twoDaysAgo);
}
throw Boom.notFound("Bot not found");
return whatsappService.receive(id, twoDaysAgo);
},
},
});
export const RegisterBotRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots/{id}/register",
export const RegisterBotRoute = withDefaults({
method: "post",
path: "/api/bots/{id}/register",
options: {
description: "Register a bot",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params;
const whatsappService = request.services("whatsapp");
const whatsappService = getService(request);
const bot = await whatsappService.findById(id);
await whatsappService.register(id, (error: string) => {
if (error) {
return _h.response(error).code(500);
}
request.logger.info({ id }, "Register bot at %s", new Date());
if (bot) {
await whatsappService.register(bot, (error: string) => {
if (error) {
return _h.response(error).code(500);
}
// @ts-ignore
request.logger.info({ bot }, "Register bot at %s", new Date());
return _h.response().code(200);
});
}
throw Boom.notFound("Bot not found");
return _h.response().code(200);
});
},
},
});
export const UnverifyBotRoute = Helpers.withDefaults({
export const UnverifyBotRoute = withDefaults({
method: "post",
path: "/api/whatsapp/bots/{id}/unverify",
path: "/api/bots/{id}/unverify",
options: {
description: "Unverify bot",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params;
const whatsappService = request.services("whatsapp");
const whatsappService = getService(request);
const bot = await whatsappService.findById(id);
if (bot) {
return whatsappService.unverify(bot);
}
throw Boom.notFound("Bot not found");
return whatsappService.unverify(id);
},
},
});
export const RefreshBotRoute = Helpers.withDefaults({
export const GetBotRoute = withDefaults({
method: "get",
path: "/api/whatsapp/bots/{id}/refresh",
path: "/api/bots/{id}",
options: {
description: "Refresh messages",
description: "Get bot info",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params;
const whatsappService = request.services("whatsapp");
const whatsappService = getService(request);
const bot = await whatsappService.findById(id);
if (bot) {
// @ts-ignore
request.logger.info({ bot }, "Refreshed messages at %s", new Date());
// await whatsappService.refresh(bot);
return;
}
throw Boom.notFound("Bot not found");
},
},
});
interface BotRequest {
phoneNumber: string;
description: string;
}
export const CreateBotRoute = Helpers.withDefaults({
method: "post",
path: "/api/whatsapp/bots",
options: {
description: "Register a bot",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { phoneNumber, description } = request.payload as BotRequest;
const whatsappService = request.services("whatsapp");
console.log("request.auth.credentials:", request.auth.credentials);
const bot = await whatsappService.create(
phoneNumber,
description,
request.auth.credentials.email as string,
);
if (bot) {
// @ts-ignore
request.logger.info({ bot }, "Register bot at %s", new Date());
return bot;
}
throw Boom.notFound("Bot not found");
return whatsappService.getBot(id);
},
},
});