@@ -82,25 +82,33 @@ func main() {
8282 }
8383
8484 if sanitized {
85+ // Create send options with reply if original was a reply
86+ sendOpts := & telebot.SendOptions {ParseMode : telebot .ModeMarkdown }
87+ if m .IsReply () {
88+ sendOpts .ReplyTo = m .ReplyTo
89+ }
90+
8591 if isPhotoURL && len (photoURLs ) > 0 {
8692 // Create album of photos with caption on first photo
8793 album := make (telebot.Album , 0 )
8894 for i , photoPath := range photoURLs {
8995 var photo * telebot.Photo
9096 if i == 0 {
97+ // Escape brackets in the URL to prevent Markdown parsing issues
98+ escapedURL := escapeMarkdown (m .Text )
9199 // Add caption to first photo
92100 photo = & telebot.Photo {
93101 File : telebot .FromDisk (photoPath ),
94- Caption : fmt .Sprintf ("@%s said: [Original Link](%s)" , username , m . Text ),
102+ Caption : fmt .Sprintf ("@%s said: [Original Link](%s)" , username , escapedURL ),
95103 }
96104 } else {
97105 photo = & telebot.Photo {File : telebot .FromDisk (photoPath )}
98106 }
99107 album = append (album , photo )
100108 }
101109
102- // Send the album
103- _ , err := b .SendAlbum (m .Chat , album , & telebot. SendOptions { ParseMode : telebot . ModeMarkdown } )
110+ // Send the album with reply
111+ _ , err := b .SendAlbum (m .Chat , album , sendOpts )
104112 if err != nil {
105113 log .Printf ("Failed to send album: %v" , err )
106114 }
@@ -110,13 +118,25 @@ func main() {
110118 os .Remove (photoPath )
111119 }
112120 } else {
121+ var err error
122+ // Escape any Markdown special characters in the sanitized URL
123+ escapedMsg := escapeMarkdown (sanitizedMsg )
124+
113125 if m .FromGroup () && strings .Contains (m .Text , "anon" ) {
114- b .Send (m .Chat , strings .Replace (sanitizedMsg , "anon" , "" , 1 ))
126+ _ , err = b .Send (m .Chat , strings .Replace (escapedMsg , "anon" , "" , 1 ), sendOpts )
115127 } else {
116- b .Send (m .Chat , "@" + username + " said: " + sanitizedMsg )
128+ _ , err = b .Send (m .Chat , "@" + username + " said: " + escapedMsg , sendOpts )
129+ }
130+ if err != nil {
131+ log .Printf ("Failed to send message: %v" , err )
132+ return
117133 }
118134 }
119- b .Delete (m )
135+
136+ // Only try to delete the original message if we successfully sent the new one
137+ if err := b .Delete (m ); err != nil {
138+ log .Printf ("Failed to delete original message: %v" , err )
139+ }
120140 }
121141 })
122142
@@ -450,6 +470,15 @@ func ExpandUrl(shortURL string) (string, error) {
450470 return resp .Request .URL .String (), nil
451471}
452472
473+ func escapeMarkdown (text string ) string {
474+ text = strings .ReplaceAll (text , "[" , "\\ [" )
475+ text = strings .ReplaceAll (text , "]" , "\\ ]" )
476+ text = strings .ReplaceAll (text , "_" , "\\ _" )
477+ text = strings .ReplaceAll (text , "*" , "\\ *" )
478+ text = strings .ReplaceAll (text , "`" , "\\ `" )
479+ return text
480+ }
481+
453482func downloadImage (imageURL string ) (string , error ) {
454483 // Create cache directory if it doesn't exist
455484 cacheDir := "image_cache"
0 commit comments