Skip to content

Commit

Permalink
fix: add multiple messages to avoid the bot being blocked as spam
Browse files Browse the repository at this point in the history
  • Loading branch information
dubisdev committed Dec 2, 2022
1 parent 66e18f6 commit 3abb6e1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/DirectMessages/DMHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sendDirectMessage } from 'services/twitter-api';
import { findUser } from 'services/database';
import TEXTS from './texts';
import { getRandomUserNotFoundMessage } from './texts';
import { handleConfig, handleProject, handleDefaultDM, handleDelete, handleDeleteAll, handleInit, handleMain, handleHelp, handleThread } from './handlers';
import { handleSettings } from './settings/handleSettings';

Expand All @@ -12,7 +12,7 @@ function withUser() {
const userId = message.sender_id;

const user = await findUser(userId);
if (!user) return sendDirectMessage(userId, TEXTS.USER_NOT_FOUND);
if (!user) return sendDirectMessage(userId, getRandomUserNotFoundMessage());

return mainFunc(message, user);
};
Expand Down
12 changes: 8 additions & 4 deletions src/DirectMessages/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Project } from '@doist/todoist-api-typescript';
import TEXTS, { generateConfigText, generateInitText, generateInvalidDMText } from './texts';
import TEXTS, { generateConfigText, generateInitText, generateInvalidDMText, getRandomAddedToAccountText } from './texts';
import { getTodoistProjects, getTodoistUserData, revokeAccessToken, addTodoistTask } from 'services/todoist-api';
import { getProjectNumFromMessage, getDefaultTaskContent, getUserCustomTaskContent } from 'services/texts';
import { decryptString } from 'services/crypto';
Expand Down Expand Up @@ -123,7 +123,7 @@ const handleDefaultDM: DMHandlerFunction = async (message, user) => {

if (user.noResponse) return;

sendDirectMessage(userId, TEXTS.ADDED_TO_ACCOUNT);
sendDirectMessage(userId, getRandomAddedToAccountText());
};

const handleInvalidDM = async (message) => {
Expand Down Expand Up @@ -171,8 +171,10 @@ const handleMain: DMHandlerFunction = async (message: TWDirectMessage, user) =>
Bugsnag.notify(e);
return sendDirectMessage(userId, TEXTS.BAD_TOKEN + '\nErr: TDS_ERROR');
}

if (user.noResponse) return;

sendDirectMessage(userId, TEXTS.ADDED_TO_ACCOUNT);
sendDirectMessage(userId, getRandomAddedToAccountText());
};

const handleThread: DMHandlerFunction = async (message: TWDirectMessage, user) => {
Expand Down Expand Up @@ -214,7 +216,9 @@ const handleThread: DMHandlerFunction = async (message: TWDirectMessage, user) =
return sendDirectMessage(userId, TEXTS.BAD_TOKEN + '\nErr: TDS_ERROR');
}

sendDirectMessage(userId, TEXTS.ADDED_TO_ACCOUNT);
if (user.noResponse) return;

sendDirectMessage(userId, getRandomAddedToAccountText());

};

Expand Down
72 changes: 55 additions & 17 deletions src/DirectMessages/texts.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,81 @@
enum TWHookServerTexts {
USER_NOT_FOUND = 'πŸ”΄ User Not Found.\nPlease use\n/init\nto initialize your account',
INVALID_PROJECT_NUM = 'πŸ”΄ Invalid project number\n',
BAD_TOKEN = 'πŸ”΄ Something is wrong with your account configuration.\nPlease run\n/init\ncommand',
TWEETS_SAVED_TO = 'πŸ”΄ Now the tweets will be saved to:\n',
ADDED_TO_ACCOUNT = 'πŸ”΄ Added to your account',
CANT_DELETE = 'πŸ”΄ Can\'t delete your account. Try again. If the problem persists, contact @dubisdev_',
DELETED_ACCOUNT = 'πŸ”΄ All your account data has been deleted',
ALERT_DELETE = 'πŸ”΄ Are you sure you want to delete your account? This action CAN NOT be undone. \nType\n/deleteall\nto continue',
HELP = 'πŸ”΄ Available Commands:\n\n/init - Set up your account\n/config - Get your account data\n/delete - Delete your account\n/project <number> - Set up your default project\n/help - Shows this message\n\n⚠️ Don\'t forget the slash (/) before the command'
}

export const generateInitText = (userId: string) => `
πŸ”΄ Hi there!\n\
export const getRandomAddedToAccountText = () => {
const texts = [
'πŸ”΄ Added to your account',
'πŸ”΄ Tweet added to your account',
'πŸ”΄ I\'ve added that to your account',
'πŸ”΄ The tweet\'s been added to your account',
'πŸš€ Tweet added to your account',
'πŸš€ Added to your account',
'πŸš€ I\'ve added that to your account',
'πŸš€ The tweet\'s been added to your account'] as const;
return texts[Math.floor(Math.random() * texts.length)];
};

export const getRandomUserNotFoundMessage = () => {
const texts = [
'πŸ”΄ User not found. Please send \'/init\' to start using this bot',
'❌ User not found. Please send \'/init\' to start using this bot',
'πŸ”΄ I couldn\'t find you in my database. Please send \'/init\' to start using this bot',
'❌ I couldn\'t find you in my database. Please send \'/init\' to start using this bot',
'❓ User Not Found. Who are you? Please send \'/init\' to start using this bot',
'❓ I couldn\'t find you in my database. Who are you? Please send \'/init\' to start using this bot',
'πŸ”΄ User Not Found. Who are you? Please send \'/init\' to start using this bot'
] as const;
return texts[Math.floor(Math.random() * texts.length)];
};

const TWHookServerTexts = {
USER_NOT_FOUND: 'πŸ”΄ User Not Found.\nPlease use\n/init\nto initialize your account',
INVALID_PROJECT_NUM: 'πŸ”΄ Invalid project number\n',
BAD_TOKEN: 'πŸ”΄ Something is wrong with your account configuration.\nPlease run\n/init\ncommand',
TWEETS_SAVED_TO: 'πŸ”΄ Now tweets will be saved to:\n',
CANT_DELETE: 'πŸ”΄ Can\'t delete your account. Maybe your account has already been deleted?',
DELETED_ACCOUNT: 'πŸ”΄ All your account data has been deleted',
ALERT_DELETE: 'πŸ”΄ Are you sure you want to delete your account? This action CAN NOT be undone. \nType\n/deleteall\nto continue',
HELP: 'πŸ”΄ Available Commands:\n\n/init - Set up your account\n/config - Get your account data\n/delete - Delete your account\n/project <number> - Set up your default project\n/help - Shows this message\n\n⚠️ Don\'t forget the slash (/) before the command'
} as const;

export const generateInitText = (userId: string) => {
const HEADER_EMOJIS = ['πŸ”΄', 'πŸ‘‹', 'πŸš€', 'πŸ“’'] as const;
const headerEmoji = HEADER_EMOJIS[Math.floor(Math.random() * HEADER_EMOJIS.length)];
return `
${headerEmoji} Hi there!\n\
I'm AddTodoist bot and I'm here to help you save tweets and threads to your Todoist account.\n\
\nFirst of all, you must cofigure me for making me able to access your account.\n\n\
Follow this steps:\n\
1. Go to https://todoist.com/oauth/authorize?client_id=${process.env.TODOIST_CLIENT_ID}&scope=data:read_write&state=${userId}\n\
2. Grant app permissions.\n\
3. When you are done I will come back to set up your projects cofiguration.`;
};

export const generateConfigText = ({
username,
email,
projectName,
projectId,
}) => `
πŸ”΄ This is your current configuration:\n\
}) => {
const HEADER_EMOJIS = ['πŸ”΄', 'βš™οΈ', '⬇️', 'πŸ”Ž'] as const;
const headerEmoji = HEADER_EMOJIS[Math.floor(Math.random() * HEADER_EMOJIS.length)];
return `
${headerEmoji} This is your current configuration:\n\
- Username: ${username}\n\
- Email: ${email}\n\
- Project: ${projectName} (id: ${projectId})`;
};

export const generateInvalidDMText = (username: string) => `
πŸ”΄ Welcome ${username}!\n\n\
export const generateInvalidDMText = (username: string) => {
const HEADER_EMOJIS = ['πŸ”΄', '🚫', '❌', '❓'] as const;
const headerEmoji = HEADER_EMOJIS[Math.floor(Math.random() * HEADER_EMOJIS.length)];
return `
${headerEmoji} Hi ${username}!\n\n\
It seems like you have sent me an invalid message.\n\n\
Please, send me a valid command or type\n\
'/init'\n\
to start your account configuration.\n\n\
For more information type\n\
'/help'\n\n\
⚠️ Don't forget the slash (/) before the commands`;
};

export default TWHookServerTexts;

0 comments on commit 3abb6e1

Please sign in to comment.