Skip to content

Commit

Permalink
Add support configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Augsorn Chanklad committed Jul 9, 2019
1 parent edd0649 commit 9d97107
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Access your Dyno and check at tab **settings** and **Reveal Config Vars**. you c
- Webhook URL: __https://{your_heroku_app_name}.herokuapp.com/callback__.
- If you wish your bot can join group chats. Then enable __Allow bot to join group chats__.

## Global config
Any config can put here as you want. You can overwrite global_config.json via Firebase too. Sample structure is in sample/app.json. Restart is need for update config.

## Usage

### Query item from poporing.life
Expand Down
18 changes: 18 additions & 0 deletions global_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"message": {
"prefix_help": "ทุกคำสั่งกรุณาใส่เครื่องหมาย ! นำหน้าด้วยนะคะ (เช่น !taming)",
"command_not_found": [
"ไม่รู้จัก{}เลยค่ะ","{}คืออะไรคะ? ' ')?",
"ไม่มี{}นะคะ","{}ไม่มีในความทรงจำของฉันเลยค่ะ",
"ไม่พบคำสั่ง{} ค่ะ ลองพิมพ์ !help เพื่อดูคำสั่งที่ถูกต้องนะคะ"
],
"item_not_found": [
"ไม่รู้จัก{}เลยค่ะ",
"{}คืออะไรคะ? ' ')?",
"ไม่มี{}นะคะ",
"{}ไม่มีในความทรงจำของฉันเลยค่ะ",
"ไม่พบไอเท็ม{} ค่ะ",
"ไอ้นี่มันมีอยู่จริงๆเหรอคะ?"
]
}
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const lineConfig = require('./src/line.config');
const eventMessageHandler = require('./src/event-message.handler');
const express = require('express');
const firebase = require('./src/firebase');

const app = express();

Expand Down Expand Up @@ -38,4 +39,5 @@ app.get('/',(req, res) => {
const port = process.env.PORT || 3200;
app.listen(port, () => {
console.log(`listening on ${port}`);
firebase.getAppConfig();
});
17 changes: 17 additions & 0 deletions sample/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"app": {
"message": {
"item_not_found": [
"ไม่รู้จัก{}เลยค่ะ","{}คืออะไรคะ? ' ')?",
"ไม่มี{}นะคะ","{}ไม่มีในความทรงจำของฉันเลยค่ะ",
"ไม่พบไอเท็ม{} ค่ะ","ไอ้นี่มันมีอยู่จริงๆเหรอคะ?"
],
"prefix_help": "ทุกคำสั่งกรุณาใส่เครื่องหมาย ! นำหน้าด้วยนะคะ (เช่น !taming)",
"command_not_found": [
"ไม่รู้จัก{}เลยค่ะ","{}คืออะไรคะ? ' ')?",
"ไม่มี{}นะคะ","{}ไม่มีในความทรงจำของฉันเลยค่ะ",
"ไม่พบคำสั่ง{} ค่ะ ลองพิมพ์ !help เพื่อดูคำสั่งที่ถูกต้องนะคะ"
]
}
}
}
34 changes: 34 additions & 0 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const axios = require('axios');
const cache = require('./cache');
const moment = require('moment');
const romEvents = require('./rom.events');
const fs = require('fs');
// For local development
// var serviceAccount = require('./../serviceAccountKey.json');

Expand Down Expand Up @@ -220,6 +221,39 @@ module.exports = {
});
});
},
getAppConfig: () => {

const key = 'firebase_get_app_config';

return cache.get(key)
.then(value => {

if(value) {
console.log('Cache [' + key + '] exists. Get config from cache.');
return value;
}

console.log('Cache [' + key + '] not found. Get config from firebase');

return db.collection('app').get()
.then((snapshot) => {

let appConfig = {}
snapshot.docs.forEach((doc) => {
appConfig[doc.id] = doc.data();
})

fs.writeFileSync(__dirname + '/../global_config.json', JSON.stringify(appConfig),{encoding:'utf8',flag:'w'});

console.log('config updated!');

return appConfig;
})
.catch((err) => {
console.log('Error getting documents', err);
});
});
},
downloadFile: filename => {
return admin.storage().bucket().file(filename).download({
destination: './ref/' + filename
Expand Down
3 changes: 2 additions & 1 deletion src/kafra.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Chance = require('chance')
const chance = new Chance();
const fs = require('fs');
const customCmd = require('./custom.cmd');
const globalConfig = require('./../global_config.json');

const compare = (a,b) => {
if (a.name < b.name)
Expand All @@ -16,7 +17,7 @@ const compare = (a,b) => {
}

const generateHelp = cmdList => {
let content = 'ทุกคำสั่งกรุณาใส่เครื่องหมาย ! นำหน้าด้วยนะคะ (เช่น !taming)\n';
let content = globalConfig.message.prefix_help + '\n';

content += cmdList
.sort(compare)
Expand Down
5 changes: 3 additions & 2 deletions src/message.default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Chance = require('chance')
const config = require('./../global_config.json');

module.exports = {
"command_not_found": [ "ไม่รู้จัก{}เลยค่ะ", "{}คืออะไรคะ? ' ')? ", "ไม่มี{}นะคะ", "{}ไม่มีในความทรงจำของฉันเลยค่ะ", "ไม่พบคำสั่ง{} ค่ะ ลองพิมพ์ !help เพื่อดูคำสั่งที่ถูกต้องนะคะ"],
"item_not_found": [ "ไม่รู้จัก{}เลยค่ะ", "{}คืออะไรคะ? ' ')? ", "ไม่มี{}นะคะ", "{}ไม่มีในความทรงจำของฉันเลยค่ะ", "ไม่พบไอเท็ม{} ค่ะ", "ไอ้นี่มันมีอยู่จริงๆเหรอคะ?"]
"command_not_found": config.message.command_not_found,
"item_not_found": config.message.item_not_found
}

0 comments on commit 9d97107

Please sign in to comment.