Skip to content

Commit

Permalink
support silent bot messages 1inch#170
Browse files Browse the repository at this point in the history
  • Loading branch information
Rulexec committed Nov 21, 2021
1 parent 8a462ad commit 454f397
Show file tree
Hide file tree
Showing 42 changed files with 365 additions and 90 deletions.
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
- Collect stats (at least requests per minute)
- Use external queue for telegram updates (for seamless updates)
- Write scripts for backup/restore
- Extract all commands to one object with their name/short description/long description/initializer for:
- viewConfig/setConfig can be implemented more convinient
- help will be always actual
- easy to implement command pallete https://github.com/Rulexec/shieldy/issues/3
- Write tests for `/viewConfig` and `/setConfig` pair
22 changes: 16 additions & 6 deletions src/__tests__/captcha/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('custom captcha', () => {
{name: 'should ask for custom question'},
{name: 'should ask for custom question, multianswer', isMultianswer: true},
{name: 'should ask for custom question, degradated', isDegradated: true},
])('$name', async ({isMultianswer, isDegradated}) => {
{name: 'should ask for custom question, silent', isSilent: true},
])('$name', async ({isMultianswer, isDegradated, isSilent = false}) => {
const {
appContext,
handleUpdate,
Expand All @@ -31,11 +32,18 @@ describe('custom captcha', () => {

await findChatById(appContext, groupChat.id);

await appContext.database.setChatProperty({
chatId: groupChat.id,
property: 'captchaType',
value: CaptchaType.CUSTOM,
});
await Promise.all([
appContext.database.setChatProperty({
chatId: groupChat.id,
property: 'captchaType',
value: CaptchaType.CUSTOM,
}),
appContext.database.setChatProperty({
chatId: groupChat.id,
property: 'silentMessages',
value: isSilent,
}),
]);

if (!isDegradated) {
await appContext.database.setChatProperty({
Expand Down Expand Up @@ -109,6 +117,7 @@ describe('custom captcha', () => {
expect(message.text).toBe(
`<a href="tg://user?id=${user.id}">@${user.username}</a>, Say my name (60 sec)`,
);
expect(Boolean(message.isSilent)).toBe(isSilent);
}

for (let i = 0; i < 2; i++) {
Expand Down Expand Up @@ -186,6 +195,7 @@ describe('custom captcha', () => {
'any message to this group within the time amount specified, otherwise ' +
'you will be kicked. Thank you! (60 sec)',
);
expect(Boolean(message.isSilent)).toBe(isSilent);
}

const anyMessageId = getUniqueCounterValue();
Expand Down
101 changes: 101 additions & 0 deletions src/__tests__/commands/silent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {findChatById} from '@root/helpers/find-chat';
import {getUniqueCounterValue} from '@root/util/id/unique-counter';
import {setupTest} from '../helpers/setup';
import {createMessage} from '../test-data/updates';

describe('/silent', () => {
const botTest = setupTest();

afterEach(botTest.afterEach);

it('should toggle silent notifications', async () => {
const {
appContext,
handleUpdate,
onIdle,
popMessages,
unixSeconds,
user,
groupChat,
} = await botTest.init();

await findChatById(appContext, groupChat.id);
await appContext.database.setChatProperty({
chatId: groupChat.id,
property: 'silentMessages',
value: false,
});

const checkSilentPing = async (expectedSilent: boolean) => {
await handleUpdate(
createMessage({
messageId: getUniqueCounterValue(),
user,
chat: groupChat,
unixSeconds,
text: '/ping',
isBotCommand: true,
}),
);
await onIdle();

{
const messages = popMessages();

expect(messages.length).toBe(1);
expect(messages[0].text).toBe('pong');
expect(Boolean(messages[0].isSilent)).toBe(expectedSilent);
}
};

await checkSilentPing(false);

await handleUpdate(
createMessage({
messageId: getUniqueCounterValue(),
user,
chat: groupChat,
unixSeconds,
text: '/silent',
isBotCommand: true,
}),
);
await onIdle();

{
// Should change to silent
const messages = popMessages();

expect(messages.length).toBe(1);
expect(messages[0].text).toBe(
'Sesuritu will send messages without sound',
);
expect(messages[0].isSilent).toBeTruthy();
}

await checkSilentPing(true);

await handleUpdate(
createMessage({
messageId: getUniqueCounterValue(),
user,
chat: groupChat,
unixSeconds,
text: '/silent',
isBotCommand: true,
}),
);
await onIdle();

{
// Should change to not silent
const messages = popMessages();

expect(messages.length).toBe(1);
expect(messages[0].text).toBe('Sesuritu will send messages with sound');
expect(messages[0].isSilent).toBeFalsy();
}

await checkSilentPing(false);
});
});
1 change: 1 addition & 0 deletions src/__tests__/telegram-bot-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class TelegramBotServer {
replyToMessageId: data.reply_to_message_id,
inlineKeyboard: inlineKeyboard.length ? inlineKeyboard : undefined,
unixSeconds,
isSilent: Boolean(data.disable_notification),
});

const chat = this.getChatById(data.chat_id);
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/test-data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Message = {
replyToMessageId?: number;
inlineKeyboard?: InlineKeyboardKey[];
unixSeconds: number;
isSilent?: boolean;
};

export type MessageEdit = {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/allowInvitingBots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function setupAllowInvitingBots(bot: Bot): void {
? 'allowInvitingBots_true'
: 'allowInvitingBots_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
6 changes: 5 additions & 1 deletion src/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {isGroup} from '@helpers/isGroup';
import {Bot} from '@root/types/index';
import {checkLock} from '@middlewares/checkLock';
import {botDeleteMessageSafe} from '@root/helpers/deleteMessageSafe';
import {Extra} from 'telegraf';

export function setupBan(bot: Bot): void {
bot.command('ban', checkLock, clarifyIfPrivateMessages, async (ctx) => {
Expand Down Expand Up @@ -61,6 +62,9 @@ export function setupBan(bot: Bot): void {
});
}
// Reply with success
await ctx.replyWithMarkdown(ctx.translate('trust_success'));
await ctx.replyWithMarkdown(
ctx.translate('trust_success'),
Extra.notifications(!ctx.dbchat.silentMessages),
);
});
}
4 changes: 3 additions & 1 deletion src/commands/banForFastRepliesToPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function setupBanForFastRepliesToPosts(bot: Bot): void {
? 'banForFastRepliesToPosts_true'
: 'banForFastRepliesToPosts_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/banNewTelegramUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function setupBanNewTelegramUsers(bot: Bot): void {
? 'banNewTelegramUsers_true'
: 'banNewTelegramUsers_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/banUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function setupBanUsers(bot: Bot): void {

ctx.replyWithMarkdown(
ctx.translate(chat.banUsers ? 'banUsers_true' : 'banUsers_false'),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
});
}
4 changes: 3 additions & 1 deletion src/commands/buttonText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function setupButtonText(bot: Bot): void {
});
await ctx.replyWithMarkdown(
ctx.translate('trust_success'),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
20 changes: 11 additions & 9 deletions src/commands/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ export function setupCaptcha(appContext: AppContext): void {
ctx.appContext.telegramApi.replyWithMarkdown(
ctx,
ctx.translate('captcha'),
Extra.inReplyTo(message.message_id).markup((m) =>
m.inlineKeyboard([
m.callbackButton(ctx.translate('simple'), 'simple'),
m.callbackButton(ctx.translate('digits'), 'digits'),
m.callbackButton(ctx.translate('button'), 'button'),
m.callbackButton(ctx.translate('image'), 'image'),
m.callbackButton(ctx.translate('custom'), 'custom'),
]),
),
Extra.inReplyTo(message.message_id)
.markup((m) =>
m.inlineKeyboard([
m.callbackButton(ctx.translate('simple'), 'simple'),
m.callbackButton(ctx.translate('digits'), 'digits'),
m.callbackButton(ctx.translate('button'), 'button'),
m.callbackButton(ctx.translate('image'), 'image'),
m.callbackButton(ctx.translate('custom'), 'custom'),
]),
)
.notifications(!ctx.dbchat.silentMessages),
),
);

Expand Down
5 changes: 4 additions & 1 deletion src/commands/captchaMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function setupCaptchaMessage(appContext: AppContext): void {
: 'captchaMessage_true'
: 'captchaMessage_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);

if (chat.customCaptchaMessage && chat.captchaMessage) {
Expand All @@ -46,6 +48,7 @@ export function setupCaptchaMessage(appContext: AppContext): void {
// @ts-ignore
chat.captchaMessage.message.chat = undefined;
await ctx.telegram.sendCopy(chat.id, chat.captchaMessage.message, {
...Extra.notifications(!ctx.dbchat.silentMessages),
entities: chat.captchaMessage.message.entities,
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/cas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function setupCAS(bot: Bot): void {

ctx.replyWithMarkdown(
ctx.translate(chat.cas ? 'cas_true' : 'cas_false'),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
});
}
16 changes: 13 additions & 3 deletions src/commands/customCaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function setupCustomCaptcha(appContext: AppContext): void {
text = ctx.translate('custom_no_variants');
}

await ctx.replyWithMarkdown(text);
await ctx.replyWithMarkdown(
text,
Extra.notifications(!ctx.dbchat.silentMessages),
);

return BotMiddlewareNextStrategy.abort;
},
Expand All @@ -52,7 +55,10 @@ export function setupCustomCaptcha(appContext: AppContext): void {
value: ctx.dbchat.customCaptchaVariants,
});

await ctx.replyWithMarkdown(ctx.translate('custom_removed'));
await ctx.replyWithMarkdown(
ctx.translate('custom_removed'),
Extra.notifications(!ctx.dbchat.silentMessages),
);

return BotMiddlewareNextStrategy.abort;
},
Expand All @@ -65,6 +71,7 @@ export function setupCustomCaptcha(appContext: AppContext): void {
async (ctx) => {
const message = await ctx.replyWithMarkdown(
ctx.translate('custom_add_question'),
Extra.notifications(!ctx.dbchat.silentMessages),
);
ctx.dbchat.lastReplySetting = {
type: ReplySettingType.ADD_CUSTOM_CAPTCHA,
Expand Down Expand Up @@ -115,6 +122,7 @@ export function setupCustomCaptcha(appContext: AppContext): void {
) {
const botMessage = await ctx.replyWithMarkdown(
ctx.translate('custom_add_answer'),
Extra.notifications(!ctx.dbchat.silentMessages),
);

ctx.dbchat.lastReplySetting = {
Expand Down Expand Up @@ -160,7 +168,9 @@ export function setupCustomCaptcha(appContext: AppContext): void {

await ctx.replyWithMarkdown(
ctx.translate('custom_success'),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/commands/deleteEntryMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function setupDeleteEntryMessages(bot: Bot): void {
? 'deleteEntryMessages_true'
: 'deleteEntryMessages_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/deleteEntryOnKick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function setupDeleteEntryOnKick(bot: Bot): void {
? 'deleteEntryOnKick_true'
: 'deleteEntryOnKick_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
},
);
Expand Down
5 changes: 4 additions & 1 deletion src/commands/greeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ export function setupGreeting(bot: Bot): void {
: 'greetsUsers_true'
: 'greetsUsers_false',
),
Extra.inReplyTo(ctx.message.message_id),
Extra.inReplyTo(ctx.message.message_id).notifications(
!ctx.dbchat.silentMessages,
),
);
if (chat.greetingMessage && chat.greetsUsers) {
// TODO: investigate
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
chat.greetingMessage.message.chat = undefined;
await ctx.telegram.sendCopy(chat.id, chat.greetingMessage.message, {
...Extra.notifications(!ctx.dbchat.silentMessages),
entities: chat.greetingMessage.message.entities,
});
}
Expand Down
Loading

0 comments on commit 454f397

Please sign in to comment.