use webhooks

This commit is contained in:
Dot-Rar 2020-03-05 14:56:00 +00:00
parent 714a064119
commit 07256467d3
6 changed files with 109 additions and 6 deletions

View File

@ -7,10 +7,13 @@ import (
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/GoPanel/utils/discord" "github.com/TicketsBot/GoPanel/utils/discord"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" "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/GoPanel/utils/discord/objects"
"github.com/TicketsBot/TicketsGo/database"
"github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"net/http"
"strconv" "strconv"
"sync" "sync"
) )
@ -166,10 +169,41 @@ func WebChatWs(ctx *gin.Context) {
content = content[0:1999] content = content[0:1999]
} }
endpoint := channel.CreateMessage(int(ticket.Channel)) // Preferably send via a webhook
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{ webhookChan := make(chan *string)
Content: content, go database.GetWebhookByUuid(ticket.Uuid, webhookChan)
}, nil, nil) 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)
}
} }
} }
} }

View 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
}
}

View File

@ -23,6 +23,7 @@ const (
BEARER AuthorizationType = "Bearer" BEARER AuthorizationType = "Bearer"
BOT AuthorizationType = "BOT" BOT AuthorizationType = "BOT"
NONE AuthorizationType = "NONE"
ApplicationJson ContentType = "application/json" ApplicationJson ContentType = "application/json"
ApplicationFormUrlEncoded ContentType = "application/x-www-form-urlencoded" ApplicationFormUrlEncoded ContentType = "application/x-www-form-urlencoded"
@ -36,7 +37,7 @@ type Endpoint struct {
Endpoint string 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 url := BASE_URL + e.Endpoint
// Create req // Create req
@ -118,7 +119,7 @@ func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, bod
} }
if rawResponse != nil { if rawResponse != nil {
*rawResponse<-string(content) *rawResponse<-res
} }
return json.Unmarshal(content, response) return json.Unmarshal(content, response)

View 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,
}
}

View File

@ -0,0 +1,7 @@
package objects
type AllowedMention struct {
Parse []AllowedMentionType
Roles []string
Users []string
}

View File

@ -0,0 +1,9 @@
package objects
type AllowedMentionType string
const(
EVERYONE AllowedMentionType = "everyone"
USERS AllowedMentionType = "users"
ROLES AllowedMentionType = "roles"
)