Browse Source

mvdr: More cleanup by splitting main

master
Remi Reuvekamp 3 years ago
parent
commit
2b4a93b08b
  1. 88
      mannenvanderadio/main.go

88
mannenvanderadio/main.go

@ -94,7 +94,7 @@ func main() {
bot.Debug = true
fmt.Println("Authorized on account %s", bot.Self.UserName)
fmt.Println("Authorized on account:", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@ -106,54 +106,68 @@ func main() {
continue
}
sanitizedMsg := strings.ToLower(
rmSpecialCharsRegex.ReplaceAllString(
update.Message.Text,
""))
handleMsg(bot, update)
}
}
msgParts := strings.Split(sanitizedMsg, " ")
func handleMsg(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
sanitizedMsg := strings.ToLower(
rmSpecialCharsRegex.ReplaceAllString(
update.Message.Text,
""))
var replyMsgs []string
outerLoop:
for _, p := range msgParts {
for _, reply := range replies {
for _, replyTo := range reply.triggerSanitizedWords {
if p != replyTo {
continue
}
msgParts := strings.Split(sanitizedMsg, " ")
replyMsgs = reply.replies
var replyMsgs []string
break outerLoop
}
}
for _, r := range replies {
replyMsgs = r.checkSanitizedWords(msgParts)
if len(replyMsgs) == 0 {
fmt.Println("Check die sanitized sentence")
replyMsgs = r.checkSanitizedSentences(sanitizedMsg)
}
if replies != nil {
outerLoop2:
for _, reply := range replies {
for _, replyTo := range reply.triggerSanitizedSentences {
if sanitizedMsg != replyTo {
continue
}
fmt.Println("replyMsgs:", replyMsgs)
if len(replyMsgs) > 0 {
sendRandomReply(bot, update.Message, replyMsgs)
}
}
}
replyMsgs = reply.replies
func (r autoReply) checkSanitizedWords(words []string) []string {
return r.checkWords(words, r.triggerSanitizedWords)
}
break outerLoop2
}
func (r autoReply) checkWords(words []string, triggers []string) []string {
for _, replyTo := range triggers {
for _, w := range words {
if w != replyTo {
continue
}
}
var replyMsg string
if len(replyMsgs) > 0 {
replyMsg = replyMsgs[rand.Intn(len(replyMsgs))]
return r.replies
}
}
if replyMsg != "" {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, replyMsg)
msg.ReplyToMessageID = update.Message.MessageID
return nil
bot.Send(msg)
}
}
func (r autoReply) checkSanitizedSentences(sentence string) []string {
return r.checkWords([]string{sentence}, r.triggerSanitizedSentences)
}
func sendRandomReply(bot *tgbotapi.BotAPI, message *tgbotapi.Message, replies []string) {
var reply string
if len(replies) > 0 {
reply = replies[rand.Intn(len(replies))]
}
if reply != "" {
msg := tgbotapi.NewMessage(message.Chat.ID, reply)
msg.ReplyToMessageID = message.MessageID
bot.Send(msg)
}
}

Loading…
Cancel
Save