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.
40 lines
1.1 KiB
40 lines
1.1 KiB
package webhookeverything
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net/url"
|
|
"path"
|
|
|
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
|
|
)
|
|
|
|
func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
|
|
commandSplitted := telegrampackage.ParseTelegramBotCommand(text)
|
|
if len(commandSplitted) > 0 && commandSplitted[0] == "/register-webhook" {
|
|
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
|
|
env.DB.Create(&webhookRoute)
|
|
|
|
responseText := fmt.Sprintf("Your generated webhook URL is: %s", webhookURL)
|
|
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)
|
|
}
|