@@ -157,22 +157,67 @@ telegram.post('/webhook', async (c) => {
157157 }
158158
159159 const telegramService = new TelegramService ( c . env ) ;
160+ const memberSheetServices = new MemberSheetServices ( c . env ) ;
160161
161162 // Check if the user has the bot activated (can receive messages)
162163 const hasBotActivated = await telegramService . canSendMessageToUser ( telegramId ) ;
163164
164165 if ( hasBotActivated ) {
165- // User has bot activated - send direct message
166- try {
167- await telegramService . sendMessage (
168- telegramId ,
169- `مرحباً ${ escapeMarkdownV2 ( fullName ) } \\!\n\n` +
170- `لقد تلقينا طلب انضمامك إلى المجموعة\\.\n\n` +
171- `يرجى استخدام الأمر /verify للتحقق من عضويتك حتى تتمكن من الوصول إلى المجموعة\\.`
172- ) ;
173- console . log ( `Sent verification message to user ${ telegramId } who has bot activated` ) ;
174- } catch ( error ) {
175- console . error ( 'Error sending message to user with bot activated:' , error ) ;
166+ // User has bot activated - check if they are verified
167+ const member = await memberSheetServices . getMemberByTelegramId ( telegramId . toString ( ) ) ;
168+
169+ if ( member && member . telegram_id ) {
170+ // User is verified - create and send private invite link
171+ try {
172+ const inviteLink = await telegramService . createChatInviteLink (
173+ chatId ,
174+ telegramId ,
175+ fullName
176+ ) ;
177+
178+ if ( inviteLink ) {
179+ // Send the private invite link to the user
180+ await telegramService . sendMessage (
181+ telegramId ,
182+ `مرحباً ${ escapeMarkdownV2 ( fullName ) } \\! 🎉\n\n` +
183+ `تم التحقق من عضويتك بنجاح\\.\n\n` +
184+ `إليك رابط الانضمام الخاص بك:\n` +
185+ `${ escapeMarkdownV2 ( inviteLink ) } \n\n` +
186+ `⚠️ *ملاحظة مهمة:*\n` +
187+ `• هذا الرابط خاص بك فقط\n` +
188+ `• يمكن استخدامه مرة واحدة فقط\n` +
189+ `• لا تشاركه مع أي شخص آخر`
190+ ) ;
191+
192+ console . log ( `Sent private invite link to verified user ${ telegramId } ` ) ;
193+
194+ // Optionally approve the join request automatically
195+ // await telegramService.approveChatJoinRequest(chatId, telegramId);
196+ } else {
197+ // Failed to create invite link - fall back to manual approval
198+ await telegramService . sendMessage (
199+ telegramId ,
200+ `مرحباً ${ escapeMarkdownV2 ( fullName ) } \\!\n\n` +
201+ `تم التحقق من عضويتك\\. سيتم الموافقة على طلبك قريباً\\.`
202+ ) ;
203+ console . log ( `Failed to create invite link, sent confirmation to user ${ telegramId } ` ) ;
204+ }
205+ } catch ( error ) {
206+ console . error ( 'Error creating/sending invite link:' , error ) ;
207+ }
208+ } else {
209+ // User has bot but not verified - ask them to verify
210+ try {
211+ await telegramService . sendMessage (
212+ telegramId ,
213+ `مرحباً ${ escapeMarkdownV2 ( fullName ) } \\!\n\n` +
214+ `لقد تلقينا طلب انضمامك إلى المجموعة\\.\n\n` +
215+ `يرجى استخدام الأمر /verify للتحقق من عضويتك حتى تتمكن من الوصول إلى المجموعة\\.`
216+ ) ;
217+ console . log ( `Sent verification message to unverified user ${ telegramId } ` ) ;
218+ } catch ( error ) {
219+ console . error ( 'Error sending message to user with bot activated:' , error ) ;
220+ }
176221 }
177222 } else {
178223 // User doesn't have bot activated - send silent message to group
0 commit comments