use webhooks
This commit is contained in:
parent
714a064119
commit
07256467d3
@ -7,10 +7,13 @@ import (
|
||||
"github.com/TicketsBot/GoPanel/utils"
|
||||
"github.com/TicketsBot/GoPanel/utils/discord"
|
||||
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel"
|
||||
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/webhooks"
|
||||
"github.com/TicketsBot/GoPanel/utils/discord/objects"
|
||||
"github.com/TicketsBot/TicketsGo/database"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
@ -166,10 +169,41 @@ func WebChatWs(ctx *gin.Context) {
|
||||
content = content[0:1999]
|
||||
}
|
||||
|
||||
endpoint := channel.CreateMessage(int(ticket.Channel))
|
||||
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{
|
||||
Content: content,
|
||||
}, nil, nil)
|
||||
// Preferably send via a webhook
|
||||
webhookChan := make(chan *string)
|
||||
go database.GetWebhookByUuid(ticket.Uuid, webhookChan)
|
||||
webhook := <-webhookChan
|
||||
|
||||
success := false
|
||||
if webhook != nil {
|
||||
endpoint := webhooks.ExecuteWebhook(*webhook)
|
||||
|
||||
resChan := make(chan *http.Response)
|
||||
err = endpoint.Request(store, &contentType, webhooks.ExecuteWebhookBody{
|
||||
Content: content,
|
||||
Username: store.Get("name").(string),
|
||||
AvatarUrl: store.Get("avatar").(string),
|
||||
AllowedMentions: objects.AllowedMention{
|
||||
Parse: make([]objects.AllowedMentionType, 0),
|
||||
Roles: make([]string, 0),
|
||||
Users: make([]string, 0),
|
||||
},
|
||||
}, nil, &resChan)
|
||||
res := <-resChan
|
||||
|
||||
if res.StatusCode == 200 {
|
||||
success = true
|
||||
} else {
|
||||
go database.DeleteWebhookByUuid(ticket.Uuid)
|
||||
}
|
||||
}
|
||||
|
||||
if !success {
|
||||
endpoint := channel.CreateMessage(int(ticket.Channel))
|
||||
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{
|
||||
Content: content,
|
||||
}, nil, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
31
database/table/ticketwebhook.go
Normal file
31
database/table/ticketwebhook.go
Normal file
@ -0,0 +1,31 @@
|
||||
package table
|
||||
|
||||
import "github.com/TicketsBot/GoPanel/database"
|
||||
|
||||
type TicketWebhook struct {
|
||||
Uuid string `gorm:"column:UUID;type:varchar(36);unique;primary_key"`
|
||||
WebhookUrl string `gorm:"column:CDNURL;type:varchar(200)"`
|
||||
}
|
||||
|
||||
func (TicketWebhook) TableName() string {
|
||||
return "webhooks"
|
||||
}
|
||||
|
||||
func (w *TicketWebhook) AddWebhook() {
|
||||
database.Database.Create(w)
|
||||
}
|
||||
|
||||
func DeleteWebhookByUuid(uuid string) {
|
||||
database.Database.Where(TicketWebhook{Uuid: uuid}).Delete(TicketWebhook{})
|
||||
}
|
||||
|
||||
func GetWebhookByUuid(uuid string, res chan *string) {
|
||||
var row TicketWebhook
|
||||
database.Database.Where(TicketWebhook{Uuid: uuid}).Take(&row)
|
||||
|
||||
if row.WebhookUrl == "" {
|
||||
res <- nil
|
||||
} else {
|
||||
res <- &row.WebhookUrl
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ const (
|
||||
|
||||
BEARER AuthorizationType = "Bearer"
|
||||
BOT AuthorizationType = "BOT"
|
||||
NONE AuthorizationType = "NONE"
|
||||
|
||||
ApplicationJson ContentType = "application/json"
|
||||
ApplicationFormUrlEncoded ContentType = "application/x-www-form-urlencoded"
|
||||
@ -36,7 +37,7 @@ type Endpoint struct {
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}, rawResponse *chan string) error {
|
||||
func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}, rawResponse *chan *http.Response) error {
|
||||
url := BASE_URL + e.Endpoint
|
||||
|
||||
// Create req
|
||||
@ -118,7 +119,7 @@ func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, bod
|
||||
}
|
||||
|
||||
if rawResponse != nil {
|
||||
*rawResponse<-string(content)
|
||||
*rawResponse<-res
|
||||
}
|
||||
|
||||
return json.Unmarshal(content, response)
|
||||
|
21
utils/discord/endpoints/webhooks/executewebhook.go
Normal file
21
utils/discord/endpoints/webhooks/executewebhook.go
Normal file
@ -0,0 +1,21 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"github.com/TicketsBot/GoPanel/utils/discord"
|
||||
"github.com/TicketsBot/GoPanel/utils/discord/objects"
|
||||
)
|
||||
|
||||
type ExecuteWebhookBody struct {
|
||||
Content string `json:"content"`
|
||||
Username string `json:"username"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
AllowedMentions objects.AllowedMention `json:"allowed_mentions"`
|
||||
}
|
||||
|
||||
func ExecuteWebhook(url string) discord.Endpoint {
|
||||
return discord.Endpoint{
|
||||
RequestType: discord.POST,
|
||||
AuthorizationType: discord.NONE,
|
||||
Endpoint: url,
|
||||
}
|
||||
}
|
7
utils/discord/objects/allowedmention.go
Normal file
7
utils/discord/objects/allowedmention.go
Normal file
@ -0,0 +1,7 @@
|
||||
package objects
|
||||
|
||||
type AllowedMention struct {
|
||||
Parse []AllowedMentionType
|
||||
Roles []string
|
||||
Users []string
|
||||
}
|
9
utils/discord/objects/allowedmentiontype.go
Normal file
9
utils/discord/objects/allowedmentiontype.go
Normal file
@ -0,0 +1,9 @@
|
||||
package objects
|
||||
|
||||
type AllowedMentionType string
|
||||
|
||||
const(
|
||||
EVERYONE AllowedMentionType = "everyone"
|
||||
USERS AllowedMentionType = "users"
|
||||
ROLES AllowedMentionType = "roles"
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user