-
Notifications
You must be signed in to change notification settings - Fork 11
/
SmsProxy.js
73 lines (56 loc) · 1.97 KB
/
SmsProxy.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
'use strict'
const Nexmo = require('nexmo');
class SmsProxy {
constructor() {
this.nexmo = new Nexmo({
apiKey: process.env.NEXMO_API_KEY,
apiSecret: process.env.NEXMO_API_SECRET
}, {
debug: true
});
}
createChat(userANumber, userBNumber) {
this.chat = {
userA: userANumber,
userB: userBNumber
};
this.sendSMS();
}
sendSMS() {
/*
Send a message from userA to the virtual number
*/
this.nexmo.message.sendSms(this.chat.userA,
process.env.VIRTUAL_NUMBER,
'Reply to this SMS to talk to UserA');
/*
Send a message from userB to the virtual number
*/
this.nexmo.message.sendSms(this.chat.userB,
process.env.VIRTUAL_NUMBER,
'Reply to this SMS to talk to UserB');
}
getDestinationRealNumber(from) {
let destinationRealNumber = null;
// Use `from` numbers to work out who is sending to whom
const fromUserA = (from === this.chat.userA);
const fromUserB = (from === this.chat.userB);
if (fromUserA || fromUserB) {
destinationRealNumber = fromUserA ? this.chat.userB : this.chat.userA;
}
return destinationRealNumber;
}
proxySms(from, text) {
// Determine which real number to send the SMS to
const destinationRealNumber = this.getDestinationRealNumber(from);
if (destinationRealNumber === null) {
console.log(`No chat found for this number`);
return;
}
// Send the SMS from the virtual number to the real number
this.nexmo.message.sendSms(process.env.VIRTUAL_NUMBER,
destinationRealNumber,
text);
}
}
module.exports = SmsProxy;