-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (125 loc) · 4.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require('express')
const app = express();
const server = require('http').createServer(app)
const cors = require('cors');
const io = require('socket.io')(server,{
cors:{
origin:"*"
}
})
const bodyParser = require('body-parser');
app.use(express.json(),cors());
// const poolSocket=io.of("/pool");
// const accountSid = 'ACf680c4080efebab4cea8b60cbb5620d4'; // Your Twilio Account SID
// const authToken = '6eb830f7275aa64d35a56500c572e9f2'; // Your Twilio Auth Token
// // const client = require('twilio')(accountSid, authToken);
const accountSid = 'AC6588fe8f78edd2b554062bf026a42889';
const authToken = 'ee6d439839ed79a18ef66d2211a77b3c';
const client = require('twilio')(accountSid, authToken);
app.use(bodyParser.json());
const mobileNumberPools = {};
let totalUsers;
let acceptedPayments = 0;
io.on("connection", (socket) => {
console.log("user connected with: ",socket.id);
socket.on("joinPool", (mobileNumber) => {
// Join the POOL with mobile number
const existingPool = socket.rooms[mobileNumber];
// console.log(existingPool)
if (existingPool) {
// Leave the existing pool
socket.leave(existingPool);
console.log(`User left Pool: ${existingPool}`);
}
// Join the POOL with mobile number
socket.join(mobileNumber);
console.log(`User joined Pool: ${mobileNumber}`);
// Update the mobile number Pool with the socket ID
mobileNumberPools[mobileNumber] = socket.id;
console.log(mobileNumberPools);
});
socket.on("paymentConfirmation", (users) => {
// Iterate through the user details and emit payment confirmation to each user
console.log(mobileNumberPools)
console.log(users.length)
totalUsers = users.length;
// console.log(totalUsers)
users.forEach((user) => {
const mobileNumber = user.phoneNumber;
const socketId = mobileNumberPools[mobileNumber]; // Get the socket ID for the user
console.log(socketId)
if (socketId) {
// Emit the "Payment Confirmation" message to the user's socket
socket.to(socketId).emit("paymentStatus", {
status: "Payment Confirmation",
Amount:user.amount
});
console.log(`Confirmation message sent to user: ${socketId}`, mobileNumber);
} else {
console.log("No socket found for mobileNumber: ", mobileNumber);
}
});
});
socket.on("paymentAccepted", () => {
acceptedPayments++;
console.log(acceptedPayments)
console.log(totalUsers)
if (acceptedPayments === totalUsers) {
console.log("All payments accepted")
io.emit("Accepted", { status: "All payments accepted" });
acceptedPayments=0;
}
});
socket.on("paymentDeclined", () => {
io.emit("Declined",{status:"someone declined the payment"})
});
socket.on("disconnect", () => {
console.log("A user disconnected");
// Find the Pools the socket is currently in
const Pools = Object.keys(socket.rooms);
// Loop through the Pools (excluding the socket ID)
Pools.forEach((Pool) => {
if (Pool !== socket.id) {
// Leave the Pool
console.log(`User left Pool: ${Pool}`);
socket.leave(Pool);
// Remove the socket ID from the mobile number Pool
const mobileNumber = Pool;
if (mobileNumberPools[mobileNumber]) {
mobileNumberPools[mobileNumber] = mobileNumberPools[mobileNumber].filter(
(id) => id !== socket.id
);
// Remove the mobile number Pool entry if there are no sockets in it
if (mobileNumberPools[mobileNumber].length === 0) {
delete mobileNumberPools[mobileNumber];
}
}
}
});
});
});
// POST endpoint to send messages to multiple mobile numbers
app.post('/send-messages', async (req, res) => {
const { numbers, message } = req.body;
console.log(req.body)
try {
// Loop through each mobile number and send a message
for (const number of numbers) {
await client.messages
.create({
body: message,
from: 'whatsapp:+14155238886',
to: `whatsapp:+91${number}`
})
.then(message => console.log(message.sid))
}
res.json({ success: true, message: 'Messages sent successfully' });
} catch (error) {
console.error('Error sending messages:', error);
res.status(500).json({ success: false, message: 'Failed to send messages' });
}
});
const port = 4000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});