@@ -102,7 +102,11 @@ async function handleBotCommand(request, env) {
102102 // Handle text messages
103103 if ( update . message && update . message . text ) {
104104 const chatId = update . message . chat . id ;
105- const text = update . message . text ;
105+ const text = update . message . text . trim ( ) ;
106+
107+ if ( ! text ) {
108+ return new Response ( 'OK' ) ;
109+ }
106110
107111 if ( text === '/start' ) {
108112 return await handleStartCommand ( BOT_TOKEN , chatId ) ;
@@ -304,6 +308,13 @@ To create a new post, you have two options:
304308}
305309
306310async function handleSearchCommand ( BOT_TOKEN , chatId , text , env ) {
311+ // Safety check for text parameter
312+ if ( ! text || typeof text !== 'string' ) {
313+ const message = "❌ Invalid search command. Please try again." ;
314+ await sendTextMessage ( BOT_TOKEN , chatId , message , [ ] ) ;
315+ return new Response ( 'OK' , { status : 200 } ) ;
316+ }
317+
307318 const searchQuery = text . replace ( '/search' , '' ) . trim ( ) ;
308319
309320 if ( ! searchQuery ) {
@@ -824,10 +835,21 @@ function htmlToMarkdown(html) {
824835// Handle callback queries from inline buttons
825836async function handleCallbackQuery ( request , env , callbackQuery ) {
826837 const BOT_TOKEN = env . TELEGRAM_BOT_TOKEN ;
838+
839+ // Safety checks
840+ if ( ! callbackQuery || ! callbackQuery . message || ! callbackQuery . message . chat ) {
841+ return new Response ( 'OK' , { status : 200 } ) ;
842+ }
843+
827844 const chatId = callbackQuery . message . chat . id ;
828845 const data = callbackQuery . callback_data ;
829846 const queryId = callbackQuery . id ;
830847
848+ // Safety check for callback_data
849+ if ( ! data || typeof data !== 'string' ) {
850+ return new Response ( 'OK' , { status : 200 } ) ;
851+ }
852+
831853 // Answer the callback query first
832854 await fetch ( `https://api.telegram.org/bot${ BOT_TOKEN } /answerCallbackQuery` , {
833855 method : 'POST' ,
0 commit comments