Whatsapp service updates

This commit is contained in:
Darren Clarke 2024-05-16 18:22:10 +02:00
parent e22a8e8d98
commit 3da103c010
16 changed files with 151 additions and 36 deletions

View file

@ -2,7 +2,7 @@ import type { Metadata } from "next";
import { LicenseInfo } from "@mui/x-license";
LicenseInfo.setLicenseKey(
"7c9bf25d9e240f76e77cbf7d2ba58a23Tz02NjU4OCxFPTE3MTU4NjIzMzQ2ODgsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
"c787ac6613c5f2aa0494c4285fe3e9f2Tz04OTY1NyxFPTE3NDYzNDE0ODkwMDAsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
);
export const metadata: Metadata = {

View file

@ -26,6 +26,7 @@ export const SendMessageRoute = withDefaults({
description: "Send a message",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params;
console.log({ payload: request.payload });
const { phoneNumber, message } = request.payload as MessageRequest;
const whatsappService = getService(request);
await whatsappService.send(id, phoneNumber, message as string);

View file

@ -89,8 +89,8 @@ export default class WhatsappService extends Service {
if (qr) {
console.log("got qr code");
const botDirectory = this.getBotDirectory(botID);
const qrPath = `${botDirectory}/qr.png`;
fs.writeFileSync(qrPath, qr, "base64");
const qrPath = `${botDirectory}/qr.txt`;
fs.writeFileSync(qrPath, qr, "utf8");
const verifiedFile = `${botDirectory}/verified`;
if (fs.existsSync(verifiedFile)) {
fs.rmSync(verifiedFile);
@ -220,13 +220,16 @@ export default class WhatsappService extends Service {
whatsappBotId: botID,
};
await fetch(`http://localhost:3000/api/whatsapp/${botID}/receive`, {
method: "POST",
headers: {
"Content-Type": "application/json",
await fetch(
`${process.env.BRIDGE_FRONTEND_URL}/api/whatsapp/bots/${botID}/receive`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(receivedMessage),
},
body: JSON.stringify(receivedMessage),
});
);
}
}
}
@ -242,9 +245,9 @@ export default class WhatsappService extends Service {
getBot(botID: string): Record<string, any> {
const botDirectory = this.getBotDirectory(botID);
const qrPath = `${botDirectory}/qr.png`;
const qrPath = `${botDirectory}/qr.txt`;
const verifiedFile = `${botDirectory}/verified`;
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "base64") : null;
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "utf8") : null;
const verified = fs.existsSync(verifiedFile);
return { qr, verified };
@ -276,6 +279,7 @@ export default class WhatsappService extends Service {
_lastReceivedDate: Date,
): Promise<proto.IWebMessageInfo[]> {
const connection = this.connections[botID]?.socket;
console.log({ connection });
const messages = await connection.loadAllUnreadMessages();
return messages;

View file

@ -1,12 +1,22 @@
// import { db, getWorkerUtils } from "bridge-common";
import { db, getWorkerUtils } from "bridge-common";
interface ReceiveWhatsappMessageTaskOptions {
token;
message: any;
}
const receiveWhatsappMessageTask = async ({
token,
message,
}: ReceiveWhatsappMessageTaskOptions): Promise<void> => {};
}: ReceiveWhatsappMessageTaskOptions): Promise<void> => {
const bot = await db
.selectFrom("WhatsappBot")
.selectAll()
.where((eb) => eb.or([eb("token", "=", token), eb("id", "=", token)]))
.executeTakeFirstOrThrow();
console.log(bot);
};
export default receiveWhatsappMessageTask;

View file

@ -1,11 +1,32 @@
// import { db, getWorkerUtils } from "bridge-common";
import { db } from "bridge-common";
interface SendWhatsappMessageTaskOptions {
token: string;
recipient: string;
message: any;
}
const sendWhatsappMessageTask = async ({
message,
}: SendWhatsappMessageTaskOptions): Promise<void> => {};
recipient,
token,
}: SendWhatsappMessageTaskOptions): Promise<void> => {
const bot = await db
.selectFrom("WhatsappBot")
.selectAll()
.where("token", "=", token)
.executeTakeFirstOrThrow();
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`;
const params = { message, phoneNumber: recipient };
console.log({ params });
const result = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(params),
});
console.log({ result });
};
export default sendWhatsappMessageTask;

View file

@ -15,7 +15,7 @@ import fr from "leafcutter-ui/locales/fr.json";
import { LicenseInfo } from "@mui/x-license";
LicenseInfo.setLicenseKey(
"7c9bf25d9e240f76e77cbf7d2ba58a23Tz02NjU4OCxFPTE3MTU4NjIzMzQ2ODgsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
"c787ac6613c5f2aa0494c4285fe3e9f2Tz04OTY1NyxFPTE3NDYzNDE0ODkwMDAsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
);
const messages: any = { en, fr };

View file

@ -14,7 +14,7 @@ import { LicenseInfo } from "@mui/x-license";
import { locales, LeafcutterProvider } from "leafcutter-ui";
LicenseInfo.setLicenseKey(
"7c9bf25d9e240f76e77cbf7d2ba58a23Tz02NjU4OCxFPTE3MTU4NjIzMzQ2ODgsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
"c787ac6613c5f2aa0494c4285fe3e9f2Tz04OTY1NyxFPTE3NDYzNDE0ODkwMDAsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
);
export const MultiProvider: FC<PropsWithChildren> = ({ children }) => {