Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +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();

});

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;
48 changes: 33 additions & 15 deletions functions/handleCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +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 - get team name from user.
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);
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);
})
}

// Provides 11 popular players options
// Provides 6 popular players options - get player name from user.
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);
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);
})
}

// Repeats the main bot function.
Expand All @@ -63,7 +74,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?'
}
Expand All @@ -86,25 +96,33 @@ 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(new Date(match.val().time) - new Date()).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.'
};
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`
Expand Down
57 changes: 46 additions & 11 deletions functions/informationLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})
});
Expand All @@ -39,6 +42,12 @@ function teamNameLookup(sender_psid, key) {
"text" : `Cannot find the Team: ${key}`
}
sendResponse.directMessage(sender_psid, response);
setTimeout(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mấy cái function này set timeout chi

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cho nó delay 1 tí thay vì send 2 tin liên tục, dễ bị tin này đè tin kia

handleCases.popularTeam(sender_psid);
}, 1000)
console.log("Error occured on Server for TEAM: " + key);
console.error(err);
console.error("\n\n")
}
else {
flag = false;
Expand All @@ -55,6 +64,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);
}
})
Expand Down Expand Up @@ -141,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({
Expand Down Expand Up @@ -214,14 +230,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;
sendResponse.playerReply(sender_psid, eachPlayer.val().playerTitle,
eachPlayer.val().playerSubtitle, eachPlayer.val().playerImageURL,
eachPlayer.val().playerURL);
}
})

// Act as loop's break

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sao ko xài loop break bthg

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xài forEach không hỗ trợ 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
Expand All @@ -242,12 +273,15 @@ function playerLookup(sender_psid, key) {
'text' : `Cannot find player: ${key}`
};
sendResponse.directMessage(sender_psid, response);
setTimeout(() => {
handleCases.popularPlayer(sender_psid);
}, 1000)
}
else {
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++) {
Expand All @@ -266,14 +300,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 on the player.
updateDB.popularPlayer(playerName);

console.log("replied");
sendResponse.playerReply(sender_psid, playerTitle, playerSubtitle, playerImageURL, playerURL);
Expand Down
63 changes: 31 additions & 32 deletions functions/receive-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,14 +59,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);
}
})
}
Expand Down Expand Up @@ -139,14 +134,26 @@ 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 {
handleChoice.child(sender_psid).set({
"choice": "TEAM"
})
handleCases.popularTeam(sender_psid);
}
setTimeout(() => {
handleCases.getContinue(sender_psid);
}, 1000)
}
}

Expand All @@ -160,23 +167,15 @@ function handlePostback(sender_psid, messagePostback) {
handleChoice.child(sender_psid).set({
"choice": "PLAYER"
})

response = {
'text': 'Please give us the player name'
};
sendResponse.directMessage(sender_psid, response);
handleCases.popularPlayer(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.popularTeam(sender_psid);
}

// Looking for match's information
Expand Down
5 changes: 5 additions & 0 deletions functions/sendResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
}
Expand Down
Loading