-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
219 lines (201 loc) · 7.08 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
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
app.set('port', (process.env.PORT || 5000))
// 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'] === 'my_voice_is_my_password_verify_me') {
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'))
})
var sender
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
if(event){
if(event.message){
sender = event.sender.id
if(event.message.text)
{
let text = event.message.text
//call
console.log(text);
//console.log("http://www.wolframalpha.com/input/?i="+text)
rateProf(text);
continue;
}
}
}
}
res.sendStatus(200)
})
const token = "EAADDqvzyrQ8BAFW4oIp3k9LSyzs6fKcgnoLW43zKAaTuXHsPOoAl6kgeHZBQ4oDln1yZCviwu0Q0cbOTnZCgZBgOTCJZAQRitsy7lubQE7H9hVRV6pstBEKPBJ6ld7SBInnz8APD4CwCfDm4RMuUdmZCMPMC8L71qIBk52C3kpZAAZDZD"
function sendTextMessage(sender, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
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)
}
})
}
var Client = require('node-wolfram');
/*if (event.message && event.message.text) {
let text = event.message.text
if (text === 'Generic') {
sendGenericMessage(sender)
continue
}
sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
}
if (event.postback) {
let text = JSON.stringify(event.postback)
sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token)
continue
}
}*/
var Wolfram = new Client('8WQXLX-RXP44VTAEG');
//console.log("wolfram name")
//console.log(Wolfram)
function rateProf(text) {
var callback = function(e,result){
if (!result) {
console.log("return");
return;
}
console.log("in");
//Wolfram.query(text1, function(err, result) {
//if(err)
// console.log(err);
//else
//{
var flag = 0
console.log("inside");
if(result)
{
console.log("Result emp:")
if(result.queryresult)
{
console.log("QueryResult emp:")
if(result.queryresult.pod)
{
console.log("Pod emp:")
for(var a=1; a<(result.queryresult.pod.length>3?3:result.queryresult.pod.length); a++)
{
var pod = result.queryresult.pod[a];
if(!pod)
{
console.log("return");
//return;
}
else
{
console.log(pod.$.title,": ");
for(var b=0; b<(pod.subpod.length>3?3:pod.subpod.length); b++)
{
var subpod = pod.subpod[b];
if(subpod)
{
console.log("Subpod")
for(var c=0; c<subpod.plaintext.length; c++)
{
var text = subpod.plaintext[c];
console.log('\t', text);
sendTextMessage(sender,text)
flag = 1
}
for(var c=0; c<subpod.img.length; c++)
{
var img_url = subpod.img[c];
console.log('\t', img_url);
sendGenericMessage(sender,img_url)
flag = 1
}
}
}
//return;
}
}
}
}
}
if(flag == 0)
sendTextMessage(sender,"Not ready for this question, but soon!")
//sendTextMessage(sender,"http://www.wolframalpha.com/input/?i="+text)
}
//console.log("
Wolfram.query(text,callback);
};
function sendGenericMessage(sender,img_url) {
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "First card",
"subtitle": "Element #1 of an hscroll",
"image_url": img_url,
"buttons": [{
"type": "web_url",
"uarl": "https://www.google.com",
"title": "web url"
}, {
"type": "postback",
"title": "Postback",
"payload": "Payload for first element in a generic bubble",
}],
}, {
"title": "Second card",
"subtitle": "Element #2 of an hscroll",
"image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
"buttons": [{
"type": "postback",
"title": "Postback",
"payload": "Payload for second element in a generic bubble",
}],
}]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
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)
}
})
}