Skip to content

Commit

Permalink
Merge pull request #32 from Nummenpojat/messages-to-lists
Browse files Browse the repository at this point in the history
Messages to lists
  • Loading branch information
Aromiii authored Dec 19, 2022
2 parents 2af7cf0 + 59fd4b8 commit eff32f3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
44 changes: 28 additions & 16 deletions src/modules/whatsapp/commands/sendMessageToList.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
}
26 changes: 26 additions & 0 deletions src/modules/whatsapp/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}




Expand Down
13 changes: 12 additions & 1 deletion src/router/whatsapp.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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)
}
})
})

0 comments on commit eff32f3

Please sign in to comment.