Skip to content

Commit

Permalink
Add messages logging to TelegramService
Browse files Browse the repository at this point in the history
  • Loading branch information
ashkuc committed Oct 16, 2022
1 parent 26af3d8 commit c642078
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/telegram/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Update } from 'typegram';
import { Address } from '@unique-nft/utils';
import { SdkService } from '../sdk/service';
import { CacheConfig } from '../config/cache.config';
import { formatDuration } from './utils';
import { formatDuration, getUsername } from './utils';

@Injectable()
export class TelegramService implements OnModuleInit {
Expand Down Expand Up @@ -47,6 +47,9 @@ export class TelegramService implements OnModuleInit {

async onMessage(ctx) {
const address = ctx.message?.text || '';

this.logger.log(`${getUsername(ctx.message?.from)} -> bot: ${address}`);

if (Address.is.substrateAddress(address)) {
await this.tryDrop(ctx, address);
} else if (Address.is.ethereumAddress(address)) {
Expand All @@ -59,6 +62,8 @@ export class TelegramService implements OnModuleInit {

private reply(ctx, message: string, options?) {
try {
this.logger.log(`bot -> ${getUsername(ctx.message?.from)}: ${message}`);

return ctx.reply(message, {
...options,
reply_to_message_id: ctx.message.message_id,
Expand Down Expand Up @@ -122,8 +127,7 @@ export class TelegramService implements OnModuleInit {

await this.reply(
ctx,
`The payment successful.
Current balance: ${balance.availableBalance.formatted}`,
`The payment successful. Current balance: ${balance.availableBalance.formatted}`,
{
reply_markup: {
resize_keyboard: true,
Expand Down
10 changes: 10 additions & 0 deletions src/telegram/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { User } from 'typegram';

export function formatDuration(duration: number) {
const hours = Math.floor(duration / 3600);
const minutes = Math.floor((duration - hours * 3600) / 60);
Expand All @@ -9,3 +11,11 @@ export function formatDuration(duration: number) {
values.push(`0${seconds}`.substr(-2));
return values.join(':');
}

export function getUsername(user?: User): string {
if (!user) return 'unknown user';

const { username, id } = user;

return username ? `@${username}` : `id:${id}`;
}

0 comments on commit c642078

Please sign in to comment.