link-stack/apps/metamigo-api/src/app/routes/whatsapp/index.ts

217 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-02-13 12:41:30 +00:00
import * as Hapi from "@hapi/hapi";
import * as Helpers from "../helpers";
import Boom from "boom";
export const GetAllWhatsappBotsRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots",
options: {
description: "Get all bots",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bots = await whatsappService.findAll();
if (bots) {
// with the pino logger the first arg is an object of data to log
// the second arg is a message
// all other args are formated args for the msg
request.logger.info({ bots }, "Retrieved bot(s) at %s", new Date());
return { bots };
}
return _h.response().code(204);
},
},
});
export const GetBotsRoute = Helpers.noAuth({
method: "get",
path: "/api/whatsapp/bots/{token}",
options: {
description: "Get one bot",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { token } = request.params;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bot = await whatsappService.findByToken(token);
if (bot) {
// with the pino logger the first arg is an object of data to log
// the second arg is a message
// all other args are formated args for the msg
request.logger.info({ bot }, "Retrieved bot(s) at %s", new Date());
return bot;
}
throw Boom.notFound("Bot not found");
},
},
});
interface MessageRequest {
phoneNumber: string;
message: string;
}
export const SendBotRoute = Helpers.noAuth({
method: "post",
path: "/api/whatsapp/bots/{token}/send",
options: {
description: "Send a message",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { token } = request.params;
const { phoneNumber, message } = request.payload as MessageRequest;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bot = await whatsappService.findByToken(token);
if (bot) {
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");
},
},
});
export const ReceiveBotRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots/{token}/receive",
options: {
description: "Receive messages",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { token } = request.params;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bot = await whatsappService.findByToken(token);
if (bot) {
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");
},
},
});
export const RegisterBotRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots/{id}/register",
options: {
description: "Register a bot",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { id } = request.params;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bot = await whatsappService.findById(id);
if (bot) {
await whatsappService.register(bot, (error: string) => {
if (error) {
return _h.response(error).code(500);
}
request.logger.info({ bot }, "Register bot at %s", new Date());
return _h.response().code(200);
});
}
throw Boom.notFound("Bot not found");
},
},
});
export const UnverifyBotRoute = Helpers.withDefaults({
method: "post",
path: "/api/whatsapp/bots/{id}/unverify",
options: {
description: "Unverify bot",
2023-03-13 16:09:41 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params;
2023-03-13 16:12:34 +00:00
const { whatsappService } = request.services("app");
const bot = await whatsappService.findById(id);
if (bot) {
return whatsappService.unverify(bot);
}
throw Boom.notFound("Bot not found");
},
},
});
2023-02-13 12:41:30 +00:00
export const RefreshBotRoute = Helpers.withDefaults({
method: "get",
path: "/api/whatsapp/bots/{id}/refresh",
options: {
description: "Refresh messages",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { id } = request.params;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
const bot = await whatsappService.findById(id);
if (bot) {
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",
2023-03-13 14:42:49 +00:00
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
2023-02-13 12:41:30 +00:00
const { phoneNumber, description } = request.payload as BotRequest;
2023-03-13 14:42:49 +00:00
const { whatsappService } = request.services("app");
2023-02-13 12:41:30 +00:00
console.log("request.auth.credentials:", request.auth.credentials);
const bot = await whatsappService.create(
phoneNumber,
description,
request.auth.credentials.email as string
);
if (bot) {
request.logger.info({ bot }, "Register bot at %s", new Date());
return bot;
}
throw Boom.notFound("Bot not found");
},
},
});