From 27a12c35a19ef2d02219d124de32dd26bccc9f47 Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Sun, 29 Apr 2018 13:45:34 -0400 Subject: [PATCH 1/6] Setting up DB --- app.js | 3 ++ functions/handleCases.js | 42 +++++++++++++++++++++++++-- functions/informationLookup.js | 11 +++++-- functions/receive-message.js | 39 +++++++++++-------------- functions/sendResponse.js | 5 ++++ functions/updateDB.js | 52 +++++++++++++++++++++++++++++----- routes/webhooks.js | 4 +++ 7 files changed, 121 insertions(+), 35 deletions(-) diff --git a/app.js b/app.js index ea4b1b9..f56b3bb 100644 --- a/app.js +++ b/app.js @@ -23,6 +23,9 @@ app.listen(process.env.PORT || 1337, () => { // Update all the matches that are stored in the database. updateDB.updateAllCurrentMatches(); + // Set up all previous Reminders + updateDB.setAllReminders(); + }); app.use(bodyParser.json()); diff --git a/functions/handleCases.js b/functions/handleCases.js index 34fcdc0..9d3b46c 100644 --- a/functions/handleCases.js +++ b/functions/handleCases.js @@ -43,6 +43,20 @@ function popularTeam(sender_psid) { sendResponse.quickReply(sender_psid, response, 'POPULART', key); } + +function anotherTeam(sender_psid) { + const handleChoice = db.ref("HandleChoices/"); + + handleChoice.child(sender_psid).set({ + "choice": "TEAM" + }) + + let response = { + 'text': 'Please give us the team name' + }; + sendResponse.directMessage(sender_psid, response); +} + // Provides 11 popular players options function popularPlayer(sender_psid) { let key = ['Ronaldo', 'Messi', 'Bale', 'Neymar', 'Hazard', 'Morata', 'Ozil', 'Kroos', 'Isco', 'Alexis', 'Salad']; @@ -52,6 +66,19 @@ function popularPlayer(sender_psid) { sendResponse.quickReply(sender_psid, response, 'POPULARP', key); } +function anotherPlayer(sender_psid) { + const handleChoice = db.ref("HandleChoices/"); + + handleChoice.child(sender_psid).set({ + "choice": "PLAYER" + }) + + let response = { + 'text': 'Please give us the team name' + }; + sendResponse.directMessage(sender_psid, response); +} + // Repeats the main bot function. function getContinue(sender_psid) { let key = ['Yes', 'No']; @@ -63,7 +90,6 @@ function getContinue(sender_psid) { // Ask whether the user want to set reminder or not function askReminder(sender_psid, match) { - let key = ['Yes', 'No']; let response = { 'text': 'Do you want to Set Reminder for this match?' } @@ -86,12 +112,17 @@ function setReminder(sender_psid, key) { // Set reminder using setTimeout else if (timeDif > 0) { + + // Save reminders to database + db.ref("Reminders/" + sender_psid + "/").push(match.val()) + setTimeout(() => { var response = { - 'text': `In 15 minutes:\n${match.val().team1} vs ${match.val().team2}` + 'text': `In ${new Date(timeDif).getMinutes()} minutes:\n${match.val().team1} vs ${match.val().team2}` }; sendResponse.directMessage(sender_psid, response); - }, (new Date(match.val().time)) - (new Date()) - 900000); + + }, timeDif - 910000); var response = { 'text': 'Reminder is set.' @@ -99,12 +130,15 @@ function setReminder(sender_psid, key) { sendResponse.directMessage(sender_psid, response); } + // Match is played else if (timeDif < -7200000) { var response = { 'text': 'This match has been played.' }; sendResponse.directMessage(sender_psid, response); } + + // Match is playing else { var response = { 'text': `${match.val().team1} is playing again ${match.val().team2} right now` @@ -118,7 +152,9 @@ module.exports = { getStart, teamOptions, popularTeam, + anotherTeam, popularPlayer, + anotherPlayer, getContinue, askReminder, setReminder diff --git a/functions/informationLookup.js b/functions/informationLookup.js index 02b1c9e..a628c74 100644 --- a/functions/informationLookup.js +++ b/functions/informationLookup.js @@ -217,6 +217,10 @@ function playerLookup(sender_psid, key) { allPlayers.forEach((eachPlayer) => { if (eachPlayer.key.includes(key)) { flag = false; + + // Increase the number of search for the player. + updateDB.popularPlayer(eachPlayer.key); + sendResponse.playerReply(sender_psid, eachPlayer.val().playerTitle, eachPlayer.val().playerSubtitle, eachPlayer.val().playerImageURL, eachPlayer.val().playerURL); @@ -247,7 +251,7 @@ function playerLookup(sender_psid, key) { flag = false; let playerImageURL = reply[0]; let playerURL = reply[1]; - let eachPlayer = reply[2]; + let playerName = reply[2]; let playerTitle = reply[2] + ' - ' + reply[3]; let playerSubtitle = reply[4]; for (var eachData = 5; eachData < 7; eachData++) { @@ -266,14 +270,15 @@ function playerLookup(sender_psid, key) { } // Save search result to database - db.ref('Players/' + dataFormat.cleanKeyDB(eachPlayer).toUpperCase() + '/').set({ + db.ref('Players/' + dataFormat.cleanKeyDB(playerName).toUpperCase() + '/').set({ 'playerURL': playerURL, 'playerTitle': playerTitle, 'playerSubtitle': playerSubtitle, 'playerImageURL': playerImageURL }); - + // Increase the number of search for the player. + updateDB.popularPlayer(teamName.toUpperCase()); console.log("replied"); sendResponse.playerReply(sender_psid, playerTitle, playerSubtitle, playerImageURL, playerURL); diff --git a/functions/receive-message.js b/functions/receive-message.js index 551ec8b..dd0a37c 100644 --- a/functions/receive-message.js +++ b/functions/receive-message.js @@ -139,14 +139,23 @@ function handleQuickReply(sender_psid, received_message) { } } - // Sets reminder for a match + // handle Reminder answer. if (key.includes('REMINDER')) { - if (key.includes('YES')) { - handleCases.setReminder(sender_psid, key); + + // Set reminder for a match + if (!(key.includes('ANOTHERTEAM'))) { + if (key.includes('YES')) { + handleCases.setReminder(sender_psid, key); + } + setTimeout(() => { + handleCases.getContinue(sender_psid); + }, 1000) + } + + // Choose another team + else { + handleCases.anotherTeam(sender_psid); } - setTimeout(() => { - handleCases.getContinue(sender_psid); - }, 1000) } } @@ -157,26 +166,12 @@ function handlePostback(sender_psid, messagePostback) { // Search for different player name if (payload.includes('ANOTHERPLAYER')) { - handleChoice.child(sender_psid).set({ - "choice": "PLAYER" - }) - - response = { - 'text': 'Please give us the player name' - }; - sendResponse.directMessage(sender_psid, response); + handleCases.anotherPlayer(sender_psid); } // Search for different team name else if (payload.includes('Another Team')) { - handleChoice.child(sender_psid).set({ - "choice": "TEAM" - }) - - response = { - 'text': 'Please give us the team name' - }; - sendResponse.directMessage(sender_psid, response); + handleCases.anotherTeam(sender_psid) } // Looking for match's information diff --git a/functions/sendResponse.js b/functions/sendResponse.js index 45582ad..2cc114f 100644 --- a/functions/sendResponse.js +++ b/functions/sendResponse.js @@ -233,6 +233,11 @@ function sendReminder(sender_psid, response, payloadCharacteristic, match) { 'content_type': 'text', 'title': 'No, thanks', 'payload': payloadCharacteristic + 'NO' + }, + { + 'content_type': 'text', + 'title': 'Another Team', + 'payload': payloadCharacteristic + 'ANOTHERTEAM' } ] } diff --git a/functions/updateDB.js b/functions/updateDB.js index 99becf4..ca5c2e2 100644 --- a/functions/updateDB.js +++ b/functions/updateDB.js @@ -106,6 +106,9 @@ function dbNextGame(key, iniTime) { data.get_next_game(key, (err, reply) => { if (err) { console.log("Error occured on Server for MATCH: " + key); + console.error("################################"); + console.error(err); + console.error("################################"); running = false; } else { @@ -187,16 +190,50 @@ function updateMatchesFromTeams() { }) } -// Testing +// Set all Reminders when restart sever. function setAllReminders() { - db.ref('Reminder/').once("value", (newVal) => { - newVal.forEach((newVal1) => { - newVal1.forEach((newVal2) => { - handleCases.setReminder(newVal1.key, newVal2.val().team); + db.ref("Reminders/").once("value", (allSenderPSID) => { + allSenderPSID.forEach((eachSenderPSID) => { + var sender_psid = eachSenderPSID.key; + eachSenderPSID.forEach((matchInfo) => { + var timeDif = new Date(matchInfo.val().time) - new Date(); + + if (timeDif > 0) { + setTimeout(() => { + var response = { + 'text': `In ${new Date(timeDif).getMinutes()} minutes:\n${matchInfo.val().team1} vs ${matchInfo.val().team2}` + }; + sendResponse.directMessage(sender_psid, response); + + db.ref("Reminders/").child(sender_psid).child(matchInfo.key).set({}); + }, timeDif - 910000); + } + + else { + db.ref("Reminders/").child(sender_psid).child(matchInfo.key).set({}); + } }) }) - }); - console.log("All remiders have been set."); + + console.log("All reminders have been set"); + }) +} + +// Save the number of searches have been performed for a player. +function popularPlayer(playerName) { + db.ref("PopularPlayers").child(playerName).once("value", (result) => { + if (!(result.exists())) { + db.ref("PopularPlayers/").child(playerName).set({ + "searchCount": 1 + }) + } + + else { + db.ref("PopularPlayers").child(playerName).set({ + "searchCount": result.val().searchCount + 1 + }) + } + }) } // Give back all the matches that are currently observed and updated @@ -220,6 +257,7 @@ module.exports = { clearOldMatches, updateAllCurrentMatches, updateMatchesFromTeams, + popularPlayer, getOnGoing, setRunning, getRunning, diff --git a/routes/webhooks.js b/routes/webhooks.js index 441d661..7a5c125 100644 --- a/routes/webhooks.js +++ b/routes/webhooks.js @@ -29,6 +29,10 @@ router.post('/', (req, res) => { } else if (webhook_event.message.quick_reply) { receive.handleQuickReply(sender_psid, webhook_event.message); + // Handle Sticker + } else if (webhook_event.message.sticker_id) { + console.log("User send a sticker"); + // Handle Direct Message } else if (webhook_event.message) { receive.handleMessage(sender_psid, webhook_event.message); From 6eb1be69a648000d00da750d71a13d74b2d3fa7c Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Sun, 29 Apr 2018 18:07:57 -0400 Subject: [PATCH 2/6] Popular Team --- functions/handleCases.js | 60 ++++++++++++---------------------- functions/informationLookup.js | 44 +++++++++++++++++-------- functions/receive-message.js | 15 +++++++-- functions/updateDB.js | 20 +++++++++++- 4 files changed, 83 insertions(+), 56 deletions(-) diff --git a/functions/handleCases.js b/functions/handleCases.js index 9d3b46c..ac9768f 100644 --- a/functions/handleCases.js +++ b/functions/handleCases.js @@ -34,49 +34,33 @@ function teamOptions(sender_psid, teamName, imageURL) { sendResponse.teamOptionChoose(sender_psid, teamName, 'OPTION', choices, imageURL); } -// Provides 11 popular team options +// Provides 6 popular teams options function popularTeam(sender_psid) { - let key = ['Manchester United', 'Real Madrid', 'Barcelona', 'Chelsea', 'Manchester City', 'Paris Saint Germain', 'Arsenal', 'Liverpool', 'Germany', 'Brazil', 'Spain']; - let response = { - 'text': `Please type a team you want or choose from some quick options below!!!` - } - sendResponse.quickReply(sender_psid, response, 'POPULART', key); -} - - -function anotherTeam(sender_psid) { - const handleChoice = db.ref("HandleChoices/"); - - handleChoice.child(sender_psid).set({ - "choice": "TEAM" + db.ref("PopularTeams/").orderByChild("searchCount").limitToLast(6).once("value", (result) => { + let teams = [] + result.forEach((team) => { + teams.push(team.key); + }) + let response = { + 'text': `Please type a team you want or choose from some quick options below!!!` + } + sendResponse.quickReply(sender_psid, response, 'POPULART', teams); }) - - let response = { - 'text': 'Please give us the team name' - }; - sendResponse.directMessage(sender_psid, response); } -// Provides 11 popular players options +// Provides 6 popular players options function popularPlayer(sender_psid) { - let key = ['Ronaldo', 'Messi', 'Bale', 'Neymar', 'Hazard', 'Morata', 'Ozil', 'Kroos', 'Isco', 'Alexis', 'Salad']; - let response = { - 'text': `Please type a player you want or choose from some quick options below!!!` - } - sendResponse.quickReply(sender_psid, response, 'POPULARP', key); -} - -function anotherPlayer(sender_psid) { - const handleChoice = db.ref("HandleChoices/"); - - handleChoice.child(sender_psid).set({ - "choice": "PLAYER" + db.ref("PopularPlayers/").orderByChild("searchCount").limitToLast(6).once("value", (result) => { + let playerNames = [] + result.forEach((playerName) => { + playerNames.push(playerName.key); + }) + + let response = { + 'text': `Please type a player you want or choose from some quick options below!!!` + } + sendResponse.quickReply(sender_psid, response, 'POPULARP', playerNames); }) - - let response = { - 'text': 'Please give us the team name' - }; - sendResponse.directMessage(sender_psid, response); } // Repeats the main bot function. @@ -152,9 +136,7 @@ module.exports = { getStart, teamOptions, popularTeam, - anotherTeam, popularPlayer, - anotherPlayer, getContinue, askReminder, setReminder diff --git a/functions/informationLookup.js b/functions/informationLookup.js index a628c74..ac2969e 100644 --- a/functions/informationLookup.js +++ b/functions/informationLookup.js @@ -21,6 +21,9 @@ function teamNameLookup(sender_psid, key) { if (teamName.key.includes(key)) { flag = false; handleCases.teamOptions(sender_psid, teamName.val().name, teamName.val().imageURL); + + // Increase the number of search on the match. + updateDB.popularTeam(teamName.val().name); } }) }); @@ -55,6 +58,10 @@ function teamNameLookup(sender_psid, key) { 'name': teamName, 'imageURL': imageURL }); + + // Increase the number of search on the match. + updateDB.popularTeam(teamName); + handleCases.teamOptions(sender_psid, teamName, imageURL); } }) @@ -214,18 +221,29 @@ function playerLookup(sender_psid, key) { // Checks if the player is already in database db.ref('Players/').once("value", (allPlayers) => { - allPlayers.forEach((eachPlayer) => { - if (eachPlayer.key.includes(key)) { - flag = false; - // Increase the number of search for the player. - updateDB.popularPlayer(eachPlayer.key); - - sendResponse.playerReply(sender_psid, eachPlayer.val().playerTitle, - eachPlayer.val().playerSubtitle, eachPlayer.val().playerImageURL, - eachPlayer.val().playerURL); - } - }) + // Act as loop's break + try { + allPlayers.forEach((eachPlayer) => { + if (eachPlayer.key.includes(key)) { + flag = false; + + // Increase the number of search on the player. + var playerName = eachPlayer.val().playerTitle; + playerName = playerName.slice(0, playerName.indexOf("-") - 1); + + updateDB.popularPlayer(playerName); + + sendResponse.playerReply(sender_psid, eachPlayer.val().playerTitle, + eachPlayer.val().playerSubtitle, eachPlayer.val().playerImageURL, + eachPlayer.val().playerURL); + + throw BreakException; + } + }) + } + + catch (e) {} }) // If the player is not in the database, search for him @@ -277,8 +295,8 @@ function playerLookup(sender_psid, key) { 'playerImageURL': playerImageURL }); - // Increase the number of search for the player. - updateDB.popularPlayer(teamName.toUpperCase()); + // Increase the number of search on the player. + updateDB.popularPlayer(playerName); console.log("replied"); sendResponse.playerReply(sender_psid, playerTitle, playerSubtitle, playerImageURL, playerURL); diff --git a/functions/receive-message.js b/functions/receive-message.js index dd0a37c..5170496 100644 --- a/functions/receive-message.js +++ b/functions/receive-message.js @@ -154,7 +154,10 @@ function handleQuickReply(sender_psid, received_message) { // Choose another team else { - handleCases.anotherTeam(sender_psid); + handleChoice.child(sender_psid).set({ + "choice": "TEAM" + }) + handleCases.popularTeam(sender_psid); } } } @@ -166,12 +169,18 @@ function handlePostback(sender_psid, messagePostback) { // Search for different player name if (payload.includes('ANOTHERPLAYER')) { - handleCases.anotherPlayer(sender_psid); + handleChoice.child(sender_psid).set({ + "choice": "PLAYER" + }) + handleCases.popularPlayer(sender_psid); } // Search for different team name else if (payload.includes('Another Team')) { - handleCases.anotherTeam(sender_psid) + handleChoice.child(sender_psid).set({ + "choice": "TEAM" + }) + handleCases.popularTeam(sender_psid); } // Looking for match's information diff --git a/functions/updateDB.js b/functions/updateDB.js index ca5c2e2..07c7895 100644 --- a/functions/updateDB.js +++ b/functions/updateDB.js @@ -219,7 +219,7 @@ function setAllReminders() { }) } -// Save the number of searches have been performed for a player. +// Save the number of searches have been performed on a player. function popularPlayer(playerName) { db.ref("PopularPlayers").child(playerName).once("value", (result) => { if (!(result.exists())) { @@ -236,6 +236,23 @@ function popularPlayer(playerName) { }) } +// Save the number of searches have been performed on a team. +function popularTeam(teamName) { + db.ref("PopularTeams").child(teamName).once("value", (result) => { + if (!(result.exists())) { + db.ref("PopularTeams/").child(teamName).set({ + "searchCount": 1 + }) + } + + else { + db.ref("PopularTeams").child(teamName).set({ + "searchCount": result.val().searchCount + 1 + }) + } + }) +} + // Give back all the matches that are currently observed and updated function getOnGoing() { return onGoing; @@ -258,6 +275,7 @@ module.exports = { updateAllCurrentMatches, updateMatchesFromTeams, popularPlayer, + popularTeam, getOnGoing, setRunning, getRunning, From 5f61b1570ad8e86a1dfc8f2ae99ef654ad366cba Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Sun, 29 Apr 2018 19:41:17 -0400 Subject: [PATCH 3/6] Optimizes Quick options for Teams and Players --- functions/handleCases.js | 4 ++-- functions/updateDB.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/functions/handleCases.js b/functions/handleCases.js index ac9768f..439f7d3 100644 --- a/functions/handleCases.js +++ b/functions/handleCases.js @@ -34,7 +34,7 @@ function teamOptions(sender_psid, teamName, imageURL) { sendResponse.teamOptionChoose(sender_psid, teamName, 'OPTION', choices, imageURL); } -// Provides 6 popular teams options +// Provides 6 popular teams options - get team name from user. function popularTeam(sender_psid) { db.ref("PopularTeams/").orderByChild("searchCount").limitToLast(6).once("value", (result) => { let teams = [] @@ -48,7 +48,7 @@ function popularTeam(sender_psid) { }) } -// Provides 6 popular players options +// Provides 6 popular players options - get player name from user. function popularPlayer(sender_psid) { db.ref("PopularPlayers/").orderByChild("searchCount").limitToLast(6).once("value", (result) => { let playerNames = [] diff --git a/functions/updateDB.js b/functions/updateDB.js index 07c7895..44814e9 100644 --- a/functions/updateDB.js +++ b/functions/updateDB.js @@ -222,12 +222,15 @@ function setAllReminders() { // Save the number of searches have been performed on a player. function popularPlayer(playerName) { db.ref("PopularPlayers").child(playerName).once("value", (result) => { + + // Create new player in the Database if not exist. if (!(result.exists())) { db.ref("PopularPlayers/").child(playerName).set({ "searchCount": 1 }) } + // Increase the number of search on the existing player. else { db.ref("PopularPlayers").child(playerName).set({ "searchCount": result.val().searchCount + 1 @@ -239,12 +242,15 @@ function popularPlayer(playerName) { // Save the number of searches have been performed on a team. function popularTeam(teamName) { db.ref("PopularTeams").child(teamName).once("value", (result) => { + + // Create new team in the Database if not exist. if (!(result.exists())) { db.ref("PopularTeams/").child(teamName).set({ "searchCount": 1 }) } + // Increase the number of search on the existing team. else { db.ref("PopularTeams").child(teamName).set({ "searchCount": result.val().searchCount + 1 From 3add5df41487e5c3dc28f874b6536722f98598e0 Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Mon, 30 Apr 2018 10:10:23 -0400 Subject: [PATCH 4/6] Fix Reminder --- functions/handleCases.js | 2 +- functions/informationLookup.js | 6 ++++++ functions/receive-message.js | 14 +++++++------- scripts/get_next_game.py | 2 +- scripts/get_player_info.py | 2 +- scripts/get_team_name.py | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/functions/handleCases.js b/functions/handleCases.js index 439f7d3..3748c22 100644 --- a/functions/handleCases.js +++ b/functions/handleCases.js @@ -102,7 +102,7 @@ function setReminder(sender_psid, key) { setTimeout(() => { var response = { - 'text': `In ${new Date(timeDif).getMinutes()} minutes:\n${match.val().team1} vs ${match.val().team2}` + 'text': `In ${new Date(new Date(match.val().time) - new Date()).getMinutes()} minutes:\n${match.val().team1} vs ${match.val().team2}` }; sendResponse.directMessage(sender_psid, response); diff --git a/functions/informationLookup.js b/functions/informationLookup.js index ac2969e..659c503 100644 --- a/functions/informationLookup.js +++ b/functions/informationLookup.js @@ -42,6 +42,9 @@ function teamNameLookup(sender_psid, key) { "text" : `Cannot find the Team: ${key}` } sendResponse.directMessage(sender_psid, response); + setTimeout(() => { + handleCases.popularTeam(sender_psid); + }, 1000) } else { flag = false; @@ -264,6 +267,9 @@ function playerLookup(sender_psid, key) { 'text' : `Cannot find player: ${key}` }; sendResponse.directMessage(sender_psid, response); + setTimeout(() => { + handleCases.popularPlayer(sender_psid); + }, 1000) } else { flag = false; diff --git a/functions/receive-message.js b/functions/receive-message.js index 5170496..0146fbd 100644 --- a/functions/receive-message.js +++ b/functions/receive-message.js @@ -64,14 +64,14 @@ function handleMessage(sender_psid, received_message) { info.teamNameLookup(sender_psid, key); } } + } - // Instruction for user to use the Bot - else { - response = { - "text": `Please begin by typing in 'Start'`, - }; - sendResponse.directMessage(sender_psid, response); - } + // Instruction for user to use the Bot + else { + response = { + "text": `Please begin by typing in 'Start'`, + }; + sendResponse.directMessage(sender_psid, response); } }) } diff --git a/scripts/get_next_game.py b/scripts/get_next_game.py index c6d9a11..c906a63 100644 --- a/scripts/get_next_game.py +++ b/scripts/get_next_game.py @@ -12,7 +12,7 @@ def main(): window_size = "1200,800" - timeout = 20 + timeout = 300 team_name = sys.argv[1] chrome_options = Options() diff --git a/scripts/get_player_info.py b/scripts/get_player_info.py index 34bf948..c0bf0d4 100644 --- a/scripts/get_player_info.py +++ b/scripts/get_player_info.py @@ -13,7 +13,7 @@ def main(): window_size = "1200,800" - timeout = 20 + timeout = 300 player_name = sys.argv[1] chrome_options = Options() diff --git a/scripts/get_team_name.py b/scripts/get_team_name.py index 5524309..d5e9168 100644 --- a/scripts/get_team_name.py +++ b/scripts/get_team_name.py @@ -12,7 +12,7 @@ def main(): window_size = "1200,800" - timeout = 20 + timeout = 300 team_name = sys.argv[1] chrome_options = Options() From 842d32e9045504fe7f1fe6699c5fd9daa3a788d1 Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Sun, 6 May 2018 15:25:03 -0400 Subject: [PATCH 5/6] Cleaner log --- functions/informationLookup.js | 6 ++++++ functions/receive-message.js | 13 ++++--------- functions/updateDB.js | 10 ++++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/functions/informationLookup.js b/functions/informationLookup.js index 659c503..e6bbeb9 100644 --- a/functions/informationLookup.js +++ b/functions/informationLookup.js @@ -45,6 +45,9 @@ function teamNameLookup(sender_psid, key) { setTimeout(() => { handleCases.popularTeam(sender_psid); }, 1000) + console.log("Error occured on Server for TEAM: " + key); + console.error(err); + console.error("\n\n") } else { flag = false; @@ -151,6 +154,9 @@ function matchLookup(sender_psid, key, status) { "text" : `Cannot find the Match for: ${key}` } sendResponse.directMessage(sender_psid, response); + console.log("Error occured on Server for MATCH: " + key); + console.error(err); + console.error("\n\n") } else { request({ diff --git a/functions/receive-message.js b/functions/receive-message.js index 0146fbd..8090640 100644 --- a/functions/receive-message.js +++ b/functions/receive-message.js @@ -18,18 +18,13 @@ function handleMessage(sender_psid, received_message) { let key = received_message.text; // Update all the matches of the teams in Database/Teams - if (key.toUpperCase() == "UPDATEMATCHESFROMTEAMS") { + if (key == process.env.MATCHES_FORM_TEAMS) { updateDB.updateMatchesFromTeams(); } - // Log out all the onGoing matches (currently observed) - if (key.toUpperCase() == "ONGOING") { - info.displayOnGoing(); - } - - // Log out status for running thread - if (key.toUpperCase() == "RUNNING") { - updateDB.getRunning(); + // Clearr outdated matches in the database + if (key == process.env.CLEAR_MATCHES) { + updateDB.clearOldMatches(); } // Users begin the search diff --git a/functions/updateDB.js b/functions/updateDB.js index 44814e9..f9a19ef 100644 --- a/functions/updateDB.js +++ b/functions/updateDB.js @@ -24,7 +24,7 @@ function dbTeamName(key) { // If the team name is not in the database if (!snapshot.exists()) { running = true - console.log('Looking for: ' + key.toUpperCase()); + console.log('Looking for Team: ' + key.toUpperCase()); // Async function helps searching for Team Name data.get_team_name(key, (err, reply) => { @@ -52,6 +52,8 @@ function dbTeamName(key) { } else { console.log("Error occured on Server for TEAM: " + key); + console.error(err); + console.error("\n\n") } // Finish Geting Team Name @@ -99,16 +101,15 @@ function dbNextGame(key, iniTime) { // Get new data and save it to database by crawling ESPN else { - console.log(key); + console.log("Looking for Match: " + key); // Get rid of all special characters key = dataFormat.cleanKeyDB(key); data.get_next_game(key, (err, reply) => { if (err) { console.log("Error occured on Server for MATCH: " + key); - console.error("################################"); console.error(err); - console.error("################################"); + console.error("\n\n") running = false; } else { @@ -179,6 +180,7 @@ function updateAllCurrentMatches() { // Search for all the matches that are currently in Database/Teams function updateMatchesFromTeams() { + console.log("Currently Updating Mathces from Teams"); db.ref('Teams/').once('value', (allTeams) => { allTeams.forEach((eachTeam) => { db.ref('Matches/').child(dataFormat.cleanKeyDB(eachTeam.key).toUpperCase()).once('value', function(snapshot) { From 2f460012f4f92b2802e964b1102547775ddecdb3 Mon Sep 17 00:00:00 2001 From: tuannguyen1101 Date: Tue, 8 May 2018 15:32:13 -0400 Subject: [PATCH 6/6] Bring updateDB out of listen --- app.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index f56b3bb..525d7f2 100644 --- a/app.js +++ b/app.js @@ -17,19 +17,19 @@ app.listen(process.env.PORT || 1337, () => { console.log('webhook is listening on port ', process.env.PORT || 1337); - // Clearr outdated matches in the database - updateDB.clearOldMatches(); - - // Update all the matches that are stored in the database. - updateDB.updateAllCurrentMatches(); - - // Set up all previous Reminders - updateDB.setAllReminders(); - }); app.use(bodyParser.json()); app.use('/webhook', webhooks); +// Clearr outdated matches in the database +updateDB.clearOldMatches(); + +// Update all the matches that are stored in the database. +updateDB.updateAllCurrentMatches(); + +// Set up all previous Reminders +updateDB.setAllReminders(); + module.exports = app;