Skip to content

Commit

Permalink
fix: prevent message is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyagung committed May 4, 2024
1 parent 1a2570e commit a740c5a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/sale/sale.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { id } from 'date-fns/locale';

const CONF_LOCALE = { locale: id };
const CONF_TZ = 'Asia/Jakarta';
const CONF_TIME_FORMAT = 'HH:mm z';
const CONF_DATE_FORMAT_SHORT = 'EEEE, dd MMMM yyyy';
const CONF_DATE_FORMAT_LONG = 'EEEE, dd MMMM yyyy - HH:mm z';
const CONF_DATE_FORMAT_LONG = `${CONF_DATE_FORMAT_SHORT} - ${CONF_TIME_FORMAT}`;
const CONF_TZ_FORMAT = 'zzzz (OOO)';
const CONF_NOW = () => new Date();

Expand Down Expand Up @@ -50,6 +51,9 @@ switch (process.env.TELEGRAM_SALE_DAY) {
const CONF_RESET_DAY_MINUS_1_WEEK = () => subWeeks(CONF_RESET_DAY(), 1);
const CONF_SALE_DAY = () => subDays(CONF_RESET_DAY(), 1);

export const TODAY_TIME = () =>
formatInTimeZone(CONF_NOW(), CONF_TZ, CONF_TIME_FORMAT, CONF_LOCALE);

export const TODAY_LONG = () =>
formatInTimeZone(CONF_NOW(), CONF_TZ, CONF_DATE_FORMAT_LONG, CONF_LOCALE);

Expand Down
47 changes: 31 additions & 16 deletions src/sale/sale.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { SaleService } from './sale.service';
import { Logger } from '@nestjs/common';
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
import {
RESET_DAY,
RESET_DAY_MINUS_1_WEEK,
SALE_DAY,
TODAY_LONG,
TODAY_SHORT,
TODAY_TIME,
} from './sale.constant';
import { Context, Telegraf } from 'telegraf';
import { ConfigService } from '@nestjs/config';
import { sendMessageToGroup } from './sale.common';
import { subWeeks } from 'date-fns';

@Update()
export class SaleUpdate {
Expand All @@ -33,39 +32,55 @@ export class SaleUpdate {
@Cron(CronExpression.EVERY_HOUR)
async saleDayScheduler() {
if (TODAY_SHORT() === SALE_DAY()) {
this.logger.log('Running sale day scheduler');
this.logger.log(`Running sale day scheduler at ${TODAY_TIME()}`);
const users = await this.saleService.getUsersWithScheduledSales();

let message = `🔥 **Today Hot Sale**\n\n`;
message += `It's **${TODAY_LONG()}**. Want to join the sale? [Chat me!](tg://user?id=${this.configService.get<string>('TELEGRAM_SALE_BOT_TOKEN')!.split(':')[0]})\n\n`;
let messages: string[] = [];

let index: number = 0;
messages[index] = `🔥 **Today Hot Sale**\n\n`;
messages[index] +=
`It's **${TODAY_LONG()}**. Want to join the sale? [Chat me!](tg://user?id=${this.configService.get<string>('TELEGRAM_SALE_BOT_TOKEN')!.split(':')[0]})\n\n`;

users.forEach((user) => {
message += `💰 [${user.first_name}](tg://user?id=${user.id}) ${user.phone ? `(\`${user.phone}\`)` : ``}\n`;
let new_message = `💰 [${user.first_name}](tg://user?id=${user.id}) ${user.phone ? `(\`${user.phone}\`)` : ``}\n`;

user.posts.forEach((post, index) => {
if (index + 1 !== user.posts.length) {
message += `├ ${post.post.replace(/\n/g, ' ')}\n`;
new_message += `├ ${post.post.replace(/\n/g, ' ')}\n`;
} else {
message += `└ ${post.post.replace(/\n/g, ' ')}\n`;
new_message += `└ ${post.post.replace(/\n/g, ' ')}\n`;
}
});

message += `\n`;
new_message += `\n`;

// if previous and new message length is too long
if ((messages[index] + new_message).length >= 4000) {
// move to next array
index++;
// set empty string so it can be appended later
messages[index] = '';
}

messages[index] += new_message;
});

sendMessageToGroup(
this.bot.telegram,
this.configService.get<string>('TELEGRAM_GROUP_ID')!,
message,
);
messages.forEach((message) => {
sendMessageToGroup(
this.bot.telegram,
this.configService.get<string>('TELEGRAM_GROUP_ID')!,
message,
);
});
}
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async resetDayScheduler() {
if (TODAY_SHORT() === RESET_DAY_MINUS_1_WEEK()) {
this.logger.log('Running reset day scheduler');
// await this.saleService.disableAllEnabledPosts();
this.logger.log(`Running reset day scheduler at ${TODAY_TIME()}`);
await this.saleService.disableAllEnabledPosts();
}
}
}

0 comments on commit a740c5a

Please sign in to comment.