Feat(telegram): Added new controller to handle help and show
All checks were successful
Webhook-Everything/Webhook-Everything/pipeline/head This commit looks good

This commit is contained in:
2022-09-10 00:40:10 +08:00
parent a54bdd3f90
commit b3644f05e6
7 changed files with 36 additions and 4 deletions

View File

@@ -30,6 +30,8 @@ func WebhookEverythingRoutes(db *gorm.DB, telegramEnv *telegrampackage.Env, host
// Telegram handlers
env.TelegramEnv.AddTelegramHandlerFunc(env.registerWebhook)
env.TelegramEnv.AddTelegramHandlerFunc(env.displayHelp)
env.TelegramEnv.AddTelegramHandlerFunc(env.showRoutes)
return r
}

View File

@@ -30,6 +30,34 @@ func (env *Env) registerWebhook(shortCode string, text string) (bool, *string) {
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 -> Register for a web hook\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{WebhookID: 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\n", webhookURL)
}
return true, &responseText
}
return false, nil
}
func genWebhookCode(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
runeCode := make([]rune, n)