Skip to content

Commit

Permalink
S12-010-read-info-for-user
Browse files Browse the repository at this point in the history
  • Loading branch information
jbergant committed Nov 19, 2019
1 parent 0404788 commit f3033af
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pg = require('pg');
pg.defaults.ssl = true;

const userService = require('./user');
const colors = require('./colors');

// Messenger API parameters
if (!config.FB_PAGE_TOKEN) {
Expand Down Expand Up @@ -228,6 +229,13 @@ function handleEcho(messageId, appId, metadata) {

function handleDialogFlowAction(sender, action, messages, contexts, parameters) {
switch (action) {
case "iphone_colors":
colors.readAllColors(function (allColors) {
let allColorsString = allColors.join(', ');
let reply = `IPhone xxx is available in ${allColorsString}. What is your favourite color?`;
sendTextMessage(sender, reply);
});
break;
case "get-current-weather":
if ( parameters.fields.hasOwnProperty('geo-city') && parameters.fields['geo-city'].stringValue!='') {
request({
Expand Down
76 changes: 76 additions & 0 deletions colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';
const request = require('request');
const config = require('./config');
const pg = require('pg');
pg.defaults.ssl = true;

module.exports = {

readAllColors: function(callback) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
client
.query(
'SELECT color FROM public.iphone_colors',
function(err, result) {
if (err) {
console.log(err);
callback([]);
} else {
let colors = [];
for (let i = 0; i < result.rows.length; i++) {
colors.push(result.rows[i]['color']);
}
callback(colors);
};
});
});
pool.end();
},


readUserColor: function(callback, userId) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
client
.query(
'SELECT color FROM public.users WHERE fb_id=$1',
[userId],
function(err, result) {
if (err) {
console.log(err);
callback('');
} else {
callback(result.rows[0]['color']);
};
});

});
pool.end();
},

updateUserColor: function(color, userId) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
let sql = 'UPDATE public.users SET color=$1 WHERE fb_id=$2';
client.query(sql,
[
color,
userId
]);

});
pool.end();
}


}

0 comments on commit f3033af

Please sign in to comment.