-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
323 lines (275 loc) · 12.6 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const FB_TOKEN = process.env.FB_PAGE_ACCESS_TOKEN
const WIT_TOKEN = process.env.WIT_AI_SERVER_TOKEN
const FB_HUB_VERIFY_TOKEN = process.env.FB_HUB_VERIFY_TOKEN
let Wit = null
let log = null
try {
// if running from repo
Wit = require('../').Wit
log = require('../').log
} catch (e) {
Wit = require('node-wit').Wit
log = require('node-wit').log
}
// ----------------------------------------------------------------------------
// Wit.ai bot specific code
const wit = new Wit({
accessToken: WIT_TOKEN,
logger: new log.Logger(log.INFO)
})
// ----------------------------------------------------------------------------
const app = express()
app.set('port', (process.env.PORT || 5000))
app.use(({method, url}, rsp, next) => {
rsp.on('finish', () => {
console.log(`${rsp.statusCode} ${method} ${url}`)
})
next()
})
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === FB_HUB_VERIFY_TOKEN_) {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
// Process messages
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
console.log("Received messaging events at webhook: ", messaging_events)
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
forwardMessengerEventToWit(event)
}
res.sendStatus(200)
})
function forwardMessengerEventToWit(event) {
if (!event.message || !event.message.text) {
console.log("There was no event message! Did not forward to Wit")
return
}
console.log('Received event:', event)
let messageText = event.message.text
wit.message(
messageText
).then((context) => {
handleWitSuccessResponse(context, event, messageText)
})
.catch((err) => {
console.error('Oops! Got an error from Wit: ', err.stack || err)
})
}
function handleWitSuccessResponse(context, messengerEvent, originalMessage) {
let entities = context.entities
let fbSenderId = messengerEvent.sender.id
if (Object.keys(entities).length != 1) {
console.log('Context entities for message \"', originalMessage, '\" does not equal 1 for context: ', context)
let messageToSend = 'I don\'t understand what you mean by "' + originalMessage + '"\n\n' + 'You can try calling the Toll-Free HIV and AIDS Helpline and speak to a human - 0800-012-322'
sendMessengerTextMessageToUserWithId(fbSenderId, messageToSend)
logAnalytics_WitHadNoEntityForQuestion(originalMessage, fbSenderId)
return
}
let entityName = Object.keys(entities)[0]
console.log('Will send message for entity with name: ', entityName)
let messageToSend = messageForWitEntityName(entityName)
sendMessengerTextMessageToUserWithId(fbSenderId, messageToSend)
logAnalytics_UserAskedQuestionEvent(entityName, fbSenderId)
}
function messageForWitEntityName(entityName) {
switch (entityName) {
case 'can_anyone_get_hiv':
return 'Anybody can get HIV and AIDS. ' +
'No matter what age, race, culture or religion they belong to. ' +
'Men, women and even children can get HIV and AIDS.'
case 'can_you_get_hiv_from_oral_sex':
return 'It\'s possible to get HIV from oral sex -- whether you are giving or getting oral sex'
case 'can_you_get_hiv_from_anal_sex':
return 'Yes. Anal sex without a condom is very risky behavior. Either sex partner can become infected with HIV. ' +
'\n\nWhen you have anal sex, use a latex condom. ' +
'They\'re more likely to break during anal than vaginal sex, so also use a lot of water-based lubricant to lower the chance of that happening.'
case 'can_you_have_hiv_without_aids':
return 'A person can have HIV for a long time without having AIDS. Most people don\'t look or feel sick when they first get HIV. ' +
'They may not get sick for a long time. The virus can stay in their blood for years. At this stage, the person does not have AIDS. ' +
'Usually people with HIV get sick only after five to ten years.'
case 'can_you_still_have_sex':
return 'You can still have sex if you have HIV. However, it is important to remember to use a condom.'
case 'does_having_sex_with_a_virgin_cure_hiv':
return 'No, having sex with a virgin does not cure HIV or AIDS. ' +
'\n\nThere is no cure for HIV or AIDS yet, but it is still possible to live a long and healthy life.'
case 'how_long_does_it_take_for_hiv_to_cause_aids':
return 'How long it takes for AIDS symptoms to appear is different for each person. ' +
'It depends on things like your health in general and how well you are taking care of yourself.' +
'\n\n Usually people with HIV get sick only after five to ten years.' +
'\n\n Remember, it is still possible to live a long and healthy life if you have HIV'
case 'how_to_avoid_getting_hiv':
return 'Always remember the following rules to keep safe from HIV:' +
'\n\n1. Use a new condom every time you have sex. Unprotected sex spreads HIV!' +
'\n2. Avoid touching blood with your bare hands.' +
'\n3. Never touch a used injection needle, or a knife or a razor blade that has blood on it' +
'\n4. Cover a fresh open cut or bleeding wound with a plaster or bandage.'
case 'how_to_stay_healthy_with_hiv':
return 'A person with HIV can stay healthy by:' +
'\n1. Taking the required medicines regularly.' +
'\n2. Eating fresh fruit and vegetables.' +
'\n3. Exercising and playing sport, but also making sure to get plenty of rest.'
case 'how_to_tell_if_you_have_hiv':
return 'The only way to know for sure if a person has HIV is to have a blood test at a clinic or hospital. ' +
'\n\nYou cannot tell if someone has HIV by looking at them. ' +
'Many people who have HIV don\'t have any symptoms at all for many years.'
case 'is_it_safe_to_get_an_injection':
return 'It is safe to have an injection at a clinic or a hospital. ' +
'Doctors and nurses use only sterile injection needles. ' +
'Sterile means that it is so clean that it has no germs on it.'
case 'is_there_a_cure_for_hiv_or_aids':
return 'There is no cure for HIV or AIDS yet, but it is still possible to live a long and healthy life.' +
'\n\n' + messageForWitEntityName('how_to_stay_healthy_with_hiv')
case 'thank_you':
return textMessageReplyForThankYou()
case 'what_causes_hiv':
return 'There are only three ways that people can get HIV:' +
'\n 1. By having unprotected sex with someone who has HIV' +
'\n 2. By allowing blood from an infected person to get into their own bloodstream. ' +
'For instance, if a person with HIV uses a needle to inject drugs, and then shares the need with someone else, the virus can be passed on' +
'\n 3. A mother with HIV can pass it on to her bahby during pregnancy, in childbirth, or by breast-feeding.' +
'\n\n You *cannot* get HIV from someone sneezing or coughing near you. You also cannot get HIV by touching, hugging or kissing someone who has HIV or AIDS'
case 'what_causes_aids':
case 'what_happens_when_you_have_hiv':
return 'HIV causes AIDS:' +
'\n\nHIV slowly weakens the body\'s immune system. ' +
'Five to ten years after getting the virus, the immune system becomes so weak that it can\'t defend the body against infections. ' +
'The person with HIV then gets sick, usually with more than one illness. We then say that the person has AIDS'
case 'what_is_aids':
return "AIDS stands for Acquired Immune Deficiency Syndrome. " +
'\n"Acquired" means something that you get. Most people get AIDS from having unprotected sex or by sharing needles to inject drugs ' +
'\n\n"Immune Deficiency" means that the body\'s immune system becomes damaged. ' +
'When the immune system is weak, the body cannot fight off illnesses the way it usually does.' +
'\n\n"Syndrome" means that a person gets several illnesses all at once.'
case 'what_is_an_immune_system':
return 'Can you remember the last time you had a cold? ' +
'For a while, your head ached, you coughed and you sniffed. ' +
'Then the cold went away. This is because your body has an "immune system". ' +
'The immune system defends the body, and fights the germs and viruses that make you ill. ' +
'But HIV attacks the immune system, and the body can no longer fight germs and infections'
case 'what_is_hiv':
return 'HIV stands for Human Immunodeficiency Virus. ' +
'\n\nLet\' start with the short words: ' +
'\n"Human" means that only people can get it. ' +
'\nA "virus" is a type of germ that gets into a person\'s body. ' +
'\n"Immunodeficiency" means that the body\'s immune system becomes weak' +
'\n\n' + messageForWitEntityName('what_is_an_immune_system')
case 'what_is_the_difference_between_hiv_and_aids':
return 'There is a difference between HIV and AIDS. People who have HIV can stay healthy for a long time. ' +
'They only start getting sick when their immune system is damaged and weak. We then say that they have AIDS.'
case 'what_is_unprotected_sex':
return 'Unprotected sex is any sex without a condom. Sometimes the condom might break or slip off during sex. This still counts as unprotected sex.' +
'\n\nHaving unprotected sex puts you at risk of getting HIV. It is important to use a condom when having sex.'
case 'what_should_i_eat':
return 'People with HIV or AIDS should eat plenty of fresh fruit, vegetables, chicken and fish to stay healthy for as long as possible.' +
'\n\nFresh vegetables and fruit are full of vitamins. Vitamins make the immune system strong, which helps your body to fight against illnesses.'
case 'where_did_aids_come_from':
return 'Nobody knows where HIV came from. Scientists think that it is a new germ that appeared only some years ago. ' +
'HIV and AIDS were first identified in the early 1980s.'
case 'where_to_get_tested':
return 'You can get tested for HIV at your local clinic or hospital.'
case 'greetings':
return 'Hello! I\'m Health Buddy. I\'m a robot that can answer your questions on HIV and AIDS' +
'\n\nFor example, you can ask me "What is HIV?" or "How does HIV spread?"'
default:
//Should not get here
console.log('No message defined for entityName:', entityName);
return ''
}
}
function textMessageReplyForThankYou() {
let randomNumber = Math.floor(Math.random() * 4)
switch (randomNumber) {
case 0:
return 'You\'re welcome!'
case 1:
return 'I\'m happy to help 😊'
case 2:
return 'It\'s a pleasure 🤗'
}
}
// ----------------------------------------------------------------------------
function sendMessengerTextMessageToUserWithId(id, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:FB_TOKEN},
method: 'POST',
json: {
recipient: {id:id},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
} else {
console.log("Sent text message to user ", id)
console.log("Message", messageData)
}
})
}
// ----------------------------------------------------------------------------
// Analytics
function logAnalytics_UserAskedQuestionEvent(witEntityName, userId) {
request.post({
url : "https://graph.facebook.com/823503547798776/activities",
form: {
event: 'CUSTOM_APP_EVENTS',
custom_events: JSON.stringify([{
_eventName: "user_asked_question",
fb_description: witEntityName
}]),
advertiser_tracking_enabled: 0,
application_tracking_enabled: 0,
extinfo: JSON.stringify(['mb1']),
page_id: 1184018911707858,
page_scoped_user_id: userId
}
}, function(err,httpResponse,body){
if (err !== null) {
console.log('Error sending analytics event "user_asked_question". \n Error:', err)
}
});
}
function logAnalytics_WitHadNoEntityForQuestion(questionText, userId) {
request.post({
url : "https://graph.facebook.com/823503547798776/activities",
form: {
event: 'CUSTOM_APP_EVENTS',
custom_events: JSON.stringify([{
_eventName: "wit_had_no_entity_for_question",
fb_description: questionText
}]),
advertiser_tracking_enabled: 0,
application_tracking_enabled: 0,
extinfo: JSON.stringify(['mb1']),
page_id: 1184018911707858,
page_scoped_user_id: userId
}
}, function(err,httpResponse,body){
if (err !== null) {
console.log('Error sending analytics event "wit_had_no_entity_for_question". \n Error:', err)
}
});
}