Skip to content

Commit

Permalink
Refactor file upload to telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 11, 2024
1 parent fe6ceb4 commit 6b1c33a
Showing 1 changed file with 47 additions and 53 deletions.
100 changes: 47 additions & 53 deletions handlers/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
Expand Down Expand Up @@ -270,49 +269,7 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
status.AddLog(log)

case "application":
fileData, err := downloadFileToBytes(mediaURL)
if err != nil {
return status, err
}

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)

part, err := writer.CreateFormFile("document", filepath.Base(fileName))
if err != nil {
return status, err
}

_, err = part.Write(fileData)
if err != nil {
return status, err
}

_ = writer.WriteField("chat_id", msg.URN().Path())
if err != nil {
return status, err
}

if attachmentKeyBoard == nil {
err = writer.WriteField("reply_markup", `{"remove_keyboard":true}`)
} else {
err = writer.WriteField("reply_markup", string(jsonx.MustMarshal(keyboard)))
}
if err != nil {
return status, err
}

err = writer.WriteField("caption", caption)
if err != nil {
return status, err
}

err = writer.Close()
if err != nil {
return status, err
}

externalID, log, err := sendUploadDocument(msg, authToken, writer)
externalID, log, err := sendUploadDocument(msg, authToken, mediaURL, fileName, attachmentKeyBoard, caption)
status.SetExternalID(externalID)
hasError = err != nil
status.AddLog(log)
Expand Down Expand Up @@ -474,8 +431,48 @@ func escapeOdd(text string, c string) string {
return text
}

func sendUploadDocument(msg courier.Msg, token string, writer *multipart.Writer) (string, *courier.ChannelLog, error) {
func sendUploadDocument(msg courier.Msg, token string, mediaURL string, fileName string, attachmentKeyBoard *ReplyKeyboardMarkup, caption string) (string, *courier.ChannelLog, error) {
fileData, err := downloadFileToBytes(mediaURL)
if err != nil {
return "", nil, err
}

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)

part, err := writer.CreateFormFile("document", filepath.Base(fileName))
if err != nil {
return "", nil, err
}

_, err = part.Write(fileData)
if err != nil {
return "", nil, err
}

_ = writer.WriteField("chat_id", msg.URN().Path())
if err != nil {
return "", nil, err
}

if attachmentKeyBoard == nil {
err = writer.WriteField("reply_markup", `{"remove_keyboard":true}`)
} else {
err = writer.WriteField("reply_markup", string(jsonx.MustMarshal(attachmentKeyBoard)))
}
if err != nil {
return "", nil, err
}

err = writer.WriteField("caption", caption)
if err != nil {
return "", nil, err
}

err = writer.Close()
if err != nil {
return "", nil, err
}

url := fmt.Sprintf(apiURL, token)
req, err := http.NewRequest("POST", url, body)
Expand Down Expand Up @@ -506,18 +503,15 @@ func sendUploadDocument(msg courier.Msg, token string, writer *multipart.Writer)
}

func downloadFileToBytes(fileURL string) ([]byte, error) {
// Fazer a requisição HTTP para o link
resp, err := http.Get(fileURL)
req, err := http.NewRequest("GET", fileURL, nil)
if err != nil {
return nil, fmt.Errorf("erro ao baixar o arquivo: %v", err)
return nil, err
}
defer resp.Body.Close()

// Ler o conteúdo do arquivo para um []byte
body, err := io.ReadAll(resp.Body)
resp, err := utils.MakeHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("erro ao ler o conteúdo do arquivo: %v", err)
return nil, fmt.Errorf("erro ao baixar o arquivo: %v", err)
}

return body, nil
return resp.Body, nil
}

0 comments on commit 6b1c33a

Please sign in to comment.