Skip to content

Commit e1963e0

Browse files
committed
update
1 parent 0e21078 commit e1963e0

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/telegram_execution_context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export default class TelegramExecutionContext {
141141
}
142142
}
143143

144-
if (ownerId !== undefined && this.getChatId() === ownerId.toString()) {
144+
if (ownerId !== undefined && (this.getChatId() === ownerId.toString() || this.userId === ownerId)) {
145145
return false;
146146
}
147147

test/telegram_bot.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,78 @@ describe('telegram bot', () => {
109109
globalThis.fetch = originalFetch;
110110
}
111111
});
112+
113+
// Test for business message handling
114+
it('business message from owner should be skipped', async () => {
115+
const handler = vi.fn().mockResolvedValue(new Response('handler_called'));
116+
const bot = new TelegramBot('123456789').on(':message', handler);
117+
118+
const ownerId = 999;
119+
const connectionId = 'conn123';
120+
const originalFetch = globalThis.fetch;
121+
122+
globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({
123+
ok: true,
124+
result: {
125+
user: { id: ownerId },
126+
can_reply: true
127+
}
128+
}), { status: 200 }));
129+
130+
const request = new Request('http://example.com/123456789', {
131+
method: 'POST',
132+
body: JSON.stringify({
133+
business_message: {
134+
business_connection_id: connectionId,
135+
from: { id: ownerId },
136+
chat: { id: 123, type: 'private' },
137+
text: 'Hello from owner',
138+
message_id: 1
139+
}
140+
}),
141+
});
142+
143+
const response = await bot.handle(request);
144+
expect(await response.text()).toBe('ok');
145+
expect(handler).not.toHaveBeenCalled();
146+
147+
globalThis.fetch = originalFetch;
148+
});
149+
150+
it('business message from user should be processed', async () => {
151+
const handler = vi.fn().mockResolvedValue(new Response('handler_called'));
152+
const bot = new TelegramBot('123456789').on(':message', handler);
153+
154+
const ownerId = 999;
155+
const userId = 123;
156+
const connectionId = 'conn456';
157+
const originalFetch = globalThis.fetch;
158+
159+
globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({
160+
ok: true,
161+
result: {
162+
user: { id: ownerId },
163+
can_reply: true
164+
}
165+
}), { status: 200 }));
166+
167+
const request = new Request('http://example.com/123456789', {
168+
method: 'POST',
169+
body: JSON.stringify({
170+
business_message: {
171+
business_connection_id: connectionId,
172+
from: { id: userId },
173+
chat: { id: userId, type: 'private' },
174+
text: 'Hello from user',
175+
message_id: 2
176+
}
177+
}),
178+
});
179+
180+
const response = await bot.handle(request);
181+
expect(await response.text()).toBe('handler_called');
182+
expect(handler).toHaveBeenCalled();
183+
184+
globalThis.fetch = originalFetch;
185+
});
112186
});

0 commit comments

Comments
 (0)