-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
331 lines (320 loc) · 8.62 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
324
325
326
327
328
329
330
331
'use strict'
var NodeSession = require('node-session');
var http = require('http')
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
var needle = require('needle');
const app = express()
const superagent = require('superagent');
const CLIENT_ACCESS_TOKEN = "f5db0bbe9c0d4c02928bf261e2fe431c"
const apiaiApp = require('apiai')(CLIENT_ACCESS_TOKEN);
var user = ""
var http = require('http')
const token = "EAAByjr8pz8MBAKvkSsCh4VsewZAT5q37xdXzHveh3ApdCuYui3xpqIfznKmY9RqJeK2cqU8uXqmP4JGbTXTGqSkMHEvb9hdykFGi2jCWjXnq4fxftp2fW8y0QD5FeYUYDZATDqeEaXZAbZAuB6y7XxMwg8f7QojIbdYi5UB48wZDZD"
const builder = require('botbuilder');
const quick = require('botbuilder-facebook-quick-replies');
// var session = require('express-session');
var session = new NodeSession({secret : "288ed46ac547e46a1e92024e52ab6db1"})
var queryAboutContext = ""
var listOrCount = ""
module.exports.TOKEN_GITHUB_DOT_COM = "d8908df607caae5d5c10ac96151235c828eebd8b"
app.set('port', (process.env.PORT || 5000))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
sendTextMessage(1162839713765285, res.toString())
if (req.query['hub.verify_token'] === '3649') {
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'))
})
app.post('/webhook/', function (req, res) {
var jsonBody = req.body
if(req.body.entry[0])
{
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]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
sendApiAiMessage(sender, text.substring(0,200))
}
}
}
else
{
var msg
if(jsonBody.issue != undefined)
{
msg = ""+jsonBody.issue.title + " is created on "+jsonBody.issue.created_at +"\n and updated on "+ jsonBody.issue.updated_at+"\nfor user "+jsonBody.issue.user.login
+".\n\nOpen the issue using with "+jsonBody.issue.html_url
}
else if (jsonBody.pusher)
{
msg = "A push is performedd by "+jsonBody.pusher.name+"\non repository "+jsonBody.repository.name
+"\nat "+(new Date(jsonBody.repository.pushed_at).toString("dd MMM yyyy"))+"\n with commit id "+jsonBody.commits[0].id
+"\nwith a commit message "+jsonBody.commits[0].message
+"\n\nFurther information can be viewed on "+jsonBody.repository.html_url
}
else if(jsonBody.forkee)
{
msg = "A fork is made to "+jsonBody.forkee.name +" repository in "
+jsonBody.organization.login+"\n On "+jsonBody.forkee.created_at+"\n By "+jsonBody.forkee.owner.login
+"\n\nFurther information can be found at \n"+jsonBody.repository.html_url
}
else if(jsonBody.ref_type == "branch" && jsonBody.ref)
{
msg = "A new branch is created for repository "+jsonBody.repository.name+"\nIn organization "+jsonBody.organization.login
+"\nBy user "+jsonBody.sender.login+ "\nOn "+(new Date().toString())+"\n\n Further information can be found at \n"
+jsonBody.repository.html_url
}
else if(jsonBody.repository)
{
msg = "A repository named "+jsonBody.repository.full_name
+" is "+ jsonBody.action+" in "+jsonBody.organization.login+" profile"
+" on "+jsonBody.repository.created_at
+".\n\n Further information can be found in "+jsonBody.repository.owner.html_url
}
sendTextMessage(1162839713765285, msg)
}
res.sendStatus(200)
})
function sendApiAiMessage(sender, text)
{
let messageData = { text:text }
let apiai = apiaiApp.textRequest(text, {
sessionId: 'checking_username_replies'
});
apiai.on('response', (response) => {
// Got a response from api.ai. Let's POST to Facebook Messenger
let aiText = response.result.fulfillment.speech;
let str = "nothing";
aiText = aiText?aiText:str
if(aiText == "hey!! What is your query about?")
{
var messageJson = {
text: aiText,
"quick_replies" :[
{
"content_type":"text",
"title":"Forks",
"payload": "later"
},
{
"content_type":"text",
"title":"Pushes",
"payload": "later"
},
{
"content_type":"text",
"title":"Branches",
"payload": "later"
},
{
"content_type":"text",
"title":"Commits",
"payload": "later"
},
{
"content_type":"text",
"title":"PRs",
"payload": "later"
},
{
"content_type":"text",
"title":"Issues",
"payload": "later"
},
{
"content_type":"text",
"title":"Repos",
"payload": "later"
},
{
"content_type":"text",
"title":"Followers",
"payload": "later"
},
{
"content_type":"text",
"title":"Following",
"payload": "later"
},
]
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id: sender},
message: messageJson
}
}, function(error, response, body) {
if (error) {
throw error;
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
else if (aiText == "Forks"
|| aiText == "Pushes"
|| aiText == "Issues"
|| aiText == "Repos"
|| aiText == "Following"
|| aiText == "Followers"
|| aiText == "Branches"
|| aiText == "PRs" )
{
queryAboutContext = aiText
var messageJson = {
text: aiText,
"quick_replies" :[
{
"content_type":"text",
"title":"Count",
"payload": "later"
},
{
"content_type":"text",
"title":"List",
"payload": "later"
}
]
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id: sender},
message: messageJson
}
}, function(error, response, body) {
if (error) {
throw error;
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
else if(aiText == "Count" || aiText == "List")
{
listOrCount = aiText
var queryObject = {
"queryAboutContext" : queryAboutContext,
"listOrCount" : listOrCount
}
if(queryAboutContext == "Followers")
{
messageJson = getFollowersList(messageJson, sender);
}
}
else {
messageJson = {text: aiText}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id: sender},
message: messageJson
}
}, function(error, response, body) {
if (error) {
throw error;
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
});
apiai.on('error', (error) => {
console.log(error);
});
apiai.end();
}
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: {text: text},
}
}, function(error, response, body) {
if (error) {
throw error;
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function getFollowersList(messageJson, sender) {
needle.get ('https://api.github.com/users/mahikanthnag/followers', function(error, response)
{
if(!error)
{
messageJson =
{
attachment: {
type: "template",
payload:
{
template_type: "button",
text: "Link to profile",
buttons: []
}
}
};
console.log(response.body);
var i = 0;
for(var index of response.body) {
var button = {};
button.type = "web_url",
button.url = index.html_url,
button.title = index.login;
messageJson.attachment.payload.buttons[0] = button;
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json:
{
recipient: { id: sender },
message: messageJson
}
}, function(error, response, body)
{
if(error)
{
throw error;
console.log('Error sending messages: ', error);
}
else if(response.body.error)
{
console.log('Error: ', response.body.error);
}
});
}
}
});
return messageJson;
}