Skip to content

Commit

Permalink
Merge pull request #30 from iswanj/fixes-eslint-issues
Browse files Browse the repository at this point in the history
#28 fixed lint issues
  • Loading branch information
supasate authored Oct 3, 2016
2 parents 7b9b028 + aaf40cd commit 01801ef
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"browser": true,
"es6": true
},
rules: {
"no-underscore-dangle": 0,
"no-console": 0,
"global-require": 0,
},
"plugins": []
}
27 changes: 13 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const bodyParser = require('body-parser');
const config = require('config');
const express = require('express');
Expand Down Expand Up @@ -29,6 +27,7 @@ const API_PASSWORD = (process.env.API_PASSWORD) ?
const API_USER_ID = (process.env.API_USER_ID) ?
process.env.API_USER_ID : config.get('apiUserId');

// eslint-disable-next-line max-len
if (!(APP_SECRET && VALIDATION_TOKEN && PAGE_ACCESS_TOKEN && API_URI && API_USERNAME && API_PASSWORD && API_USER_ID)) {
console.error('Missing config values');
process.exit(1);
Expand All @@ -53,9 +52,10 @@ const m = require('./messenger.js')(PAGE_ACCESS_TOKEN);
const conversation = require('./conversation.js')(config.get('sessionMaxLength'));

// Youpin API utils
const api_lib = require('./youpin-api.js');
var youpin;
new api_lib(API_URI, API_USERNAME, API_PASSWORD).then(function(api) {
const ApiLib = require('./youpin-api.js');

let youpin;
new ApiLib(API_URI, API_USERNAME, API_PASSWORD).then((api) => {
// Youpin bot
youpin = require('./youpin.js')(m, api, conversation, API_USER_ID);
}).catch(err => {
Expand All @@ -66,13 +66,13 @@ new api_lib(API_URI, API_USERNAME, API_PASSWORD).then(function(api) {
});

// Index route
app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.send('ทดลองคุยกับป้ายุพินได้ที่ https://m.me/youpin.city.test');
});


// Webhook verification
app.get('/webhook/', function (req, res) {
app.get('/webhook/', (req, res) => {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === config.get('validationToken')) {
res.status(200).send(req.query['hub.challenge']);
Expand All @@ -82,7 +82,7 @@ app.get('/webhook/', function (req, res) {


// Handle messages
app.post('/webhook/', function(req, res) {
app.post('/webhook/', (req, res) => {
// Verify signature
if (req.isXHub) {
if (req.isXHubValid()) {
Expand All @@ -94,22 +94,21 @@ app.post('/webhook/', function(req, res) {
return;
}

let data = req.body;
if (data.object == 'page') {
data.entry.forEach((pageEntry) => {
const data = req.body;
if (data.object === 'page') {
data.entry.forEach((pageEntry) => {
pageEntry.messaging.forEach((msgEvent) => {
if (msgEvent.message || msgEvent.postback) {
youpin.onMessaged(msgEvent);
} else {
console.log('Webhook received unhandled messaging event: ' +
msgEvent);
console.log(`Webhook received unhandled messaging event: ${msgEvent}`);
}
});
});
}
});


app.listen(app.get('port'), function() {
app.listen(app.get('port'), () => {
console.log(`Node app is running on port ${app.get('port')}`);
});
90 changes: 44 additions & 46 deletions messenger.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const request = require('request');

module.exports = (PAGE_ACCESS_TOKEN) => {

function _callSendAPI(messageData) {
request(
{
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData,
headers: {'Content-Type': 'application/json'},
headers: { 'Content-Type': 'application/json' },
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
(error, response, body) => {
if (!error && response.statusCode === 200) {
console.log('Successfully sent generic message ' +
`with id ${body.message_id} to recipient ${body.recipient_id}`);
} else {
Expand All @@ -25,101 +24,101 @@ module.exports = (PAGE_ACCESS_TOKEN) => {
}

return {
sendImage: function(userid, url) {
sendImage(userid, url) {
const messageData = {
recipient: {
id: userid
id: userid,
},
message: {
attachment: {
type: 'image',
payload: {
url: url
}
}
}
url,
},
},
},
};

_callSendAPI(messageData);
},

sendText: function(userid, text) {
sendText(userid, text) {
const messageData = {
recipient: {
id: userid
id: userid,
},
message: {
text: text
}
text,
},
};
_callSendAPI(messageData);
},

sendTextWithReplies: function(userid, text, replies) {
sendTextWithReplies(userid, text, replies) {
const messageData = {
recipient: {
id: userid
id: userid,
},
message: {
text: text,
quick_replies: replies
}
text,
quick_replies: replies,
},
};
_callSendAPI(messageData);
},

sendButton: function(userid, text, buttons) {
sendButton(userid, text, buttons) {
const messageData = {
recipient: {
id: userid
id: userid,
},
message: {
attachment: {
type: 'template',
payload: {
template_type: 'button',
text: text,
buttons: buttons
}
}
}
text,
buttons,
},
},
},
};

_callSendAPI(messageData);
},

sendGeneric: function(userid, elements) {
sendGeneric(userid, elements) {
const messageData = {
recipient: {
id: userid
id: userid,
},
message: {
attachment: {
type: 'template',
payload: {
template_type: 'generic',
elements: elements
}
}
}
elements,
},
},
},
};

_callSendAPI(messageData);
},

getProfile: function(userid, callback) {
getProfile(userid, callback) {
request(
{
uri: 'https://graph.facebook.com/v2.6/' + userid,
uri: `https://graph.facebook.com/v2.6/${userid}`,
qs: {
fields: 'first_name,last_name,profile_pic,locale,timezone,gender',
access_token: PAGE_ACCESS_TOKEN
access_token: PAGE_ACCESS_TOKEN,
},
method: 'GET',
json: true
json: true,
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
(error, response, body) => {
if (!error && response.statusCode === 200) {
callback(body);
} else {
console.error('Unable to get user profile.');
Expand All @@ -130,21 +129,20 @@ module.exports = (PAGE_ACCESS_TOKEN) => {
);
},

createPostbackButton: function(title, payload) {
createPostbackButton(title, payload) {
return {
type: 'postback',
title: title,
payload: payload
title,
payload,
};
},

createQuickReplyButton: function(title, payload) {
createQuickReplyButton(title, payload) {
return {
content_type: 'text',
title: title,
payload: payload
title,
payload,
};
}
},
};

};
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
"dependencies": {
"asyncawait": "^1.0.6",
"bluebird": "^3.4.3",
"string-saw": "0.0.25",
"body-parser": "^1.15.2",
"config": "^1.21.0",
"eslint-plugin-import": "^1.9.2",
"eslint-plugin-jsx-a11y": "^1.5.3",
"eslint-plugin-react": "^5.2.2",
"express": "^4.14.0",
"express-x-hub": "^1.0.4",
"feathers": "^2.0.1",
Expand All @@ -38,7 +36,6 @@
"eslint-plugin-import": "^1.9.2",
"eslint-plugin-jsx-a11y": "^1.5.3",
"eslint-plugin-react": "^5.2.2",
"lodash": "^4.15.0",
"string-saw": "0.0.25"
"lodash": "^4.15.0"
}
}
10 changes: 5 additions & 5 deletions scripts/i18n-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const files = ['youpin.js'];

function extractKeysFromFile(file) {
return fs.readFileAsync(file, 'utf8')
.then(data => {
.then((data) => {
const keys = saw(data)
.split(os.EOL)
.map(line => saw(line)
Expand All @@ -26,7 +26,7 @@ function extractKeysFromFile(file) {
function updateKeysToFile(keys, lang) {
const file = `${i18nConfig.directory}/${lang}.json`;
fs.readFileAsync(file, 'utf8')
.then(data => {
.then((data) => {
console.log(`Updating ${file}`);
const oldKeys = JSON.parse(data);
const mergedKeys = _.merge(keys, oldKeys);
Expand All @@ -38,19 +38,19 @@ function updateKeysToFile(keys, lang) {
});
}

Promise.map(files, extractKeysFromFile).then(items => {
Promise.map(files, extractKeysFromFile).then((items) => {
const keys = _.chain(items)
.flatten()
.uniq()
.sort()
.transform((obj, a) => {
obj[a] = '';
obj[a] = ''; // eslint-disable-line
}, {})
.value();

console.log(`We have ${_.keys(keys).length} i18n keys.`);

_.each(i18nConfig.locales, lang => {
_.each(i18nConfig.locales, (lang) => {
updateKeysToFile(keys, lang);
});
});
Loading

0 comments on commit 01801ef

Please sign in to comment.