Some checks failed
dev test / test (push) Successful in 30s
dev test / vulnCheck (push) Successful in 36s
dev test / Ci-Lint (push) Failing after 16s
${{ github.actor }} executed Build Push Prod / build (push) Failing after 38s
${{ github.actor }} executed Build Push Prod / deploy (push) Has been skipped
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package chat
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"unicode"
|
|
|
|
tgb "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"
|
|
)
|
|
|
|
var ChatPool *sync.Pool
|
|
|
|
type ChatObj struct {
|
|
update *tgb.Update
|
|
bot *tgb.BotAPI
|
|
}
|
|
|
|
func NewChatObj(bot *tgb.BotAPI, updt *tgb.Update) (chat *ChatObj) {
|
|
if ChatPool == nil {
|
|
ChatPool = &sync.Pool{
|
|
New: func() any { return &ChatObj{} },
|
|
}
|
|
for i := 0; i < 20; i++ {
|
|
ChatPool.Put(ChatPool.New())
|
|
}
|
|
} else {
|
|
fmt.Println("alredy populated")
|
|
}
|
|
|
|
chat = ChatPool.Get().(*ChatObj)
|
|
chat.update = updt
|
|
chat.bot = bot
|
|
return chat
|
|
}
|
|
|
|
func EmptyChat(chat *ChatObj) {
|
|
chat.update = nil
|
|
ChatPool.Put(chat)
|
|
}
|
|
|
|
func HandleChat(bot *tgb.BotAPI, updt *tgb.Update) {
|
|
chat := NewChatObj(bot, updt)
|
|
defer EmptyChat(chat)
|
|
text := NormalizeText(updt.Message.Text)
|
|
textList := strings.Split(text, " ")
|
|
msg := tgb.NewMessage(updt.Message.Chat.ID, strings.Join(textList, " "))
|
|
msg.ReplyToMessageID = updt.Message.MessageID
|
|
bot.Send(msg)
|
|
|
|
}
|
|
|
|
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 ""
|
|
}
|
|
|
|
// NormalizeText
|
|
// remove foreign accent
|
|
func NormalizeText(text string) string {
|
|
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
|
result, _, _ := transform.String(t, text)
|
|
return result
|
|
}
|
|
|
|
func RemoveSpaces(text string) (res string) {
|
|
re := regexp.MustCompile(`[\s]+`)
|
|
|
|
res = re.ReplaceAllString(text, " ")
|
|
return
|
|
}
|