You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.2 KiB
91 lines
3.2 KiB
package webhookeverything
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net/url"
|
|
"path"
|
|
|
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
|
|
)
|
|
|
|
func (env *Env) registerWebhookRaw(shortCode string, text string) (bool, *string) {
|
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/register_webhook_raw" {
|
|
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 = "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)
|
|
return true, &responseText
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
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_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
|
|
}
|
|
|
|
func (env *Env) showRoutes(shortCode string, text string) (bool, *string) {
|
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/show" {
|
|
var results []WebhookRoute
|
|
env.DB.Where(&WebhookRoute{TelegramShortCode: shortCode}).Find(&results)
|
|
responseText := "The following webhook URLs were previously registered:"
|
|
for _, curr := range results {
|
|
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, curr.WebhookID)
|
|
webhookURL := baseURL.String()
|
|
responseText += fmt.Sprintf("%s (%s)\n", webhookURL, curr.WebhookType)
|
|
}
|
|
return true, &responseText
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func genWebhookCode(n int) string {
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
|
|
runeCode := make([]rune, n)
|
|
for i := range runeCode {
|
|
runeCode[i] = letters[rand.Intn(len(letters))]
|
|
}
|
|
return string(runeCode)
|
|
}
|