Compare commits
2 Commits
ad69c0470d
...
2a7cad131e
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a7cad131e | |||
| 0d38e24b3e |
@@ -29,7 +29,8 @@ func WebhookEverythingRoutes(db *gorm.DB, telegramEnv *telegrampackage.Env, host
|
||||
r.HandleFunc("/routes/{routeID}", env.handleWebhook)
|
||||
|
||||
// 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.showRoutes)
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"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)
|
||||
if len(commandSplitted) > 0 && commandSplitted[0] == "/register-webhook" {
|
||||
if len(commandSplitted) > 0 && commandSplitted[0] == "/register_webhook_raw" {
|
||||
newWebhookID := genWebhookCode(6)
|
||||
baseURL, _ := url.Parse(env.HostURL)
|
||||
baseURL.Path = path.Join(baseURL.Path, "webhook")
|
||||
@@ -22,6 +22,29 @@ func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
|
||||
var webhookRoute WebhookRoute
|
||||
webhookRoute.TelegramShortCode = shortCode
|
||||
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)
|
||||
|
||||
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) {
|
||||
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
||||
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_raw -> Register for a web hook for raw requests\n/register_webhook_body -> Register for a web hook for requests body\n/show -> Show existing webhooks"
|
||||
return true, &responseText
|
||||
}
|
||||
return false, nil
|
||||
@@ -51,7 +74,7 @@ func (env *Env) showRoutes(shortCode string, text string) (bool, *string) {
|
||||
baseURL.Path = path.Join(baseURL.Path, "routes")
|
||||
baseURL.Path = path.Join(baseURL.Path, curr.WebhookID)
|
||||
webhookURL := baseURL.String()
|
||||
responseText += fmt.Sprintf("%s\n", webhookURL)
|
||||
responseText += fmt.Sprintf("%s (%s)\n", webhookURL, curr.WebhookType)
|
||||
}
|
||||
return true, &responseText
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package webhookeverything
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
@@ -14,6 +15,21 @@ func (env *Env) forwardHookToTelegram(r *http.Request, routeID string) error {
|
||||
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
|
||||
responseBytes, err := httputil.DumpRequest(r, true)
|
||||
if err != nil {
|
||||
@@ -28,3 +44,26 @@ func (env *Env) forwardHookToTelegram(r *http.Request, routeID string) error {
|
||||
|
||||
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
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
WebhookID string `gorm:"index,unique"`
|
||||
WebhookType string `gorm:"index"`
|
||||
TelegramShortCode string `gorm:"index"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user