127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
package helper
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"git.maximotejeda.com/maximo/cedulados-bot/internal/application/domain"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"golang.org/x/text/runes"
|
|
"golang.org/x/text/transform"
|
|
"golang.org/x/text/unicode/norm"
|
|
)
|
|
|
|
// AsserCedulas
|
|
// check for cedula format
|
|
// 000-+ 0000000 +- 0
|
|
func NewCedula(ctx context.Context, ced string)(c *domain.Cedula, err error){
|
|
c = &domain.Cedula{}
|
|
if len(ced) <= 0 {
|
|
return nil, fmt.Errorf("cedula length err lenght:%d", len(ced))
|
|
}
|
|
// use Regexp to simplify cases
|
|
re := regexp.MustCompile(`(?P<munCed>\d{3})[\s-]+(?P<seqCed>\d{7})[\s-]+(?P<verCed>\d{1})$`)
|
|
result := re.FindStringSubmatch(ced)
|
|
if len(result) != 4 {
|
|
return nil, fmt.Errorf("Cedula format Err (%s) is not a good format", ced)
|
|
}
|
|
for i, name := range re.SubexpNames() {
|
|
if i != 0 && name != "" {
|
|
switch name {
|
|
case "munCed":
|
|
c.MunCed = result[i]
|
|
case "seqCed":
|
|
c.SeqCed = result[i]
|
|
case "verCed":
|
|
c.VerCed = result[i]
|
|
default:
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// NewMultiCedula
|
|
// create a list of Multiple Cedulas from a string
|
|
// separated by \n
|
|
func NewMultiCedula(ctx context.Context, ceds string) (cList []*domain.Cedula, err error){
|
|
splitedCed := strings.Split(ceds, "\n")
|
|
for _, ced := range splitedCed{
|
|
c, err := NewCedula(ctx, ced)
|
|
if err != nil {
|
|
fmt.Printf("error querying cedula: %s", err)
|
|
continue
|
|
}
|
|
cList = append(cList, c)
|
|
}
|
|
return cList, err
|
|
}
|
|
|
|
// RemoveAccent
|
|
// helps normalize names in db
|
|
// https://stackoverflow.com/questions/24588295/go-removing-accents-from-strings
|
|
func RemoveAccent(str string) string {
|
|
if str == "" {
|
|
return ""
|
|
}
|
|
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
|
s, _, _ := transform.String(t, str)
|
|
|
|
return s
|
|
}
|
|
|
|
|
|
// RemoveSpaces
|
|
func RemoveSpaces(text string) (res string) {
|
|
re := regexp.MustCompile(`[\s]+`)
|
|
|
|
res = re.ReplaceAllString(text, " ")
|
|
return
|
|
}
|
|
|
|
// MessageChecker
|
|
// Check if message start with a number or letter
|
|
func MessageChecker(text string) string {
|
|
// Check is text start with number
|
|
checkSpace := regexp.MustCompile(`^[\s]+.*`)
|
|
checkNumber := regexp.MustCompile(`^[\d]+.*`)
|
|
checkWord := regexp.MustCompile(`^[a-zA-Z% ]+.*`)
|
|
|
|
if checkNumber.MatchString(text) {
|
|
return "digit"
|
|
} else if checkWord.MatchString(text) {
|
|
return "word"
|
|
} else if checkSpace.MatchString(text) {
|
|
t := strings.TrimSpace(text)
|
|
return MessageChecker(t)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// CreateKeyboard
|
|
// create keybowrds of two rows of any map[string]string input
|
|
func CreateKeyboard(data map[string]string) tgbotapi.InlineKeyboardMarkup {
|
|
// hardcoded models
|
|
keyboard := tgbotapi.NewInlineKeyboardMarkup()
|
|
// subbuttons := []tgbot.InlineKeyboardButton{}
|
|
rows := tgbotapi.NewInlineKeyboardRow()
|
|
counter := 0
|
|
for key, val := range data {
|
|
|
|
if counter != 0 && counter%3 == 0 {
|
|
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rows)
|
|
rows = tgbotapi.NewInlineKeyboardRow()
|
|
}
|
|
rows = append(rows, tgbotapi.NewInlineKeyboardButtonData(key, val))
|
|
if counter >= len(data)-1 {
|
|
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rows)
|
|
}
|
|
counter++
|
|
}
|
|
return keyboard
|
|
}
|