Skip to content

Commit d529655

Browse files
committed
update
1 parent d8724df commit d529655

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/telegram_bot.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ export default class TelegramBot {
187187
const ctx = new TelegramExecutionContext(this, this.update);
188188
this.currentContext = ctx;
189189

190+
if (!(await ctx.shouldProcess())) {
191+
console.log('Skipping update processing based on context validation');
192+
return new Response('ok');
193+
}
194+
190195
// Run middleware
191196
for (const middleware of this.middleware) {
192197
const result = await middleware(ctx);

src/telegram_execution_context.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,60 @@ export default class TelegramExecutionContext {
8484
* Determine the type of update received
8585
* @returns The update type as a string
8686
*/
87+
88+
/**
89+
* Determine if the current update should be processed.
90+
* For business messages, this checks if the connection is valid and has reply permissions.
91+
*/
92+
public async shouldProcess(): Promise<boolean> {
93+
if (this.update_type !== 'business_message') {
94+
return true;
95+
}
96+
97+
const connectionId = this.update.business_message?.business_connection_id?.toString();
98+
if (!connectionId) {
99+
return true;
100+
}
101+
102+
if (TelegramExecutionContext.poisonedConnections.has(connectionId)) {
103+
return false;
104+
}
105+
106+
let ownerId = TelegramExecutionContext.businessOwners.get(connectionId);
107+
if (ownerId === undefined) {
108+
try {
109+
const response = await this.api.getBusinessConnection(this.bot.api.toString(), connectionId);
110+
if (response.status === 200) {
111+
const json = await response.json() as { ok: boolean, result: { user: { id: number }, user_chat_id: number, can_reply: boolean } };
112+
if (json.ok && json.result) {
113+
ownerId = json.result.user?.id || json.result.user_chat_id;
114+
if (ownerId) {
115+
TelegramExecutionContext.businessOwners.set(connectionId, ownerId);
116+
}
117+
if (json.result.can_reply === false) {
118+
console.warn('Business connection ' + connectionId + ' lacks reply permissions, poisoning connection');
119+
TelegramExecutionContext.poisonedConnections.add(connectionId);
120+
return false;
121+
}
122+
}
123+
}
124+
} catch (e) {
125+
if (e instanceof Error && e.message === 'BUSINESS_CONNECTION_INVALID') {
126+
console.warn('Business connection ' + connectionId + ' is invalid, poisoning connection');
127+
TelegramExecutionContext.poisonedConnections.add(connectionId);
128+
return false;
129+
}
130+
console.warn('Failed to fetch business connection info:', e);
131+
}
132+
}
133+
134+
if (ownerId !== undefined && this.getChatId() === ownerId.toString()) {
135+
return false;
136+
}
137+
138+
return true;
139+
}
140+
87141
private determineUpdateType(): string {
88142
if (this.update.message?.photo) {
89143
return 'photo';

0 commit comments

Comments
 (0)