├── renovate.json
├── public
├── background.jpg
└── index.html
├── config
└── default-example.json
├── package.json
├── LICENSE
├── .gitignore
├── README.md
└── index.js
/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "config:base"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/public/background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intelligo-dev/jisho-bot/HEAD/public/background.jpg
--------------------------------------------------------------------------------
/config/default-example.json:
--------------------------------------------------------------------------------
1 | {
2 | "APP_SECRET": "",
3 | "PAGE_ACCESS_TOKEN": "",
4 | "VALIDATION_TOKEN": ""
5 | }
6 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Intelligo Bot Demo
5 |
6 |
7 |
8 | Intelligo Bot Demo
9 |
10 |
11 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jisho-bot",
3 | "version": "0.0.1",
4 | "description": "Jisho chatbot",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "node index.js"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/intelligo-systems/jisho-bot.git"
12 | },
13 | "keywords": [
14 | "bot",
15 | "ai",
16 | "memory",
17 | "intelligo",
18 | "chatbot",
19 | "messenger-bot"
20 | ],
21 | "author": "Turtuvshin Byambaa",
22 | "license": "MIT",
23 | "bugs": {
24 | "url": "https://github.com/intelligo-systems/jisho-bot/issues"
25 | },
26 | "homepage": "https://github.com/intelligo-systems/jisho-bot#readme",
27 | "dependencies": {
28 | "async": "^3.0.0",
29 | "config": "^2.0.1",
30 | "express": "^4.16.3",
31 | "intelligo": "^1.0.0",
32 | "request": "^2.87.0",
33 | "path": "^0.12.7"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Intelligo Systems
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 |
8 | # Runtime data
9 | pids
10 | *.pid
11 | *.seed
12 | *.pid.lock
13 |
14 | # Directory for instrumented libs generated by jscoverage/JSCover
15 | lib-cov
16 |
17 | # Coverage directory used by tools like istanbul
18 | coverage
19 |
20 | # nyc test coverage
21 | .nyc_output
22 |
23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24 | .grunt
25 |
26 | # Bower dependency directory (https://bower.io/)
27 | bower_components
28 |
29 | # node-waf configuration
30 | .lock-wscript
31 |
32 | # Compiled binary addons (https://nodejs.org/api/addons.html)
33 | build/Release
34 |
35 | # Dependency directories
36 | node_modules/
37 | jspm_packages/
38 |
39 | # TypeScript v1 declaration files
40 | typings/
41 |
42 | # Optional npm cache directory
43 | .npm
44 |
45 | # Optional eslint cache
46 | .eslintcache
47 |
48 | # Optional REPL history
49 | .node_repl_history
50 |
51 | # Output of 'npm pack'
52 | *.tgz
53 |
54 | # Yarn Integrity file
55 | .yarn-integrity
56 |
57 | # dotenv environment variables file
58 | .env
59 |
60 | # next.js build output
61 | .next
62 |
63 | # environment variables file
64 | config/default.json
65 |
66 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Jisho bot
2 |
3 | Japanese-English dictionary Messenger bot using [intelligo framework](https://github.com/intelligo-systems/intelligo) and [www.jisho.org](https://jisho.org/) public API.
4 |
5 | ## How to run
6 |
7 | clone this repo.
8 |
9 | ```bash
10 | $ git clone https://github.com/intelligo-systems/jisho-bot.git && cd jisho-bot
11 | ```
12 |
13 | Set the values in `config/default.json` before running the bot. Using your Facebook Page's / App's `ACCESS_TOKEN`, `VERIFY_TOKEN` and `APP_SECRET`
14 |
15 | - `ACCESS_TOKEN:` A page access token for your app, found under App -> Products -> Messenger -> Settings -> Token Generation
16 | - `VERIFY_TOKEN:` A token that verifies your webhook is being called. Can be any value, but needs to match the value in App -> Products -> Webhooks -> Edit Subscription
17 | - `APP_SECRET:` A app secret for your app, found under App -> Settings -> Basic -> App Secret -> Show
18 |
19 | **Note:** If you don't know how to get these tokens, take a look at Facebook's [Quick Start Guide](https://developers.facebook.com/docs/messenger-platform/guides/quick-start) .
20 |
21 | Install dependencies:
22 |
23 | ```bash
24 | $ npm install
25 | ```
26 |
27 | Start your bot server:
28 |
29 | ```bash
30 | $ npm start
31 | ```
32 |
33 | ## Contributors
34 |
35 | - 📥 Pull requests and 🌟 Stars are always welcome.
36 | - You may contribute in several ways like creating new features, fixing bugs, improving documentation and examples
37 | or translating any document here to your language.
38 |
39 | ## Supporting
40 |
41 | If you'd like to join them, please consider:
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | ## License
54 |
55 | > Copyright (C) 2019 Intelligo Systems.
56 | > Intelligo framework is open-sourced software licensed under the [MIT](https://opensource.org/licenses/MIT) license.
57 | > (See the [LICENSE](https://github.com/intelligo-systems/intelligo/blob/master/LICENSE) file for the whole license text.)
58 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const express = require('express'),
4 | config = require('config'),
5 | request = require('request'),
6 | async = require('async'),
7 | fs = require('fs'),
8 | path = require('path'),
9 | Intelligo = require('intelligo');
10 |
11 | const app = express();
12 |
13 | const PAGE_ACCESS_TOKEN = (process.env.PAGE_ACCESS_TOKEN) ?
14 | (process.env.PAGE_ACCESS_TOKEN) :
15 | config.get('PAGE_ACCESS_TOKEN');
16 |
17 | const VALIDATION_TOKEN = (process.VALIDATION_TOKEN) ?
18 | (process.env.VALIDATION_TOKEN) :
19 | config.get('VALIDATION_TOKEN');
20 |
21 | const APP_SECRET = (process.env.APP_SECRET) ?
22 | (process.env.APP_SECRET) :
23 | config.get('APP_SECRET');
24 |
25 | const bot = new Intelligo.MessengerBot({
26 | PAGE_ACCESS_TOKEN: PAGE_ACCESS_TOKEN,
27 | VALIDATION_TOKEN: VALIDATION_TOKEN,
28 | APP_SECRET: APP_SECRET,
29 | FB_URL: 'https://graph.facebook.com/v3.1/',
30 | app: app
31 | });
32 |
33 | bot.initWebhook();
34 | bot.addGreeting("Japanese-English-Mongolian dictionary chatbot. It lets you find words, kanji and more quickly and easily.");
35 | bot.addGetStartedButton();
36 | bot.addPersistentMenu([
37 | {
38 | "locale":"default",
39 | "composer_input_disabled": false,
40 | "call_to_actions":[
41 | {
42 | "title":"Home",
43 | "type":"postback",
44 | "payload":"HOME"
45 | },
46 | {
47 | "title":"About",
48 | "type":"nested",
49 | "call_to_actions":[
50 | {
51 | "type":"web_url",
52 | "title":"Jisho.org",
53 | "url":"https://www.jisho.org",
54 | "webview_height_ratio":"full"
55 | },
56 | {
57 | "type":"web_url",
58 | "title":"Source code",
59 | "url":"https://github.com/techstar-cloud/memorize-bot",
60 | "webview_height_ratio":"full"
61 | }
62 | ]
63 | },
64 | {
65 | "title":"Contact Info",
66 | "type":"postback",
67 | "payload":"CONTACT"
68 | }
69 | ]
70 | }
71 | ]);
72 |
73 |
74 | bot.on('message', (event) => {
75 |
76 | const senderID = event.sender.id,
77 | message = event.message;
78 |
79 | if (message.text) {
80 | bot.sendTypingOn(senderID);
81 |
82 | request({
83 | uri: 'https://jisho.org/api/v1/search/words?keyword='+message.text.toLowerCase(),
84 |
85 | }, function (error, response, body) {
86 | if (!error) {
87 |
88 | let res = JSON.parse(body);
89 |
90 | let dataSet = [];
91 |
92 | if (res.data.length > 0) {
93 |
94 | async.eachLimit(res.data, 5, function(content, callback){
95 |
96 | let definitions = [];
97 | for (let i = 0; i < content.senses.length; i++){
98 | definitions.push(content.senses[i].english_definitions);
99 | }
100 | let senses = definitions.join("\n");
101 |
102 | // markdown format
103 | // space after commas
104 | senses = senses.replace(/,/gm, ', ');
105 | // adds a bullet at the beginning of each line
106 | senses = senses.replace(/^/gm, '・');
107 |
108 | // message with the result to send
109 | let word_defined = (content.japanese[0].word != undefined) ? content.japanese[0].word : content.japanese[0].reading;
110 |
111 | dataSet.push({
112 | title: content.japanese[0].reading+" : "+word_defined,
113 | subtitle: senses,
114 | item_url: "http://jisho.org/search/" + word_defined,
115 | image_url: "https://techstar-cloud-tortuvshin.c9users.io/background.jpg",
116 | buttons: [
117 | {
118 | "type":"web_url",
119 | "title":"Search jisho",
120 | "url":"http://jisho.org/search/" + word_defined,
121 | "webview_height_ratio":"full"
122 | }
123 | ]
124 | });
125 |
126 | }, function(error){
127 | console.error(error);
128 | });
129 |
130 | console.log(dataSet);
131 | bot.sendGenericMessage(senderID, dataSet);
132 | }
133 | // if the query doesn't return any result
134 | else {
135 | bot.sendTextMessage(senderID, "Result not found ");
136 | }
137 | } else {
138 | console.error("Failed calling jisho API");
139 | }
140 | });
141 | bot.sendTypingOff(senderID);
142 | }
143 | });
144 |
145 | app.use(express.static(path.join(__dirname, "public")));
146 | app.set('port', process.env.PORT || 8080);
147 | app.listen(app.get('port'), function() {
148 | console.log('Server is running on port', app.get('port'));
149 | });
150 |
--------------------------------------------------------------------------------