-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagingHandler.js
117 lines (104 loc) · 3.18 KB
/
messagingHandler.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
"use strict";
const engine = require('./src/engine/engine.js');
const request = require('superagent');
const fbAccessToken = process.env.FB_ACCESS_TOKEN;
const fs = require('fs');
// Hack (Morgan), but a small one since the chat bot expects a reset on the
// first message
var isFirstMessage = true;
function handleMessageEvent(event) {
let sender = event.sender.id;
if (event.message && event.message.text) {
let text = event.message.text;
console.log('Text:', text);
request.get('https://www.googleapis.com/language/translate/v2')
.set({'Content-Type': 'charset=UTF-8'})
.query({key: 'AIzaSyD88YmDJil9N_KTYjks1mDco1xEpGZ_ugs'})
.query({source: 'zh-CN'})
.query({target: 'en'})
.query({q: text})
.end((err, translation)=> {
let translatedText = translation.body.data.translations;
let message;
if(translatedText.length > 0){
message = {
body: translatedText[0].translatedText
}
}else{
message = {
body: translatedText
}
}
if (isFirstMessage || translatedText === 'reset') {
message.reset = true;
isFirstMessage = false;
}
let responses = engine.turn(message);
if(typeof responses != 'undefined'){
responses.forEach((response) => {
sendMessage(sender, response);
});
}
});
}
}
function sendMessage(sender, message) {
request.post('https://graph.facebook.com/v2.6/me/messages')
.query({access_token: fbAccessToken})
.send({
recipient: {id: sender},
message,
})
.end(callback);
function callback (error, response) {
if (error) {
fs.writeFile('fb_log.json', JSON.stringify(error, null, '\t'), 'utf8', () => {});
} else if (response.body.error) {
console.warn('Error: ', response.body.error)
}
}
}
// function sendMessage(sender, message) {
// var str;
// console.log(message);
// if(typeof message.attach == 'undefined'){
// str = message.body
// }else{
// str = message.text
// }
// request.get('https://www.googleapis.com/language/translate/v2')
// .set({'Content-Type': 'charset=UTF-8'})
// .query({key: 'AIzaSyD88YmDJil9N_KTYjks1mDco1xEpGZ_ugs'})
// .query({source: 'en'})
// .query({target: 'zh-CN'})
// .query({q: str})
// .end((err, translation)=> {
// console.log(translation.body);
// let translatedText = translation.body.data.translations;
// let message;
// if(translatedText.length > 0){
// message = {
// text: translatedText[0].translatedText
// }
// }else{
// message = {
// text: translatedText
// }
// }
// request.post('https://graph.facebook.com/v2.6/me/messages')
// .query({access_token: fbAccessToken})
// .send({
// recipient: {id: sender},
// message,
// })
// .end(callback);
// function callback (error, response) {
// if (error) {
// fs.writeFile('fb_log.json', JSON.stringify(error, null, '\t'), 'utf8', () => {});
// } else if (response.body.error) {
// console.warn('Error: ', response.body.error)
// }
// }
// })
// }
module.exports = handleMessageEvent;