Skip to content

Instantly share code, notes, and snippets.

@Karitham
Created November 13, 2020 12:26
Show Gist options
  • Save Karitham/a21e8546fa1041b24b6836d0cd5f4c11 to your computer and use it in GitHub Desktop.
Save Karitham/a21e8546fa1041b24b6836d0cd5f4c11 to your computer and use it in GitHub Desktop.
small go code for using webhooks
package webhook
import (
"bytes"
"encoding/json"
"net/http"
)
// Webhook is the toplevel webhook object
type Webhook struct {
Username string `json:"username"`
AvatarURL string `json:"avatar_url"`
Content string `json:"content"`
Embeds []Embed `json:"embeds"`
}
// Embed is the embed object
type Embed struct {
Author Author `json:"author"`
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description"`
Color int64 `json:"color"`
Fields []Field `json:"fields"`
Thumbnail Image `json:"thumbnail"`
Image Image `json:"image"`
Footer Footer `json:"footer"`
}
// Author is the author object
type Author struct {
Name string `json:"name"`
URL string `json:"url"`
IconURL string `json:"icon_url"`
}
// Field is the field object inside an embed
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}
// Footer is the footer of the embed
type Footer struct {
Text string `json:"text"`
IconURL string `json:"icon_url"`
}
// Image is an image possibly contained inside the embed
type Image struct {
URL string `json:"url"`
}
// New returns a new webhook
func New(Name string) *Webhook {
return &Webhook{
Username: Name,
Embeds: []Embed{
{},
},
}
}
// Run sends the payload
func (w *Webhook) Run(URL string) (resp *http.Response, err error) {
p, err := json.Marshal(*w)
if err != nil {
return nil, err
}
return http.Post(URL, "application/json", bytes.NewBuffer(p))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment