Skip to content

Commit fb1ab70

Browse files
authored
fix(telegram): upload of images (#143)
* fix(telegram): upload of images Signed-off-by: mudler <[email protected]> * Parse markdown Signed-off-by: mudler <[email protected]> --------- Signed-off-by: mudler <[email protected]>
1 parent 94f4d35 commit fb1ab70

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

services/connectors/telegram.go

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"errors"
88
"fmt"
9+
"io"
910
"net/http"
1011
"os"
1112
"os/signal"
@@ -119,6 +120,36 @@ func (t *Telegram) cancelActiveJobForChat(chatID int64) {
119120
}
120121
}
121122

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+
122153
// handleMultimediaContent processes and sends multimedia content from the agent's response
123154
func (t *Telegram) handleMultimediaContent(ctx context.Context, chatID int64, res *types.JobResult) ([]string, error) {
124155
var urls []string
@@ -133,25 +164,8 @@ func (t *Telegram) handleMultimediaContent(ctx context.Context, chatID int64, re
133164
if imagesUrls, exists := state.Metadata[actions.MetadataImages]; exists {
134165
for _, url := range xstrings.UniqueSlice(imagesUrls.([]string)) {
135166
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)
155169
}
156170
}
157171
}
@@ -188,6 +202,19 @@ func (t *Telegram) handleMultimediaContent(ctx context.Context, chatID int64, re
188202
return urls, nil
189203
}
190204

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\nReferences:\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+
191218
func (t *Telegram) handleUpdate(ctx context.Context, b *bot.Bot, a *agent.Agent, update *models.Update) {
192219
username := update.Message.From.Username
193220

@@ -222,8 +249,9 @@ func (t *Telegram) handleUpdate(ctx context.Context, b *bot.Bot, a *agent.Agent,
222249

223250
// Send initial placeholder message
224251
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,
227255
})
228256
if err != nil {
229257
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,
305333
xlog.Error("Error handling multimedia content", "error", err)
306334
}
307335

308-
// Prepare the final response with URLs if any
309-
finalResponse := res.Response
310-
if len(urls) > 0 {
311-
finalResponse += "\n\nReferences:\n"
312-
for i, url := range urls {
313-
finalResponse += fmt.Sprintf("🔗 %d. %s\n", i+1, url)
314-
}
315-
}
316-
317336
// Update the message with the final response
318337
_, err = b.EditMessageText(ctx, &bot.EditMessageTextParams{
319338
ChatID: update.Message.Chat.ID,
320339
MessageID: msg.ID,
321-
Text: finalResponse,
340+
Text: formatResponseWithURLs(res.Response, urls),
341+
ParseMode: models.ParseModeMarkdown,
322342
})
323343
if err != nil {
324344
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+
})
325350
}
326351
}
327352

0 commit comments

Comments
 (0)