-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
125 lines (108 loc) · 3.41 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
'use strict';
const express = require('express'),
config = require('config'),
request = require('request'),
async = require('async'),
fs = require('fs'),
Intelligo = require('intelligo');
const app = express();
const PAGE_ACCESS_TOKEN = (process.env.PAGE_ACCESS_TOKEN) ?
(process.env.PAGE_ACCESS_TOKEN) :
config.get('PAGE_ACCESS_TOKEN');
const VALIDATION_TOKEN = (process.VALIDATION_TOKEN) ?
(process.env.VALIDATION_TOKEN) :
config.get('VALIDATION_TOKEN');
const APP_SECRET = (process.env.APP_SECRET) ?
(process.env.APP_SECRET) :
config.get('APP_SECRET');
const bot = new Intelligo({
PAGE_ACCESS_TOKEN: PAGE_ACCESS_TOKEN,
VALIDATION_TOKEN: VALIDATION_TOKEN,
APP_SECRET: APP_SECRET,
FB_URL: 'https://graph.facebook.com/v3.1/',
app: app
});
bot.initWebhook();
bot.addGreeting("Japanese-English-Mongolian dictionary chatbot. It lets you find words, kanji and more quickly and easily.");
bot.addGetStartedButton();
bot.addPersistentMenu([
{
"locale":"default",
"composer_input_disabled": false,
"call_to_actions":[
{
"title":"Home",
"type":"postback",
"payload":"HOME"
},
{
"title":"Nested Menu Example",
"type":"nested",
"call_to_actions":[
{
"title":"Who am I",
"type":"postback",
"payload":"WHO"
},
{
"title":"Joke",
"type":"postback",
"payload":"joke"
},
{
"title":"Contact Info",
"type":"postback",
"payload":"CONTACT"
}
]
},
{
"type":"web_url",
"title":"Source code",
"url":"https://github.com/techstar-cloud/memorize-bot",
"webview_height_ratio":"full"
}
]
}
]);
// let dataSet = [];
// function getWords() {
// request({
// uri: 'https://jisho.techstar.cloud/api/words',
// }, function (error, response, body) {
// if (!error) {
// const parsed = JSON.parse(body);
// const words = parsed.memorize;
// async.each(words, function(word, callback){
// dataSet.push({input: word.meanings, output: { meanings: word.meanings, meaningsMongolia: word.meaningsMongolia, character: word.character, kanji: word.kanji, partOfSpeech: word.partOfSpeech } });
// }, function(error){
// console.error(error);
// });
// bot.learn(dataSet);
// } else {
// console.error("Failed calling jisho API");
// }
// });
// }
// getWords();
//Train the neural network with an array of training data.
console.log("Starting training");
bot.learn(JSON.parse(fs.readFileSync('data/train.json', 'utf8')));
console.log("Finish training");
//Subscribe to messages sent by the user with the bot.on() method.
bot.on('message', (event) => {
const senderID = event.sender.id,
message = event.message;
if (message.text) {
const result = bot.answer(message.text.toLowerCase());
if (result == null || result == '') {
bot.sendTextMessage(senderID, "Мэдэхгүй үг байна. ");
} else {
bot.sendTextMessage(senderID, result);
}
}
});
app.set('port', process.env.PORT || 8080);
app.listen(app.get('port'), function() {
console.log('Server is running on port', app.get('port'));
});