-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-message.js
134 lines (117 loc) · 3.61 KB
/
process-message.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
/**
* @author : Manjeet Kumar
* @description : handles the processing of message and decides what type of response should be sent next
*/
const fetch = require('node-fetch');
const projectId = 'human-npgqes'; //https://dialogflow.com/docs/agents#settings
const sessionId = '123456';
const languageCode = 'en-US';
const dialogflow = require('dialogflow');
const config = {
credentials: {
private_key: process.env.DIALOGFLOW_PRIVATE_KEY,
client_email: process.env.DIALOGFLOW_CLIENT_EMAIL
}
};
const sessionClient = new dialogflow.SessionsClient(config);
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// Remember the Page Access Token you got from Facebook earlier?
// Don't forget to add it to your `variables.env` file.
const { FACEBOOK_ACCESS_TOKEN } = process.env;
const sendTextMessage = (userId, text) => {
return fetch(
`https://graph.facebook.com/v2.6/me/messages?access_token=${FACEBOOK_ACCESS_TOKEN}`,
{
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
messaging_type: 'RESPONSE',
recipient: {
id: userId,
},
message: {
text,
},
}),
}
);
}
function processMsgs(event, type) {
switch(type) {
case 'E' : sendAutoMatedResponse(event);
break;
case 'NEW' : signHimUp(event, type);
break;
case 'U' : signHimUp(event, type);
break;
case 'P' : signHimUp(event, type);
break;
case 'EML': signHimUp(event, type)
break;
case 'DOB': signHimUp(event, 'DOB');
break;
default : sendAutoMatedResponse(event);
break;
}
}
// sends automated response via dialogflow small talk feature
function sendAutoMatedResponse(event) {
const userId = event.sender.id;
const message = event.message.text;
const request = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: languageCode,
},
},
};
sessionClient
.detectIntent(request)
.then(responses => {
const result = responses[0].queryResult;
return sendTextMessage(userId, result.fulfillmentText);
})
.catch(err => {
console.error('ERROR:', err);
});
}
//sends custom responses for getting user data
function signHimUp(event, type) {
const userId = event.sender.id;
const message = event.message.text;
const request = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: languageCode,
},
},
};
sessionClient
.detectIntent(request)
.then(responses => {
const response = getResponseText(event, type);
console.log('the control is here =>', response);
return sendTextMessage(userId, response);
})
.catch(err => {
console.error('ERROR:', err);
});
}
function getResponseText(event, type) {
switch(type) {
case 'NEW': return 'Signing you up! Please enter your username now';
case 'U': return 'please enter your password now'
case 'P': let userName = global.users[event.sender.id].username;
return `Great!, you're signed up. Welcome ${userName}
We'll need a few details, please enter your email now`;
case 'EML': return `please enter your date of birth in dd/mm/yyyy `;
case 'DOB': return `you're all set. Send us a message! `;
}
}
module.exports = processMsgs;