-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathussd.js
More file actions
102 lines (76 loc) · 2.02 KB
/
ussd.js
File metadata and controls
102 lines (76 loc) · 2.02 KB
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
const express = require('express');
const app = express();
//user credentials to allow airtime sending
const credentials = {
apiKey: '',
username: ''
}
const AfricasTalking = require('africastalking')(credentials);
const airtime = AfricasTalking.AIRTIME;
const sms = AfricasTalking.SMS;
//Setting get route
app.get("/", (req, res) => {
res.render('ussd', res.locals.commonData);
});
//Setting post route and USSD methods
app.post("/", (req, res) => {
//const sessionId = req.body.sessionId;
//const serviceCode = req.body.serviceCode;
//const phoneNumber = req.body.phoneNumber;
//const text = req.body.text;
console.log(req);
//USSD logic
let response = '';
if (text == "") {
response = `CON What would you like to check?
1. My Account
2. My phone number
3. Send airtime
4. Give yourself a shout out!`;
} else if (text == "1") {
response = `CON Choose the information that you would like to view
1. Account number
2. Account balance`;
} else if (text == "2") {
response = `END Your phone number is ${phoneNumber}`;
} else if (text == "1*1") {
const accountNumber = "ACC001";
response = `END Your account number is ${accountNumber}`;
} else if (text == "1*2") {
const accountBalance = 1222;
response = `END Your account balance for account number ${accountNumber} is KES ${accountBalance}`;
} else if (text == "3") {
//Sends airtime to the number that is running the USSD
const recipient1 = {
to: phoneNumber,
amount: "KES 50"
};
const options = {
recipient: recipient1
};
airtime.send(options)
.then(resp => {
console.log(resp);
})
.catch(error => {
console.log(error)
});
} else if (text == "4") {
const smsOptions = {
to: ['+254727545805'],
message: "Hey, wassup!"
};
sms.send(smsOptions)
.then(resp => {
console.log(resp);
})
.catch(error => {
console.log(error);
});
response = `END Just hit you up!`;
}
});
//Start server on specified port
app.listen(3000, () => {
console.log("Server started on port 3000");
});