diff options
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index f1f7f52..1248a54 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/pkg/errors" "io" + "io/ioutil" + "net/http" "os" osUser "os/user" "path/filepath" @@ -832,10 +834,66 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str } // attach a file - var file *client.InputFileRemote + var file *client.InputFileLocal if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { - file = &client.InputFileRemote{ - Id: text, + response, err := http.Get(text) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()), + c.xmpp, + ) + return nil + } + if response != nil && response.Body != nil { + defer response.Body.Close() + + if response.StatusCode != 200 { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Received status code %v", response.StatusCode), + c.xmpp, + ) + return nil + } + + tempDir, err := ioutil.TempDir("", "telegabber-*") + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()), + c.xmpp, + ) + return nil + } + tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text))) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to create a temporary file: %s", err.Error()), + c.xmpp, + ) + return nil + } + + _, err = io.Copy(tempFile, response.Body) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to write a temporary file: %s", err.Error()), + c.xmpp, + ) + return nil + } + + file = &client.InputFileLocal{ + Path: tempFile.Name(), + } } } @@ -844,6 +902,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str newlinePos := strings.Index(text, newlineChar) if newlinePos != -1 { text = text[newlinePos+1:] + } else { + text = "" } } |