Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: golang ci/cd

on:
push:
branches:
- main
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.*'

- name: Install dependencies
run: |
go version

- name: Run build
run: go build .

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ go get github.com/gtuk/discordwebhook
Below is the most basic example on how to send a message.
For a more advanced message structure see the structs in types.go and https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html

```
```go
package main

import "github.com/gtuk/discordwebhook"
Expand All @@ -20,6 +20,7 @@ func main() {
var username = "BotUser"
var content = "This is a test message"
var url = "https://discord.com/api/webhooks/..."
var filePath = "/path/to/file"

message := discordwebhook.Message{
Username: &username,
Expand All @@ -30,6 +31,11 @@ func main() {
if err != nil {
log.Fatal(err)
}

err = discordwebhook.SendFile(url, filePath)
if err != nil {
log.Fatal(err)
}
}
```

Expand Down
42 changes: 40 additions & 2 deletions discordwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"strings"
)

func SendMessage(url string, message Message) error {
Expand All @@ -24,7 +28,7 @@ func SendMessage(url string, message Message) error {
if resp.StatusCode != 200 && resp.StatusCode != 204 {
defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand All @@ -34,3 +38,37 @@ func SendMessage(url string, message Message) error {

return nil
}

func SendFile(url string, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
log.Fatal("Err: Could not open file")
return err
}
defer file.Close()

body := new(bytes.Buffer)

writer := multipart.NewWriter(body)

pathSplit := strings.Split(filePath, "/")
part, err := writer.CreateFormFile("file", pathSplit[len(pathSplit)-1])
if err != nil {
log.Fatal("Err: Could not create form file")
return err
}

_, err = io.Copy(part, file)
if err != nil {
log.Fatal("Err: Could not copy file content to form file field")
return err
}

resp, err := http.Post(url, writer.FormDataContentType(), body)
if err != nil {
log.Fatal("Err: Post request failed")
return err
}
defer resp.Body.Close()
return nil
}
6 changes: 6 additions & 0 deletions examples/basic_example/basic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func main() {
var username = "BotUser"
var content = "This is a test message"
var url = "https://discord.com/api/webhooks/..."
var filePath = "/path/to/file"

message := discordwebhook.Message{
Username: &username,
Expand All @@ -20,5 +21,10 @@ func main() {
if err != nil {
log.Fatal(err)
}

err = discordwebhook.SendFile(url, filePath)
if err != nil {
log.Fatal(err)
}
}