Skip to content

Commit efb5c7c

Browse files
Add support for handling chat join requests in Telegram bot
1 parent 5e6db1d commit efb5c7c

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/routes/telegram.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,43 @@ telegram.post('/webhook', async (c) => {
109109
return c.json({ ok: true });
110110
}
111111

112+
// Handle chat join requests
113+
if (update.chat_join_request) {
114+
const joinRequest = update.chat_join_request;
115+
const telegramId = joinRequest.from.id;
116+
const chatId = joinRequest.chat.id;
117+
const username = joinRequest.from.username;
118+
const firstName = joinRequest.from.first_name;
119+
const lastName = joinRequest.from.last_name;
120+
121+
console.log('Received chat join request:', {
122+
chatId,
123+
chatTitle: joinRequest.chat.title,
124+
userId: telegramId,
125+
username,
126+
firstName,
127+
lastName,
128+
date: new Date(joinRequest.date * 1000).toISOString()
129+
});
130+
131+
// You can add your custom logic here to:
132+
// 1. Automatically approve/decline the request
133+
// 2. Check if user is in member sheets
134+
// 3. Send notification to admins
135+
// 4. Store the request for later processing
136+
137+
// Example: Log the request (you can extend this with your own logic)
138+
const telegramService = new TelegramService(c.env);
139+
140+
// Optionally approve the request automatically
141+
// await telegramService.approveChatJoinRequest(chatId, telegramId);
142+
143+
// Optionally decline the request
144+
// await telegramService.declineChatJoinRequest(chatId, telegramId);
145+
146+
return c.json({ ok: true });
147+
}
148+
112149
if (!update.message) {
113150
return c.json({ ok: true });
114151
}

src/services/telegram.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ export class TelegramService {
200200
},
201201
body: JSON.stringify({
202202
url: webhookUrl,
203+
allowed_updates: [
204+
'message',
205+
'callback_query',
206+
'chat_join_request'
207+
]
203208
}),
204209
});
205210

@@ -377,4 +382,58 @@ _يُستخدم هذا البوت للتحقق من العضوية والإشع
377382

378383
await this.sendMessage(chatId, welcomeText.trim());
379384
}
385+
386+
async approveChatJoinRequest(chatId: number | string, userId: number): Promise<void> {
387+
const url = `https://api.telegram.org/bot${this.botToken}/approveChatJoinRequest`;
388+
389+
try {
390+
const response = await fetch(url, {
391+
method: 'POST',
392+
headers: {
393+
'Content-Type': 'application/json',
394+
},
395+
body: JSON.stringify({
396+
chat_id: chatId,
397+
user_id: userId,
398+
}),
399+
});
400+
401+
if (!response.ok) {
402+
const error = await response.text();
403+
throw new Error(`Failed to approve chat join request: ${response.status} ${error}`);
404+
}
405+
406+
console.log(`Approved chat join request for user ${userId} in chat ${chatId}`);
407+
} catch (error) {
408+
console.error('Error approving chat join request:', error);
409+
throw error;
410+
}
411+
}
412+
413+
async declineChatJoinRequest(chatId: number | string, userId: number): Promise<void> {
414+
const url = `https://api.telegram.org/bot${this.botToken}/declineChatJoinRequest`;
415+
416+
try {
417+
const response = await fetch(url, {
418+
method: 'POST',
419+
headers: {
420+
'Content-Type': 'application/json',
421+
},
422+
body: JSON.stringify({
423+
chat_id: chatId,
424+
user_id: userId,
425+
}),
426+
});
427+
428+
if (!response.ok) {
429+
const error = await response.text();
430+
throw new Error(`Failed to decline chat join request: ${response.status} ${error}`);
431+
}
432+
433+
console.log(`Declined chat join request for user ${userId} in chat ${chatId}`);
434+
} catch (error) {
435+
console.error('Error declining chat join request:', error);
436+
throw error;
437+
}
438+
}
380439
}

src/types/telegram.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ export interface TelegramUpdate {
6767
};
6868
data?: string;
6969
};
70+
chat_join_request?: {
71+
chat: {
72+
id: number;
73+
title?: string;
74+
username?: string;
75+
type: string;
76+
};
77+
from: {
78+
id: number;
79+
is_bot: boolean;
80+
first_name: string;
81+
last_name?: string;
82+
username?: string;
83+
language_code?: string;
84+
};
85+
user_chat_id: number;
86+
date: number;
87+
bio?: string;
88+
invite_link?: {
89+
invite_link: string;
90+
creator: {
91+
id: number;
92+
is_bot: boolean;
93+
first_name: string;
94+
username?: string;
95+
};
96+
creates_join_request: boolean;
97+
is_primary?: boolean;
98+
is_revoked?: boolean;
99+
};
100+
};
70101
}
71102

72103
export interface InlineKeyboardButton {

0 commit comments

Comments
 (0)