@ -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_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 true , & responseText
}
}
return false , nil
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 , "routes" )
baseURL . Path = path . Join ( baseURL . Path , curr . WebhookID )
baseURL . Path = path . Join ( baseURL . Path , curr . WebhookID )
webhookURL := baseURL . String ( )
webhookURL := baseURL . String ( )
responseText += fmt . Sprintf ( "%s\n" , webhookURL )
responseText += fmt . Sprintf ( "%s (%s) \n" , webhookURL , curr . WebhookType )
}
}
return true , & responseText
return true , & responseText
}
}