From 5a32c6d909b5ccef0a420dc8e7e613eca5866642 Mon Sep 17 00:00:00 2001 From: Leonardo Cabeza Date: Tue, 9 May 2017 22:12:59 -0500 Subject: [PATCH] (Agrega): Saluda si un miembro se va Cuando un miembro del grupo se va, o es expulsado, @WengyBot lo hace saber en el grupo. Relacionado a #51 --- config/messages.js | 1 + src/index.js | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/config/messages.js b/config/messages.js index 591eca8..1e95fea 100644 --- a/config/messages.js +++ b/config/messages.js @@ -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}', diff --git a/src/index.js b/src/index.js index 8d5365f..84e6770 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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; @@ -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) => {