Feat(webhook-type): Initial webhook type support
This commit is contained in:
@@ -29,7 +29,8 @@ func WebhookEverythingRoutes(db *gorm.DB, telegramEnv *telegrampackage.Env, host
|
|||||||
r.HandleFunc("/routes/{routeID}", env.handleWebhook)
|
r.HandleFunc("/routes/{routeID}", env.handleWebhook)
|
||||||
|
|
||||||
// Telegram handlers
|
// Telegram handlers
|
||||||
env.TelegramEnv.AddTelegramHandlerFunc(env.registerWebhook)
|
env.TelegramEnv.AddTelegramHandlerFunc(env.registerWebhookRaw)
|
||||||
|
env.TelegramEnv.AddTelegramHandlerFunc(env.registerWebhookBody)
|
||||||
env.TelegramEnv.AddTelegramHandlerFunc(env.displayHelp)
|
env.TelegramEnv.AddTelegramHandlerFunc(env.displayHelp)
|
||||||
env.TelegramEnv.AddTelegramHandlerFunc(env.showRoutes)
|
env.TelegramEnv.AddTelegramHandlerFunc(env.showRoutes)
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
|
func (env *Env) registerWebhookRaw(shortCode string, text string) (bool, *string) {
|
||||||
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
||||||
if len(commandSplitted) > 0 && commandSplitted[0] == "/register-webhook" {
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/register_webhook_raw" {
|
||||||
newWebhookID := genWebhookCode(6)
|
newWebhookID := genWebhookCode(6)
|
||||||
baseURL, _ := url.Parse(env.HostURL)
|
baseURL, _ := url.Parse(env.HostURL)
|
||||||
baseURL.Path = path.Join(baseURL.Path, "webhook")
|
baseURL.Path = path.Join(baseURL.Path, "webhook")
|
||||||
@@ -22,6 +22,29 @@ func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
|
|||||||
var webhookRoute WebhookRoute
|
var webhookRoute WebhookRoute
|
||||||
webhookRoute.TelegramShortCode = shortCode
|
webhookRoute.TelegramShortCode = shortCode
|
||||||
webhookRoute.WebhookID = newWebhookID
|
webhookRoute.WebhookID = newWebhookID
|
||||||
|
webhookRoute.WebhookType = "raw"
|
||||||
|
env.DB.Create(&webhookRoute)
|
||||||
|
|
||||||
|
responseText := fmt.Sprintf("Your generated webhook URL is: %s", webhookURL)
|
||||||
|
return true, &responseText
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (env *Env) registerWebhookBody(shortCode string, text string) (bool, *string) {
|
||||||
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
||||||
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/register_webhook_body" {
|
||||||
|
newWebhookID := genWebhookCode(6)
|
||||||
|
baseURL, _ := url.Parse(env.HostURL)
|
||||||
|
baseURL.Path = path.Join(baseURL.Path, "webhook")
|
||||||
|
baseURL.Path = path.Join(baseURL.Path, "routes")
|
||||||
|
baseURL.Path = path.Join(baseURL.Path, newWebhookID)
|
||||||
|
webhookURL := baseURL.String()
|
||||||
|
|
||||||
|
var webhookRoute WebhookRoute
|
||||||
|
webhookRoute.TelegramShortCode = shortCode
|
||||||
|
webhookRoute.WebhookID = newWebhookID
|
||||||
|
webhookRoute.WebhookType = "body"
|
||||||
env.DB.Create(&webhookRoute)
|
env.DB.Create(&webhookRoute)
|
||||||
|
|
||||||
responseText := fmt.Sprintf("Your generated webhook URL is: %s", webhookURL)
|
responseText := fmt.Sprintf("Your generated webhook URL is: %s", webhookURL)
|
||||||
@@ -33,7 +56,7 @@ func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
|
|||||||
func (env *Env) displayHelp(shortCode string, text string) (bool, *string) {
|
func (env *Env) displayHelp(shortCode string, text string) (bool, *string) {
|
||||||
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
||||||
if len(commandSplitted) > 0 && commandSplitted[0] == "/help" {
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/help" {
|
||||||
responseText := "/register -> Register for a chat ID\n/register-webhook -> Register for a web hook\n/show -> Show existing webhooks"
|
responseText := "/register -> Register for a chat ID\n/register_webhook -> Register for a web hook\n/show -> Show existing webhooks"
|
||||||
return true, &responseText
|
return true, &responseText
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package webhookeverything
|
package webhookeverything
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
@@ -14,6 +15,21 @@ func (env *Env) forwardHookToTelegram(r *http.Request, routeID string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if routeResult.WebhookType == "body" {
|
||||||
|
return env.forwardHookToTelegramBody(r, routeID)
|
||||||
|
} else {
|
||||||
|
return env.forwardHookToTelegramRaw(r, routeID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (env *Env) forwardHookToTelegramRaw(r *http.Request, routeID string) error {
|
||||||
|
// Get Telegram code
|
||||||
|
var routeResult WebhookRoute
|
||||||
|
err := env.DB.Where(&WebhookRoute{WebhookID: routeID}).First(&routeResult).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Dump request as string
|
// Dump request as string
|
||||||
responseBytes, err := httputil.DumpRequest(r, true)
|
responseBytes, err := httputil.DumpRequest(r, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -28,3 +44,26 @@ func (env *Env) forwardHookToTelegram(r *http.Request, routeID string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *Env) forwardHookToTelegramBody(r *http.Request, routeID string) error {
|
||||||
|
// Get Telegram code
|
||||||
|
var routeResult WebhookRoute
|
||||||
|
err := env.DB.Where(&WebhookRoute{WebhookID: routeID}).First(&routeResult).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump request as string
|
||||||
|
responseBytes, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print on screenn
|
||||||
|
log.Println(string(responseBytes))
|
||||||
|
|
||||||
|
// Send telegram
|
||||||
|
env.TelegramEnv.TelegramSend(routeResult.TelegramShortCode, string(responseBytes))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type WebhookRoute struct {
|
|||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||||
WebhookID string `gorm:"index,unique"`
|
WebhookID string `gorm:"index,unique"`
|
||||||
|
WebhookType string `gorm:"index"`
|
||||||
TelegramShortCode string `gorm:"index"`
|
TelegramShortCode string `gorm:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user