diff --git a/README.md b/README.md index c1e4716..63a8ae8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Innovative way to market and communicate 2. Run `npm install` to install dependencies of [Suppis-core](https://github.com/Nummenpojat/suppis-core) 3. Run `npm install -g ts-node` to install ts-node, so you don't have to build the project every time 4. Now you can run `npm run dev` or `npm run start` to start up the app - - If you want to build the app run `npm run build + - If you want to build the app run `npm run build` ## How to configure [Firebase](https://firebase.google.com/) @@ -34,3 +34,8 @@ Innovative way to market and communicate - Takes a message and a number in body - Responses with string on the body - On status code 409 responses with string on the body containing qr +- Send message to list of people on `/whatsapp/send/list` + - Post request + - Takes a message and list of numbers in body + - Responses with string on the body + - On status code 409 responses with string on the body containing qr diff --git a/package.json b/package.json index e237805..d6ea541 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Suppis-core", - "version": "1.0.1-Beta", + "version": "1.1.0-Beta", "description": "Backend for Suppis", "main": "dist/main.js", "scripts": { diff --git a/src/modules/whatsapp/commands/sendMessageToList.ts b/src/modules/whatsapp/commands/sendMessageToList.ts index d9ff12c..8c4df5f 100644 --- a/src/modules/whatsapp/commands/sendMessageToList.ts +++ b/src/modules/whatsapp/commands/sendMessageToList.ts @@ -1,37 +1,49 @@ -import {client} from "../main"; +import {checkNumbers, client, isClientReady} from "../main"; /** * Send same message to list of people * @param message Text that is sent to list provided * @param persons Array that contains numbers of people to send the message * */ -export const sendMessageToList = async (message: string, persons: string[]) => { - - console.log('Sending messages...'); +export const sendMessageToList = async (message: string, numbers: string[]) => { // Checking that message is not empty if (message == "" || message == null) { throw "You need to provide message to send" } - // Sending message to each on the list - persons.forEach((person: any) => { + try { + + // Checking that message can be sent + isClientReady() + await checkNumbers(numbers); + + } catch (error) { + throw error + } + + // Sending message to each number on the list + for (const num of numbers) { //Making chat id from phone number to use at client.sendMessage to identify where to send the message - let chatId = person.number + "@c.us" + let chatId = num + "@c.us" // Removing + at the start if it exits so the phone number is in right format if (chatId.startsWith('+')) { chatId = chatId.substring(1) } - // Sending message to chosen chat - client.sendMessage(chatId, message) - .then((message) => { - console.log(`Message ${message.body} sent to ${person.name}`); - }) - .catch((error: Error) => { - throw error - }) - }) + try { + + // Sending message to chosen chat + await client.sendMessage(chatId, message); + console.log(`Message ${message} sent to ${num}`); + + } catch (error: any) { + if (error.message != undefined) { + throw error.message + } + throw error + } + } } \ No newline at end of file diff --git a/src/modules/whatsapp/main.ts b/src/modules/whatsapp/main.ts index e26674a..f0b8954 100644 --- a/src/modules/whatsapp/main.ts +++ b/src/modules/whatsapp/main.ts @@ -40,6 +40,32 @@ export const startWhatsappSession = async () => { }) } +export const checkNumbers = async (numbers: string[]) => { + for (const num of numbers) { + + // Making chat id from phone number to use at client.isRegisteredUser to use as number which is checked + let chatId = num + "@c.us" + + // Removing + at the start if it exits so the phone number is in right format + if (chatId.startsWith('+')) { + chatId = chatId.substring(1) + } + + try { + const isRegistered = await client.isRegisteredUser(chatId) + if (!isRegistered) { + throw `Number ${num} is invalid` + } + console.log(`${num} is valid`) + } catch (error: any) { + if (error.message != undefined) { + throw error.message + } + throw error + } + } +} + diff --git a/src/router/whatsapp.ts b/src/router/whatsapp.ts index 517ce86..af32e88 100644 --- a/src/router/whatsapp.ts +++ b/src/router/whatsapp.ts @@ -1,6 +1,7 @@ import {Router} from "express"; import {sendMessage} from "../modules/whatsapp/commands/sendMessage"; import {qr} from "../modules/whatsapp/main"; +import {sendMessageToList} from "../modules/whatsapp/commands/sendMessageToList"; export const router = Router() @@ -19,5 +20,15 @@ router.post("/send/one", (req, res) => { }) router.post("/send/list", (req, res) => { - res.status(503).send("Sending messages to list of people is not yet implemented") + sendMessageToList(req.body.message, req.body.numbers) + .then(() => { + res.status(200).send("Messages sent") + }) + .catch((reason) => { + if (reason.type == "qr") { + res.status(409).send(qr) + } else { + res.status(500).send(reason) + } + }) }) \ No newline at end of file