Files
webhook-everything/backend/internal/webhookeverything/routegencontrollertelegram.go
Samuel Pua cd2faaa02e
Some checks failed
Webhook-Everything/Webhook-Everything/pipeline/head There was a failure building this commit
Fix(Webhook): HostURL for generation
2022-05-29 00:22:21 +08:00

41 lines
1.1 KiB
Go

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)
}