forked from Pilgrim-Xzed/USSD-Report-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
184 lines (139 loc) · 3.52 KB
/
app.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const express = require('express')
const app = express()
const morgan = require('morgan')
const bodyParser = require('body-parser')
const firebase = require('firebase')
const cors = require('cors')
const NodeGeocoder = require('node-geocoder')
const ussdMenu = require('ussd-menu-builder')
let menu = new ussdMenu()
let options = {
provider: 'google',
// Optional depending on the providers
httpAdapter: 'https', // Default
apiKey: 'AIzaSyC2MsiYaaXea2X1qGDu_tVBBMTPoUXun7k', // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
}
const geocoder = NodeGeocoder(options)
firebase.initializeApp({
serviceAccount:'./Lesson-efa0be30b4f5.json',
databaseURL:'https://playchat-591df.firebaseio.com/'
})
app.use(morgan('dev'))
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json());
app.use(cors({
'allowedHeaders': ['sessionId', 'Content-Type'],
'exposedHeaders': ['sessionId'],
'origin': '*',
'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
'preflightContinue': false
}));
const ref = firebase.database().ref('node')
const sosRef = ref.child('sos-reports')
let epState ;
let epLga;
let disease;
let desc;
let epPlace;
app.get('/',(req,res)=>{
ref.child('sos-reports').once('value').then((snap)=>{
let reports = snap.val()
return res.status(200).json({
reports
})
})
return res.status(200)
})
app.post('/',(req,res)=>{
let args = {
phoneNumber: req.body.phoneNumber,
sessionId: req.body.sessionId,
serviceCode: req.body.serviceCode,
text: req.body.text
};
menu.run(args, resMsg => {
res.send(resMsg);
});
menu.startState({
run: () => {
// use menu.con() to send response without terminating session
menu.con(`REPORT EPIDEMIC/DISEASE OUTBREAK:\n1.Kano \n2.Kaduna \n3.Lagos`);
},
// next object links to next state based on user input
next: {
'1': 'Kano',
'2': 'Kaduna',
'3': 'Lagos'
}
});
menu.state('Kano', {
run: () => {
epState = "Kano",
menu.con(`SELECT LGA :\n1.Gwarzo \n2.Bebeji \n3.Karaye`);
},
next:{
'1':'Gwarzo',
'2':'Bebeji',
'3':'Karaye'
}
});
menu.state('Gwarzo',{
run:()=>{
epLga="Gwarzo",
menu.con(`DISEASE EPIDEMIC :\n1.Malaria \n2.Lasa Fever \n3.Ebola`);
},
next:{
'1':'Malaria',
'2':'LasaFever',
'3':'Ebola'
}
})
menu.state('Malaria',{
run:()=>{
disease = "Malaria"
menu.con(`Enter Particular Location to be precised `);
},
next: {
'*[a-zA-Z]+': 'Malaria.locationName'
}
})
menu.state('Malaria.locationName',{
run:()=>{
epPlace = menu.val;
console.log(epPlace)
menu.con(`Enter Other Info `);
},
next: {
'*[a-zA-Z]+': 'Final.desc'
}
})
menu.state('Final.desc',{
run:()=>{
desc = menu.val;
geocoder.geocode({address: epPlace, countryCode: 'Ng',state:epState, minConfidence: 0.5, limit: 5}, function(err, res) {
sosRef.push()
sosRef.push({
state:epState,
lga:epLga,
desc:desc,
epidemic:disease,
location:res
})
}).catch(err=>{
geocoder.geocode({address: epLga, countryCode: 'Ng',state:epState, minConfidence: 0.5, limit: 5}, function(err, res) {
sosRef.push()
sosRef.push({
state:epState,
lga:epLga,
desc:desc,
epidemic:disease,
location:res
})
})
})
menu.end(`Thank You, SOS has been sent `);
},
})
})
module.exports = app