Skip to content

Commit

Permalink
Merge pull request #61 from ngVenezuela/develop
Browse files Browse the repository at this point in the history
Se agrega funcionalidad discutida en #51
  • Loading branch information
leocabeza authored May 10, 2017
2 parents 3389e70 + 2f9906e commit b6aba4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const messages = {
'seguirnos en twitter como [@ngVenezuela](https://twitter.com/ngVenezuela) ' +
'y a mirar nuestra comunidad en Github: https://github.com/ngvenezuela.\n\n'+
'Además nos gustaría que respondieras esta pequeña encuesta: http://bit.ly/ngve-encuesta',
goodbyeMsg: '¡Nos vemos pronto #{name}! Esperamos NO verte por el grupo de React Venezuela',
goodMornings: {
mondays: [
'Buenos días comunidad, ¡Que tengan un excelente inicio de semana! \u{1F60E}',
Expand Down
30 changes: 16 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ let goodMorningGivenToday = false;
let minuteToCheck = generateRandom(0, 59);

bot
.on('new_chat_participant', newChatParticipant)
.on('new_chat_participant', sayHello)
.on('left_chat_participant', sayGoodbye)
.on('text', newText);

morningEvent
Expand Down Expand Up @@ -69,6 +70,11 @@ blogEvent
});
});

function formatName(msgContext) {
return msgContext.username ?
'@' + msgContext.username : msgContext.first_name;
}

function newText(msg) {
if (!goodMorningGivenToday && isGoodMorningGiven(msg.text)) {
goodMorningGivenToday = true;
Expand All @@ -79,24 +85,20 @@ function newText(msg) {
}
}

function newChatParticipant(msg) {
function sayHello(msg) {
bot.sendMessage(
msg.chat.id,
getFullWelcomeMsg(msg),
messages.welcomeMsg.replace('#{name}', formatName(msg.new_chat_member)),
{reply_to_message_id: msg.message_id, parse_mode: 'Markdown'}
);
}

function getFullWelcomeMsg(msg) {
let nameToBeShown = msg.new_chat_member.first_name;

if (msg.new_chat_member.username) {
nameToBeShown = '@' + msg.new_chat_member.username;
} else if (msg.new_chat_member.hasOwnProperty('last_name')) {
nameToBeShown = nameToBeShown + ' ' + msg.new_chat_member.last_name;
}

return messages.welcomeMsg.replace('#{name}', nameToBeShown);
}
function sayGoodbye(msg) {
bot.sendMessage(
msg.chat.id,
messages.goodbyeMsg.replace('#{name}', formatName(msg.left_chat_member)),
{reply_to_message_id: msg.message_id, parse_mode: 'Markdown'}
);
}

bot.on('text', (msg) => {
Expand Down

0 comments on commit b6aba4f

Please sign in to comment.