Skip to content

Commit

Permalink
(Agrega) JSDoc (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocabeza authored Aug 21, 2017
1 parent d9b8fac commit 8546ceb
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 66 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## 3.2.2 (2017-08-20)

### Agregado

- Se agregó JSDoc a todos los métodos, funciones y clases.

## 3.2.1 (2017-08-20)

### Corregido
Expand Down
32 changes: 23 additions & 9 deletions src/bot/telegram-bot.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
const NodeTelegramBotApi = require('node-telegram-bot-api');

/**
* Class representing a TelegramBot.
* @extends NodeTelegramBotApi
*/
class TelegramBot extends NodeTelegramBotApi {

/**
* Creates an instance of NodeTelegramBotApi
* @param {string} token - Token given by @BotFather
* @param {object} options - Options to initialize NodeTelegramBotApi
* @see https://github.com/yagop/node-telegram-bot-api/blob/release/doc/api.md#new-telegrambottoken-options
*/
constructor(token, options = {}) {
super(token, options);
}

/**
* Check for a valid telegram message
* @param {object} msg - Message to check
* @return {boolean}
* @see https://core.telegram.org/bots/api#update
*/
checkMessage(msg) {
return msg.message
|| msg.edited_message
|| msg.channel_post
|| msg.edited_channel_post
|| msg.inline_query
|| msg.chosen_inline_result
|| msg.callback_query;
return msg.message ||
msg.edited_message ||
msg.channel_post ||
msg.edited_channel_post ||
msg.inline_query ||
msg.chosen_inline_result ||
msg.callback_query;
}

/**
* Give message to processUpdate parent method
* @param {object} msg - Message to process
*/
proccessMessage(msg) {
this.processUpdate(msg);
}

}

module.exports = TelegramBot;
55 changes: 33 additions & 22 deletions src/events/morning.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
const events = require('events');
const EventEmitter = require('events').EventEmitter;
const timeUtility = require('./../utils/time');

const eventEmitter = new events.EventEmitter();

/**
* this function emits an event
* with the current hour/minute &
* it also emits an event when it's
* a new day
* Class representing an EventEmitter
* @extends EventEmitter
*/
const emitMinuteMark = () => {
const vzlanHour = timeUtility.vzlanHour();
const vzlanMinute = timeUtility.vzlanMinute();
class MorningEvent extends EventEmitter {
/**
* Creates an instance of EventEmitter
*/
constructor() {
super();

eventEmitter.emit(
'minuteMark',
vzlanHour,
vzlanMinute,
timeUtility.vzlanWeekday()
);
if (vzlanHour === 0 && vzlanMinute === 0) {
eventEmitter.emit('newDay');
}
};
/**
* This function emits an event
* with the current hour/minute &
* it also emits an event when it's
* a new day
*/
const emitMinuteMark = () => {
const vzlanHour = timeUtility.vzlanHour();
const vzlanMinute = timeUtility.vzlanMinute();

setInterval(emitMinuteMark, 60 * 1000); // 60 seconds
this.emit(
'minuteMark',
vzlanHour,
vzlanMinute,
timeUtility.vzlanWeekday()
);
if (vzlanHour === 0 && vzlanMinute === 0) {
this.emit('newDay');
}
};

setInterval(emitMinuteMark, 60 * 1000); // 60 seconds
}
}

module.exports = eventEmitter;
module.exports = new MorningEvent();
18 changes: 17 additions & 1 deletion src/events/superfeedr.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
const EventEmitter = require('events').EventEmitter;

/**
* Class representing an EventEmitter
* @extends EventEmitter
*/
class Superfeedr extends EventEmitter {

/**
* Check for a valid superfeedr object
* @param {object} msg
* @see https://documentation.superfeedr.com/schema.html
*/
checkMessage(msg) {
return msg.status && msg.status.code && msg.status.http && msg.status.feed;
return msg.status &&
msg.status.code &&
msg.status.http &&
msg.status.feed;
}

/**
* Emit message to subscribers
* @param {object} msg - Message to emit
*/
proccessMessage(msg) {
this.emit('newFeed', msg);
}
Expand Down
30 changes: 22 additions & 8 deletions src/events/tweets.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const EventEmitter = require('events').EventEmitter;
const Twitter = require('twitter');
const twitterConfig = require('./../../config/config').integrations.twitter;
const debugMode = require('./../../config/config').debugMode;

/**
* Class representing an EventEmitter
* @extends NodeTelegramBotApi
*/
class TweetEvent extends EventEmitter {
/**
* Creates an instance of EventEmitter
*/
constructor() {
super();

Expand All @@ -14,25 +20,33 @@ class TweetEvent extends EventEmitter {
access_token_secret: twitterConfig.auth.accessTokenSecret
});

/**
* Validating that tweet is not a reply and neither a RT
* @param {object} tweet
*/
const tweetIsNotAReply = tweet => tweet.in_reply_to_status_id === null;

/**
* Validating that tweet is a RT
* @param {object} tweet
*/
const tweetIsRt = tweet => Object.hasOwnProperty.call(tweet, 'retweeted_status');

/**
* Validating that RT is to other accounts,
* except the one configured in config
* @param {object} tweet
*/
const rtToOtherAccountsExceptConfigured = tweet =>
tweetIsRt(tweet) &&
tweet.retweeted_status.user.id_str !== twitterConfig.id;

client
.stream('statuses/filter', { follow: twitterConfig.id }, (stream) => {
stream.on('data', (tweet) => {
if (debugMode) {
console.log('new tweet event, not validaded yet');
}
// validating that tweet is not a reply and neither a RT
if (tweetIsNotAReply(tweet) && !tweetIsRt(tweet)) {
console.log('new tweet event is not a reply or a RT, emitting new tweet event');
this.emit('newTweet', tweet);
// validating that tweet is a RT, but not to the configured account
} else if (rtToOtherAccountsExceptConfigured(tweet)) {
console.log('new tweet a RT to a non configured account');
this.emit('newTweet', tweet);
}
});
Expand Down
45 changes: 43 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ let minuteToCheck = generateRandom(0, 59);

redisClient
.on('ready', () => {
/**
* Check for /groupId text command
*/
bot
.onText(/^\/groupId/, (msg, match) =>
adminUtility.verifyGroup(
Expand All @@ -50,6 +53,9 @@ redisClient
true
)
);
/**
* Check for /comunidades text command
*/
bot
.onText(/^\/comunidades/, (msg, match) =>
adminUtility.verifyGroup(
Expand All @@ -60,6 +66,9 @@ redisClient
true
)
);
/**
* Check for /github text command
*/
bot
.onText(/^\/github/, (msg, match) =>
adminUtility.verifyGroup(
Expand All @@ -70,27 +79,41 @@ redisClient
true
)
);

/**
* Check for /gist with paramteres command
*/
bot
// eslint-disable-next-line no-useless-escape
.onText(/^\/gist ([\s\S\.]+)/, (msg, match) =>
adminUtility.verifyGroup(
msg,
() => githubUtility.createGist(bot, msg, redisClient, match[1], false),
() => githubUtility.checkGist(bot, msg, redisClient, match[1], false),
true,
false,
true
)
);

/**
* Triggered when new member(s) join
*/
bot
.on('new_chat_members', msg =>
adminUtility.verifyGroup(msg, () => chatUtility.sayHello(bot, msg))
);
/**
* Triggered when a member leaves
*/
bot
.on('left_chat_member', msg =>
adminUtility.verifyGroup(msg, () => chatUtility.sayGoodbye(bot, msg))
);
/**
* On any message:
* check if good morning was given
* check if it's a url
* check if it's code
*/
bot
.on('message', msg =>
adminUtility.verifyGroup(
Expand All @@ -104,6 +127,9 @@ redisClient
}
)
);
/**
* On any message check if api.ai has a respond to the message
*/
bot
.on('message', msg =>
adminUtility.verifyGroup(
Expand All @@ -120,6 +146,9 @@ redisClient
});

morningEvent
/**
* Every minute it checks if good morning was given
*/
.on('minuteMark', (vzlanHour, vzlanMinute, weekday) => {
const executeGoodMorningCheck =
morningUtility.canBotGiveGoodMorning(
Expand All @@ -131,13 +160,25 @@ morningEvent
minuteToCheck = executeGoodMorningCheck.minuteToCheck;
}
})
/**
* If it's a new day, reset goodMorningToday variable
*/
.on('newDay', () => {
goodMorningGivenToday = false;
});

newTweet
/**
* It triggers when there's a new tweet
*/
.on('newTweet', tweet => twitterUtility.sendNewTweet(bot, tweet));

superfeedr
/**
* It triggers when there is a new github release
*/
.on('newFeed', feed => githubUtility.checkAndSendRelease(bot, feed))
/**
* It triggers when there is a new entry in the blog
*/
.on('newFeed', feed => blogUtility.checkAndSendBlogEntry(bot, feed));
Loading

0 comments on commit 8546ceb

Please sign in to comment.