Skip to content

Commit

Permalink
/track_delete and /help commands implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
WBerredo committed Mar 30, 2017
1 parent 523b63a commit 8791391
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 19 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ to verify the availability of one website
```
/track https://telegram.org
```
* To delete an URL from the track list you have to type:
```
/track_delete
```
and then click at the URL you want to delete
* To get a list of URLs you are tracking:
```
/track_list
```
Every 5 minutes the bot you run a check all tracked URLs, if the status of an URL get changed(up->down, down->up), the user will be get notified.
Every 5 minutes the bot will run a check at track list, if the status of an URL get changed(up->down, down->up), the user will be notified.
### Group
* [Invite](http://stackoverflow.com/a/40175742) the bot to a group
Expand Down
52 changes: 47 additions & 5 deletions feature/Track.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class Track {
}

/**
* Set an url item
*/
* Set an url item
*/
setUrl(url, msgId, currentStatus, itemId) {
let ref = this._firebase.database().ref(`track/${msgId}/${itemId}`);
let urlTrack = new this._urlTrack(url, currentStatus);
Expand All @@ -88,13 +88,19 @@ class Track {


/**
* Get all tracked urls
*/
getAllFromUser(msgId, callback) {
* Get all tracked urls
*/
getAllFromUser(msgId, callback, justUrls = true) {
let ref = this._firebase.database().ref(`track/${msgId}`);
ref.once("value", snapshot => {
let urlList = [];
let urls = snapshot.val();

if (!justUrls) {
callback(msgId, urls);
return;
}

// Iterate each url from user
for (let itemKey in urls) {
let urlItem = urls[itemKey];
Expand All @@ -104,6 +110,42 @@ class Track {
callback(msgId, urlList);
});
}

/**
* Generate a keyboard with all tracked urls
*/
getAllUrlsKeyBoard(msgId, callback) {
this.getAllFromUser(msgId, (msgId, urls) => {
const urlOpts = [];
for (let itemKey in urls) {
let urlItem = urls[itemKey];
urlOpts.push([{
text: urlItem.url,
callback_data: itemKey
}]);
}

if (urlOpts.length == 0) {
callback(null);
return;
}

const keyboard = {
reply_markup: {
inline_keyboard: urlOpts
}
};

callback(keyboard)
}, false);
}

// delete an url
deleteUrl(msgId, itemId, callback) {
this._firebase.database().ref(`track/${msgId}/${itemId}`).remove(error => {
callback(!error, msgId);
});
}
}

module.exports = Track;
33 changes: 30 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let Message = require('./util/Message.js');
let Verifier = require('./feature/Verifier.js');

// first message
telegram.onText(Regex.startRegex, (msg, match) => {
telegram.onText(Regex.startOrHelpRegex, (msg, match) => {
msgId = msg.chat.id;

telegram.sendMessage(msgId, Message.welcomeFirstStep(msg.from.first_name));
Expand Down Expand Up @@ -55,10 +55,14 @@ function verifyCallback(msg, url, success, statusCode) {
}
}

// just verify
// wrong usage
telegram.onText(Regex.justVerifyRegex, (msg) => {
telegram.sendMessage(msg.chat.id, Message.verifyHowToUse);
})
});

telegram.onText(Regex.justTrackRegex, (msg) => {
telegram.sendMessage(msg.chat.id, Message.trackHowToUse);
});

// not found
telegram.on('message', msg => {
Expand Down Expand Up @@ -108,6 +112,29 @@ if (trackFeature) {
});
});

// delete an url
telegram.onText(Regex.deleteTrackRegex, function(msg, match) {
track.getAllUrlsKeyBoard(msg.chat.id, keyboard => {
if (keyboard != null) {
telegram.sendMessage(msg.from.id, 'Choose an url to delete', keyboard);
} else {
telegram.sendMessage(msg.from.id, Message.urlNotFound);
}
})
});

// callback from custom keyboard(just when it is a delete action)
telegram.on("callback_query", function(callbackQuery) {
track.deleteUrl(callbackQuery.from.id, callbackQuery.data,
(success, msgId) => {
if (success) {
telegram.sendMessage(msgId, Message.deleteSuccess);
} else {
telegram.sendMessage(msdId, Message.deleteError);
}
});
});

// setup verification
track.scheduleVerification((url, msgId, status) => {
if (status) {
Expand Down
35 changes: 29 additions & 6 deletions util/Message.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Message {
static get didntUnderstand() {
return "Sorry, I didn't understand what you said! Try to pass an URL :D";
return `Sorry, I didn't understand what you said! Try to pass an URL ` +
`or use \/help command`;
}

static get verifyHowToUse() {
return "To use /verify command you have to pass an URL. " +
"Ex: /verify telegram.org";
return `To use /verify command you have to pass an URL. ` +
`Ex: /verify telegram.org`;
}

static get trackHowToUse() {
return `To use /track command you have to pass an URL to track. ` +
`Ex: /track telegram.org`;
}

static welcomeFirstStep(username) {
Expand All @@ -14,8 +20,12 @@ class Message {
}

static get welcomeSecondStep() {
return `You can use the command /verify telegram.org or just type` +
` your url to verify the current status.`;
return `We can chat by these commands:\n` +
`\/verify yourdomain.com - Verify if an URL is up or down \n` +
`\/track yourdomain.com - Track an URL and be notified when the status get changed\n` +
`\/track_list - Get a list of all tracked URLs\n` +
`\/track_delete - Delete an URL from the track list\n` +
`You also can just type an URL to know if it's up or down.`;
}

static successStatus(url) {
Expand Down Expand Up @@ -48,10 +58,23 @@ class Message {
return ` Checked at ${currentTime} ${timezone}. `;
}

static get urlNotFound() {
return `You don't have any tracked urls, to track an url use` +
` track command. Ex: /track domain.com`;
}

static get deleteSuccess() {
return `This URL was removed. ${Message.trackListHowToUse}`;
}

static get deleteError() {
return `Sorry, I can not remove this url. ${Message.trackListHowToUse}`;
}

static getListMessage(urls) {
switch (urls.length) {
case 0:
return `You don't have any tracked urls.`
return Message.urlNotFound;

case 1:
return `Your tracked url is: ${urls[0]}`;
Expand Down
18 changes: 14 additions & 4 deletions util/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Regex {
return "\/track";
}

static get startRegex() {
return /\/start/;
static get startOrHelpRegex() {
return /^\/start|\/help/;
}

static get urlRegex() {
Expand All @@ -42,6 +42,10 @@ class Regex {
return new RegExp(Regex._trackCommandString + "\\s" + Regex._urlRegexString);
}

static get justTrackRegex() {
return new RegExp(Regex._trackCommandString + "$");
}

static get trackListRegex() {
return /^\/track_list$/;
}
Expand All @@ -54,14 +58,20 @@ class Regex {
return new RegExp(Regex._botUsernameRegexString + Regex._urlRegexString);
}

static get deleteTrackRegex() {
return /\/track_delete/;
}

static isRegexMatch(textMessage) {
return textMessage != undefined &&
(textMessage.match(Regex.urlRegex) ||
textMessage.match(Regex.verifyUrlRegex) ||
textMessage.match(Regex.startRegex) ||
textMessage.match(Regex.startOrHelpRegex) ||
textMessage.match(Regex.justVerifyRegex) ||
textMessage.match(Regex.trackUrlRegex) ||
textMessage.match(Regex.trackListRegex)
textMessage.match(Regex.justTrackRegex) ||
textMessage.match(Regex.trackListRegex) ||
textMessage.match(Regex.deleteTrackRegex)
);
}
}
Expand Down

0 comments on commit 8791391

Please sign in to comment.