Skip to content

Commit

Permalink
Merge pull request #190 from leocabeza/develop
Browse files Browse the repository at this point in the history
(Arreglado) Mensaje de despedida y reenvío enlaces
  • Loading branch information
leocabeza authored Jun 13, 2019
2 parents ea56915 + d52a721 commit b9bdbe7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## [Unreleased]

## 4.2.0 (2019-06-12)

### Arreglado

- Mensaje de despedida a usuario cuando deja el grupo solo funcionaba en grupo de admins
- Reenvío de enlaces al grupo de admins que eran compartidos en el mismo grupo no debían enviarse

## 4.1.1 (2019-06-11)

### Actualizado
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"node-telegram-bot-api": "^0.30.0"
},
"devDependencies": {
"husky": "^2.4.0",
"husky": "^2.4.1",
"lint-staged": "^8.2.0",
"prettier": "^1.18.2"
},
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ process.on('SIGUSR2', () => {
});

process.on('uncaughtException', error => {
if (NODE_ENV === 'development') {
console.log('error: ', error);
}
Sentry.captureException(error);
});
31 changes: 23 additions & 8 deletions src/listen-commands/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
const { whiteListedDomains, adminGroupId } = require('config/telegram');
const {
whiteListedDomains,
mainGroupId,
adminGroupId,
} = require('config/telegram');
const { forwardMessage } = require('bot-api-overrides');

const verifyUrls = async (bot, msg) => {
const chatId = msg.chat.id;
const chatInfo = await bot.getChat(chatId);
const chatType = async (bot, msg) => {
const {
type,
all_members_are_administrators: allMembersAreAdministrators,
} = chatInfo;
chat: { id },
} = msg;
const { id: chatId } = await bot.getChat(id);

if (chatId.toString() === mainGroupId) {
return 'main';
} else if (chatId.toString() === adminGroupId) {
return 'admin';
}

return 'private';
};

const verifyUrls = async (bot, msg) => {
const type = await chatType(bot, msg);

if (['group', 'supergroup'].includes(type) && !allMembersAreAdministrators) {
if (type === 'main') {
const urlEntities = msg.entities
? msg.entities.filter(entity => entity.type === 'url')
: [];
Expand All @@ -34,5 +48,6 @@ const verifyUrls = async (bot, msg) => {
};

module.exports = {
chatType,
verifyUrls,
};
8 changes: 5 additions & 3 deletions src/listen-commands/development-tools/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* Send group or supergroup's Id.
* Send main or admin's group id.
* This will be used in config/telegram.js
* @param {object} bot
* @param {object} msg
*/
const sendGroupId = async (bot, msg) => {
const chatId = msg.chat.id;
const {
chat: { id: chatId },
} = msg;
const chatInfo = await bot.getChat(chatId);

if (chatInfo.type === 'private') {
if (['group', 'supergroup'].includes(chatInfo.type)) {
bot.sendMessage(
chatId,
`Tú variable \`mainGroupId/adminGroupId\` es: ${chatInfo.id}`,
Expand Down
6 changes: 3 additions & 3 deletions src/listen-commands/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Sentry = require('@sentry/node');

const { gistCreated, gistRecommendation } = require('config/messages');
const { sendMessage } = require('bot-api-overrides');
const { chatType } = require('listen-commands/admin');

const { GITHUB_ACCESS_TOKEN } = process.env;
const MAX_LENGTH_GIST_TEXT = 200;
Expand Down Expand Up @@ -70,10 +71,9 @@ const sendGist = (bot, msg, text = '') => {
* @param {object} msg
*/
const verifyCode = async (bot, msg) => {
const chatId = msg.chat.id;
const chatInfo = await bot.getChat(chatId);
const type = await chatType(bot, msg);

if (['supergroup', 'group'].includes(chatInfo.type)) {
if (['main', 'admin'].includes(type)) {
if (!Object.prototype.hasOwnProperty.call(msg, 'entities')) {
return;
}
Expand Down
29 changes: 9 additions & 20 deletions src/listen-commands/greetings/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { welcome, goodBye } = require('config/messages');
const { sendMessage } = require('bot-api-overrides');

const { chatType } = require('listen-commands/admin');
/**
* Returns username or first name of the user
* @param {string} firstName
Expand All @@ -16,13 +16,8 @@ const formatName = (firstName, userName) =>
* @param {object} msg
*/
const sayHello = async (bot, msg) => {
const chatId = msg.chat.id;
const chatInfo = await bot.getChat(chatId);
const {
type,
all_members_are_administrators: allMembersAreAdministrators,
} = chatInfo;
if (['supergroup', 'group'].includes(type) && !allMembersAreAdministrators) {
const type = await chatType(bot, msg);
if (type === 'main') {
msg.new_chat_members.forEach(({ first_name, username }) => {
const message = welcome.replace(
'#{name}',
Expand All @@ -42,19 +37,13 @@ const sayHello = async (bot, msg) => {
* @param {object} msg
*/
const sayGoodbye = async (bot, msg) => {
const chatId = msg.chat.id;
const chatInfo = await bot.getChat(chatId);
const { first_name: firstName, username } = msg.left_chat_member;
const {
type,
all_members_are_administrators: allMembersAreAdministrators,
} = chatInfo;
if (['supergroup', 'group'].includes(type) && !allMembersAreAdministrators) {
sendMessage(
bot,
msg.chat.id,
goodBye.replace('#{name}', formatName(firstName, username))
);
left_chat_member: { first_name: firstName },
chat: { id: chatId },
} = msg;
const type = await chatType(bot, msg);
if (type === 'main') {
sendMessage(bot, chatId, goodBye.replace('#{name}', firstName));
}
};

Expand Down

0 comments on commit b9bdbe7

Please sign in to comment.