-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update discord-webhook-publisher
- Loading branch information
1 parent
4a1d46f
commit e638984
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module handler/function | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package function | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
webhookUrl = "https://discord.com/api/webhooks/1089563947423764550/LuUfqOQMwscSZlS9gOboRraau0J-rJw9YCMKJ03RA2P5q1GKOGQUUB8Ezqlr1G4T3EIz" | ||
) | ||
|
||
// Data Transfer Object for the request | ||
type PublishMessageRequest struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
// If the incoming request is not POST, | ||
// we return a HTTP 405 Method Not Allowed | ||
if r.Method != http.MethodPost { | ||
http.Error(w, "This function accept only POST requests.", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
// Decode the request body, and if an error occurs, | ||
// return a HTTP 400 Bad Request error | ||
dto := &PublishMessageRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(dto); err != nil { | ||
http.Error(w, fmt.Sprintf("Unable to decode request body: %v", err), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Create a valid JSON request body for the webhook. | ||
// https://discord.com/developers/docs/resources/webhook#execute-webhook | ||
data, _ := json.Marshal(map[string]interface{}{ | ||
"user": "Morty", | ||
"content": dto.Message, | ||
}) | ||
|
||
// Create the request and execute it | ||
req, _ := http.NewRequest(http.MethodPost, webhookUrl, bytes.NewReader(data)) | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
if _, err := http.DefaultClient.Do(req); err != nil { | ||
http.Error(w, fmt.Sprintf("failed to send data to webhook: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Send the HTTP 204 No Content | ||
w.WriteHeader(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name: discord-webhook-publisher | ||
runtime: go-1.19 |