Skip to content

Commit 3eab409

Browse files
authored
Merge branch 'dev' into tokento
2 parents 63cbf1e + ffce7bb commit 3eab409

File tree

9 files changed

+126
-1
lines changed

9 files changed

+126
-1
lines changed

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Creator } from './entities/creator.entity';
1313
import { Intention } from './entities/intention.entity';
1414
import { IntentionRecord } from './entities/intentionRecord.entity';
1515
import { IntentionRecordTx } from './entities/intentionRecordTx.entity';
16+
import { ScSession } from './entities/scSession.entity';
1617
import { TgGroupAndChannel } from './entities/tgGroupAndChannel.entity';
1718
import { TgMessage } from './entities/tgMessage.entity';
1819
import { MetricsModule } from './metrics';
@@ -61,6 +62,7 @@ import { UnitOfWorkModule } from './unitOfWork';
6162
IntentionRecordTx,
6263
TgMessage,
6364
TgGroupAndChannel,
65+
ScSession,
6466
]),
6567
ActionModule.forRoot(),
6668
MetricsModule,

src/entities/scSession.entity.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
2+
3+
import { BaseEntity } from './base.entity';
4+
5+
@Entity()
6+
@Index(['sessionId'], { unique: true })
7+
export class ScSession extends BaseEntity {
8+
@PrimaryGeneratedColumn()
9+
public readonly id: bigint;
10+
11+
@Column({ type: 'varchar' })
12+
public sessionId: string;
13+
14+
@Column({ type: 'varchar' })
15+
public data: string;
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class CreateTableScSession1731076287218 implements MigrationInterface {
4+
name = 'CreateTableScSession1731076287218';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "sc_session" ("createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, "id" SERIAL NOT NULL, "sessionId" character varying NOT NULL, "data" character varying NOT NULL, CONSTRAINT "PK_fb10d649e63815947580f4c6407" PRIMARY KEY ("id"))`,
9+
);
10+
await queryRunner.query(
11+
`CREATE UNIQUE INDEX "IDX_c8eae9e5d612665913e4346081" ON "sc_session" ("sessionId") `,
12+
);
13+
}
14+
15+
public async down(queryRunner: QueryRunner): Promise<void> {
16+
await queryRunner.query(
17+
`DROP INDEX "public"."IDX_c8eae9e5d612665913e4346081"`,
18+
);
19+
await queryRunner.query(`DROP TABLE "sc_session"`);
20+
}
21+
}

src/modules/tgbot/flashNewsBot.service.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
22
import { ethers } from 'ethers';
33
import html2md from 'html-to-md';
44
import { LRUCache } from 'lru-cache';
5+
import { customAlphabet } from 'nanoid';
56
import TelegramBot, {
67
ChatMemberUpdated,
78
ParseMode,
@@ -10,9 +11,11 @@ import { LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
1011

1112
import { NetworkDto } from 'src/common/dto';
1213
import configFactory from 'src/config';
14+
import { ScSession } from 'src/entities/scSession.entity';
1315
import { TgGroupAndChannel } from 'src/entities/tgGroupAndChannel.entity';
1416
import { BusinessException } from 'src/exception/business.exception';
1517
import {
18+
ScSessionRepository,
1619
TgGroupAndChannelRepository,
1720
TgMessageRepository,
1821
} from 'src/repositories';
@@ -41,6 +44,7 @@ export class FlashNewsBotService implements OnModuleInit {
4144
private readonly coingeckoService: CoingeckoService,
4245
private readonly tgMessageRepository: TgMessageRepository,
4346
private readonly tgGroupAndChannelRepository: TgGroupAndChannelRepository,
47+
private readonly scSessionRepository: ScSessionRepository,
4448
) {}
4549

4650
async update(body: any) {
@@ -580,7 +584,9 @@ ${this.formatMarkdownV2(content).replaceAll(
580584
fromTokenAddress: string,
581585
toTokenAddress: string,
582586
settings: Settings,
587+
sc?: string,
583588
) {
589+
let shareText = '';
584590
const config = await configFactory();
585591
const userMiniApp = config.tgbot.userMiniApp;
586592
const newsChannelIdCn = config.tgbot.newsChannelIdCn;
@@ -617,6 +623,11 @@ ${this.formatMarkdownV2(content).replaceAll(
617623
let linkIndex = 0;
618624
let lang = 'cn';
619625
if (this.containsChineseCharacters(content)) {
626+
shareText = sc
627+
? sc
628+
: `嘿😎,这是可以自定义你一键操作的 magicLink🎯
629+
630+
现在通过我的邀请链接开始行动吧!`;
620631
newsChannelId = newsChannelIdCn;
621632
captionTemplate = `
622633
🟢*${this.formatMarkdownV2(title)}*🟢
@@ -634,6 +645,11 @@ ${this.formatMarkdownV2(content).replaceAll(
634645
🌈在您的群中推送 magicNews 邀请 [@magicNews](${tgbot}?startgroup=join_cn) 到您的群中
635646
`;
636647
} else {
648+
shareText = sc
649+
? sc
650+
: `Hey😎, here's the magicLink that can customize your one-click action🎯
651+
652+
Start ACTION from my invite link now!`;
637653
lang = 'en';
638654
newsChannelId = newsChannelIdEn;
639655
captionTemplate = `
@@ -682,6 +698,12 @@ ${this.formatMarkdownV2(content).replaceAll(
682698
];
683699
}
684700
inlineKeyboard.push(typeActions);
701+
702+
const nanoid = customAlphabet(
703+
'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
704+
8,
705+
);
706+
const shareTextArr: ScSession[] = [];
685707
const actions = settings.intentList;
686708
for (let i = 0; i < actions.length; i++) {
687709
const lineButtons = [];
@@ -699,13 +721,20 @@ ${this.formatMarkdownV2(content).replaceAll(
699721
) {
700722
const urlTmp = new URL(action.value);
701723
const pathSegments = urlTmp.pathname.split('/');
724+
const scOfUrl = urlTmp.searchParams.get('sc');
725+
const shareTextLink = scOfUrl ? scOfUrl : shareText;
726+
const sessionId = nanoid(20);
727+
shareTextArr.push({
728+
sessionId,
729+
data: shareTextLink,
730+
} as ScSession);
702731
const code = pathSegments[pathSegments.length - 1];
703732
const btnIndex = action.btnIndex ?? '';
704733
const btnIndexStr =
705734
btnIndex === ''
706735
? ''
707736
: Math.max(parseInt(btnIndex) - 1, 0).toString();
708-
url = `${userMiniApp}?startapp=${code}_${btnIndexStr}`;
737+
url = `${userMiniApp}?startapp=${code}_${btnIndexStr}___${sessionId}`;
709738
} else {
710739
url = action.value;
711740
}
@@ -717,6 +746,13 @@ ${this.formatMarkdownV2(content).replaceAll(
717746
}
718747
inlineKeyboard.push(lineButtons);
719748
}
749+
if (shareTextArr.length > 0) {
750+
try {
751+
await this.scSessionRepository.addMany(shareTextArr);
752+
} catch (error) {
753+
this.logger.error('sendNewsOrigin error', error.stack);
754+
}
755+
}
720756
const reply_markup = {
721757
inline_keyboard: inlineKeyboard,
722758
};
@@ -769,6 +805,10 @@ ${this.formatMarkdownV2(content).replaceAll(
769805
}
770806
}
771807

808+
async getScBySessionId(sessionId: string) {
809+
return await this.scSessionRepository.getData(sessionId);
810+
}
811+
772812
async editMessageReplyMarkupPollText(
773813
chatId: string,
774814
messageId: string,

src/modules/tgbot/tgbot.controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class TgbotController extends BaseController {
9595
body.fromTokenAddress,
9696
body.toTokenAddress,
9797
body.settings,
98+
body.sc,
9899
);
99100
return this.success(true);
100101
} catch (error) {
@@ -103,6 +104,17 @@ export class TgbotController extends BaseController {
103104
}
104105
}
105106

107+
@Get('getSc')
108+
async getSc(@Query('sessionId') sessionId: string) {
109+
try {
110+
const res = await this.flashNewsBotService.getScBySessionId(sessionId);
111+
return this.success(res);
112+
} catch (error) {
113+
this.logger.error(error);
114+
return this.error(error.message);
115+
}
116+
}
117+
106118
@Get('testEditMessageReplyMarkupPollText')
107119
async testEditMessageReplyMarkupPollText(
108120
@Query('userId') userId: string,

src/modules/tgbot/tgbot.dto.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export class SendNewsOriginRequestDto {
3030
})
3131
metadata: string;
3232

33+
@ApiProperty({
34+
name: 'sc',
35+
description: 'sc.',
36+
required: true,
37+
})
38+
@IsString()
39+
@IsNotEmpty()
40+
sc: string;
41+
3342
@ApiProperty({
3443
name: 'fromTokenAddress',
3544
description: 'Address of from token.',

src/modules/tgbot/tgbot.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
IntentionRecordRepository,
88
IntentionRecordTxRepository,
99
IntentionRepository,
10+
ScSessionRepository,
1011
TgGroupAndChannelRepository,
1112
TgMessageRepository,
1213
} from 'src/repositories';
@@ -40,6 +41,7 @@ import { CoingeckoService } from '../coingecko/coingecko.service';
4041
TgGroupAndChannelRepository,
4142
FlashNewsBotService,
4243
AibotService,
44+
ScSessionRepository,
4345
],
4446
exports: [TgbotService, FlashNewsBotService],
4547
controllers: [TgbotController],

src/repositories/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './intentionRecordTx.repository';
77
export * from './commission.repository';
88
export * from './tgMessage.repository';
99
export * from './tgGroupAndChannel.repository';
10+
export * from './scSession.repository';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
import { ScSession } from 'src/entities/scSession.entity';
4+
5+
import { BaseRepository } from './base.repository';
6+
import { UnitOfWork } from '../unitOfWork';
7+
8+
@Injectable()
9+
export class ScSessionRepository extends BaseRepository<ScSession> {
10+
public constructor(unitOfWork: UnitOfWork) {
11+
super(ScSession, unitOfWork);
12+
}
13+
14+
async getData(sessionId: string): Promise<string> {
15+
const res = await this.findOneBy({ sessionId });
16+
return res?.data ?? '';
17+
}
18+
19+
async setData(sessionId: string, data: string): Promise<void> {
20+
return await this.upsert({ sessionId, data }, true, ['sessionId']);
21+
}
22+
}

0 commit comments

Comments
 (0)