-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageHandler.js
137 lines (129 loc) · 4.04 KB
/
MessageHandler.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
const request = require('request')
const yahooFinance = require('yahoo-finance')
const R = require('ramda')
var sendMessageToSenderId
// generic function sending messages
exports.receivedMessage = (event) => {
return new Promise((resolve, reject) => {
try {
var senderID = event.sender.id
var recipientID = event.recipient.id
var timeOfMessage = event.timestamp
var message = event.message
console.log('Received message for user %d and page %d at %d with message:',
senderID, recipientID, timeOfMessage)
console.log(JSON.stringify(message))
// var messageId = message.mid
var messageText = message.text
var messageAttachments = message.attachments
sendMessageToSenderId = callSendAPI(senderID)
if (messageText) {
// If we receive a text message, check to see if it matches a keyword
// and send back the example. Otherwise, just echo the text we received.
// return sendGenericMessage(senderID, messageText)
return sendTextMessage(messageText)
// switch (messageText) {
// case 'generic':
// sendGenericMessage(senderID, messageText)
// break
//
// default:
// sendTextMessage(senderID, messageText)
// }
} else if (messageAttachments) {
return sendTextMessage('Message with attachment received')
}
} catch (error) {
console.log('error', error)
reject(error)
}
})
}
function sendGenericMessage (messageText) {
var message = {
attachment: {
type: 'template',
payload: {
template_type: 'generic',
elements: [{
title: 'rift',
subtitle: 'Next-generation virtual reality',
item_url: 'https://www.oculus.com/en-us/rift/',
image_url: 'http://messengerdemo.parseapp.com/img/rift.png',
buttons: [{
type: 'web_url',
url: 'https://www.oculus.com/en-us/rift/',
title: 'Open Web URL'
}, {
type: 'postback',
title: 'Call Postback',
payload: 'Payload for first bubble'
}]
}, {
title: 'touch',
subtitle: 'Your Hands, Now in VR',
item_url: 'https://www.oculus.com/en-us/touch/',
image_url: 'http://messengerdemo.parseapp.com/img/touch.png',
buttons: [{
type: 'web_url',
url: 'https://www.oculus.com/en-us/touch/',
title: 'Open Web URL'
}, {
type: 'postback',
title: 'Call Postback',
payload: 'Payload for second bubble'
}]
}]
}
}
}
return sendMessageToSenderId(message)
}
function sendTextMessage (messageText) {
return getStockBySymbol(messageText)
.then((data) => sendMessageToSenderId(data))
}
function getStockBySymbol (symbol) {
return new Promise((resolve, reject) => {
yahooFinance.snapshot({
symbol,
fields: ['s', 'n', 'd1', 'l1', 'y', 'r']
}, (error, snapshot) => {
if (error) return reject(error)
console.log(`
Name: ${snapshot.name},
lastTraded: ${snapshot.lastTradeDate},
lastTradePriceOnly: ${snapshot.lastTradePriceOnly}`
)
let data = `
Name: ${snapshot.name},
lastTraded: ${snapshot.lastTradeDate},
lastTradePriceOnly: ${snapshot.lastTradePriceOnly}`
return resolve(data)
})
})
}
const callSendAPI = R.curry((recipientId, message) => {
return new Promise((resolve, reject) => {
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
method: 'POST',
json: {
recipient: {
id: recipientId
},
message
}
}, function (error, response, body) {
if (error) {
reject('Error sending message: ', error)
} else if (response.body.error) {
reject('Error: ', response.body.error)
}
console.log('response', response)
console.log('body', body)
resolve(response)
})
})
})