|
@@ -1,12 +1,6 @@
|
|
|
-const {
|
|
|
- start_bot, start_service,
|
|
|
- home, cmd_error, not_member_admin,
|
|
|
- view_chat, add_feed, remove_feed
|
|
|
-} = require('../messages/commands');
|
|
|
-const { go_back_btn } = require('../messages/inline_keyboard');
|
|
|
-
|
|
|
+const { start_service, home, cmd_error, not_member_admin, view_chat, add_feed, remove_feed, active_feed } = require('../messages/commands');
|
|
|
const { homeMarkup, timezonesMarkup, isNotMemberOrAdminMarkup, goBackManagerFeedsMarkup } = require('../markups');
|
|
|
-const { getChatId, isBotAdmin, isAdmin, isStillMemberAndAdmin } = require('../helpers/bot_helpers');
|
|
|
+const { getChatId, isBotAdmin, isAdmin, isStillMemberAndAdmin, removeCommand } = require('../helpers/bot_helpers');
|
|
|
const { asyncFilter, isHashtagsValid, removeSpacesInArray, removeNotHashtagsInArray, listFeeds } = require('../helpers/features_helpers');
|
|
|
|
|
|
const User = require('../domain/User');
|
|
@@ -30,32 +24,16 @@ module.exports = bot => {
|
|
|
|
|
|
bot.start(async ctx => {
|
|
|
const { type } = await ctx.getChat();
|
|
|
- const userID = String(ctx.from.id);
|
|
|
+ const userID = ctx.from.id;
|
|
|
+ const username = ctx.from.username;
|
|
|
|
|
|
if (type === 'private') {
|
|
|
- !(await userRepository.existsUser(userID))
|
|
|
- ? ctx.telegram.sendMessage(getChatId(ctx), start_bot.text, timezonesMarkup)
|
|
|
- : ctx.telegram.sendMessage(getChatId(ctx), home.text, homeMarkup);
|
|
|
+ await userRepository.add({ user_id: userID, username });
|
|
|
+
|
|
|
+ ctx.telegram.sendMessage(getChatId(ctx), home.text, homeMarkup);
|
|
|
} else {
|
|
|
ctx.telegram.sendMessage(getChatId(ctx), 'Olá!');
|
|
|
}
|
|
|
-
|
|
|
- return;
|
|
|
- })
|
|
|
-
|
|
|
- bot.command('register', async ctx => {
|
|
|
- const [, timezone] = ctx.message.text.split(' ');
|
|
|
-
|
|
|
- if (!timezone) {
|
|
|
- ctx.reply(cmd_error.text);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const user = new User(ctx.from.id, ctx.from.username, timezone);
|
|
|
- await userRepository.add(user.getValue());
|
|
|
-
|
|
|
- ctx.telegram.sendMessage(getChatId(ctx), home.text, homeMarkup);
|
|
|
-
|
|
|
return;
|
|
|
})
|
|
|
|
|
@@ -137,7 +115,7 @@ module.exports = bot => {
|
|
|
}
|
|
|
|
|
|
const userID = ctx.message.from.id;
|
|
|
- const { id: chatID, title } = await chatRepository.getOneChatByTitle(String(userID), chatTitle);
|
|
|
+ const { id: chatID } = await chatRepository.getOneChatByTitle(String(userID), chatTitle);
|
|
|
|
|
|
const getMessage = async () => {
|
|
|
const feedsList = await listFeeds(feedRepository, chatID);
|
|
@@ -156,6 +134,7 @@ module.exports = bot => {
|
|
|
|
|
|
bot.command('add', async ctx => await addFeedCommand(ctx, chatID, getMessage));
|
|
|
bot.command('remove', async ctx => await removeFeedCommand(ctx, chatID, getMessage));
|
|
|
+ bot.command('active', async ctx => await activeChatCommand(ctx, chatID, getMessage));
|
|
|
|
|
|
} else {
|
|
|
ctx.telegram.sendMessage(chatID, not_member_admin.text, isNotMemberOrAdminMarkup);
|
|
@@ -166,12 +145,17 @@ module.exports = bot => {
|
|
|
})
|
|
|
|
|
|
async function addFeedCommand(ctx, chat_id, getMessage) {
|
|
|
- let data = ctx.message.text.replace('/add', '').trim().split(' ');
|
|
|
+ let data = removeCommand(ctx.message.text, '/add').split(' ');
|
|
|
data = removeSpacesInArray(data);
|
|
|
|
|
|
const rssURL = data.slice(0, 1).toString();
|
|
|
const hashtags = removeNotHashtagsInArray(data);
|
|
|
|
|
|
+ if (hashtags.length > 3) {
|
|
|
+ ctx.telegram.sendMessage(getChatId(ctx), add_feed.max_hashtags);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (!isHashtagsValid(hashtags)) {
|
|
|
ctx.telegram.sendMessage(getChatId(ctx), add_feed.cmd_error, { parse_mode: 'HTML'});
|
|
|
return;
|
|
@@ -186,12 +170,8 @@ module.exports = bot => {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- await feedRepository.addFeed({
|
|
|
- rss_url: rssURL,
|
|
|
- hashtag: hashtags_formatted,
|
|
|
- title,
|
|
|
- chat_id
|
|
|
- });
|
|
|
+ const feed = new Feed(rssURL, hashtags_formatted, title, chat_id);
|
|
|
+ await feedRepository.addFeed(feed.getValue());
|
|
|
|
|
|
ctx.reply(add_feed.success);
|
|
|
const message = await getMessage();
|
|
@@ -205,7 +185,7 @@ module.exports = bot => {
|
|
|
}
|
|
|
|
|
|
async function removeFeedCommand(ctx, chat_id, getMessage) {
|
|
|
- const feedTitle = ctx.message.text.replace('/remove', '').trim();
|
|
|
+ const feedTitle = removeCommand(ctx.message.text, '/remove');
|
|
|
|
|
|
if (!feedTitle) {
|
|
|
ctx.telegram.sendMessage(getChatId(ctx), remove_feed.cmd_error, { parse_mode: 'HTML'});
|
|
@@ -225,5 +205,18 @@ module.exports = bot => {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- setupFeedActions({bot});
|
|
|
+ async function activeChatCommand(ctx, chat_id, getMessage) {
|
|
|
+ if (!(await feedRepository.containChat(chat_id))) {
|
|
|
+ ctx.reply(active_feed.error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ await chatRepository.updateActiveChat(true, chat_id);
|
|
|
+
|
|
|
+ ctx.reply(active_feed.success);
|
|
|
+ const message = await getMessage();
|
|
|
+ ctx.telegram.sendMessage(...message);
|
|
|
+ }
|
|
|
+
|
|
|
+ setupFeedActions({ bot });
|
|
|
}
|