6
6
"encoding/base64"
7
7
"errors"
8
8
"fmt"
9
+ "io"
9
10
"net/http"
10
11
"os"
11
12
"os/signal"
@@ -119,6 +120,36 @@ func (t *Telegram) cancelActiveJobForChat(chatID int64) {
119
120
}
120
121
}
121
122
123
+ // sendImageToTelegram downloads and sends an image to Telegram
124
+ func sendImageToTelegram (ctx context.Context , b * bot.Bot , chatID int64 , url string ) error {
125
+ resp , err := http .Get (url )
126
+ if err != nil {
127
+ return fmt .Errorf ("error downloading image: %w" , err )
128
+ }
129
+ defer resp .Body .Close ()
130
+
131
+ // Read the entire body into memory
132
+ bodyBytes , err := io .ReadAll (resp .Body )
133
+ if err != nil {
134
+ return fmt .Errorf ("error reading image body: %w" , err )
135
+ }
136
+
137
+ // Send image with caption
138
+ _ , err = b .SendPhoto (ctx , & bot.SendPhotoParams {
139
+ ChatID : chatID ,
140
+ Photo : & models.InputFileUpload {
141
+ Filename : "image.jpg" ,
142
+ Data : bytes .NewReader (bodyBytes ),
143
+ },
144
+ Caption : "Generated image" ,
145
+ })
146
+ if err != nil {
147
+ return fmt .Errorf ("error sending photo: %w" , err )
148
+ }
149
+
150
+ return nil
151
+ }
152
+
122
153
// handleMultimediaContent processes and sends multimedia content from the agent's response
123
154
func (t * Telegram ) handleMultimediaContent (ctx context.Context , chatID int64 , res * types.JobResult ) ([]string , error ) {
124
155
var urls []string
@@ -133,25 +164,8 @@ func (t *Telegram) handleMultimediaContent(ctx context.Context, chatID int64, re
133
164
if imagesUrls , exists := state .Metadata [actions .MetadataImages ]; exists {
134
165
for _ , url := range xstrings .UniqueSlice (imagesUrls .([]string )) {
135
166
xlog .Debug ("Sending photo" , "url" , url )
136
-
137
- resp , err := http .Get (url )
138
- if err != nil {
139
- xlog .Error ("Error downloading image" , "error" , err .Error ())
140
- continue
141
- }
142
- defer resp .Body .Close ()
143
-
144
- // Send image with caption
145
- _ , err = t .bot .SendPhoto (ctx , & bot.SendPhotoParams {
146
- ChatID : chatID ,
147
- Photo : & models.InputFileUpload {
148
- Filename : "image.jpg" ,
149
- Data : resp .Body ,
150
- },
151
- Caption : "Generated image" ,
152
- })
153
- if err != nil {
154
- xlog .Error ("Error sending photo" , "error" , err .Error ())
167
+ if err := sendImageToTelegram (ctx , t .bot , chatID , url ); err != nil {
168
+ xlog .Error ("Error handling image" , "error" , err )
155
169
}
156
170
}
157
171
}
@@ -188,6 +202,19 @@ func (t *Telegram) handleMultimediaContent(ctx context.Context, chatID int64, re
188
202
return urls , nil
189
203
}
190
204
205
+ // formatResponseWithURLs formats the response text and creates message entities for URLs
206
+ func formatResponseWithURLs (response string , urls []string ) string {
207
+ finalResponse := response
208
+ if len (urls ) > 0 {
209
+ finalResponse += "\n \n References:\n "
210
+ for i , url := range urls {
211
+ finalResponse += fmt .Sprintf ("🔗 %d. %s\n " , i + 1 , url )
212
+ }
213
+ }
214
+
215
+ return bot .EscapeMarkdown (finalResponse )
216
+ }
217
+
191
218
func (t * Telegram ) handleUpdate (ctx context.Context , b * bot.Bot , a * agent.Agent , update * models.Update ) {
192
219
username := update .Message .From .Username
193
220
@@ -222,8 +249,9 @@ func (t *Telegram) handleUpdate(ctx context.Context, b *bot.Bot, a *agent.Agent,
222
249
223
250
// Send initial placeholder message
224
251
msg , err := b .SendMessage (ctx , & bot.SendMessageParams {
225
- ChatID : update .Message .Chat .ID ,
226
- Text : telegramThinkingMessage ,
252
+ ChatID : update .Message .Chat .ID ,
253
+ Text : bot .EscapeMarkdown (telegramThinkingMessage ),
254
+ ParseMode : models .ParseModeMarkdown ,
227
255
})
228
256
if err != nil {
229
257
xlog .Error ("Error sending initial message" , "error" , err )
@@ -305,23 +333,20 @@ func (t *Telegram) handleUpdate(ctx context.Context, b *bot.Bot, a *agent.Agent,
305
333
xlog .Error ("Error handling multimedia content" , "error" , err )
306
334
}
307
335
308
- // Prepare the final response with URLs if any
309
- finalResponse := res .Response
310
- if len (urls ) > 0 {
311
- finalResponse += "\n \n References:\n "
312
- for i , url := range urls {
313
- finalResponse += fmt .Sprintf ("🔗 %d. %s\n " , i + 1 , url )
314
- }
315
- }
316
-
317
336
// Update the message with the final response
318
337
_ , err = b .EditMessageText (ctx , & bot.EditMessageTextParams {
319
338
ChatID : update .Message .Chat .ID ,
320
339
MessageID : msg .ID ,
321
- Text : finalResponse ,
340
+ Text : formatResponseWithURLs (res .Response , urls ),
341
+ ParseMode : models .ParseModeMarkdown ,
322
342
})
323
343
if err != nil {
324
344
xlog .Error ("Error updating final message" , "error" , err )
345
+ b .EditMessageText (ctx , & bot.EditMessageTextParams {
346
+ ChatID : update .Message .Chat .ID ,
347
+ MessageID : msg .ID ,
348
+ Text : "there was an internal error. try again!" ,
349
+ })
325
350
}
326
351
}
327
352
0 commit comments