2017-10-04 17:56:18 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
import (
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// stdlib
|
|
|
|
|
"log"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
// 3rd party
|
2017-10-07 19:58:14 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// Router is a function-handling struct for router
|
2017-10-18 07:03:34 +04:00
|
|
|
|
type Router struct{}
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RouteRequest decides, what to do with user input
|
2017-10-04 17:56:18 +04:00
|
|
|
|
func (r *Router) RouteRequest(update tgbotapi.Update) string {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
text := update.Message.Text
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
playerRaw, ok := c.Getters.GetOrCreatePlayer(update.Message.From.ID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if !ok {
|
|
|
|
|
// Silently fail
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-21 14:14:46 +04:00
|
|
|
|
chatRaw, ok := c.Getters.GetOrCreateChat(&update)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "fail"
|
2017-10-18 09:39:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 14:14:46 +04:00
|
|
|
|
log.Printf("Received message from chat ")
|
|
|
|
|
log.Println(chatRaw.TelegramID)
|
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Regular expressions
|
|
|
|
|
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
|
|
|
|
|
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
|
|
|
|
|
var blMsg = regexp.MustCompile("(\\s|^)(Б|б)(Л|л)(Я|я)(Т|т|Д|д)")
|
|
|
|
|
var ebMsg = regexp.MustCompile("(\\s|^|ЗА|За|зА|за)(Е|е|Ё|ё)(Б|б)(\\s|Л|л|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)")
|
|
|
|
|
var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)")
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Commands
|
|
|
|
|
var helpMsg = regexp.MustCompile("/help\\z")
|
|
|
|
|
var helloMsg = regexp.MustCompile("/start\\z")
|
|
|
|
|
var pokedexMsg = regexp.MustCompile("/pokede(x|ks)\\d?\\z")
|
|
|
|
|
var pokememeInfoMsg = regexp.MustCompile("/pk(\\d+)")
|
|
|
|
|
var meMsg = regexp.MustCompile("/me\\z")
|
|
|
|
|
var bestMsg = regexp.MustCompile("/best\\z")
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Forwards
|
|
|
|
|
var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)")
|
|
|
|
|
var profileMsg = regexp.MustCompile(`(Онлайн: )(\d+)\n(Турнир через)(.+)\n\n(.*)\n(Элементы)(.+)\n(.*)\n\n(.+)(Уровень)(.+)\n`)
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if update.Message.ForwardFrom != nil {
|
|
|
|
|
if update.Message.ForwardFrom.ID != 360402625 {
|
|
|
|
|
log.Printf("Forward from another user or bot. Ignoring")
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Forward from PokememBro bot! Processing...")
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if playerRaw.ID != 0 {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
switch {
|
|
|
|
|
case pokememeMsg.MatchString(text):
|
|
|
|
|
log.Printf("Pokememe posted!")
|
2017-10-18 09:39:50 +04:00
|
|
|
|
status := c.Parsers.ParsePokememe(text, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
switch status {
|
|
|
|
|
case "ok":
|
|
|
|
|
c.Talkers.PokememeAddSuccessMessage(update)
|
|
|
|
|
case "dup":
|
|
|
|
|
c.Talkers.PokememeAddDuplicateMessage(update)
|
|
|
|
|
case "fail":
|
|
|
|
|
c.Talkers.PokememeAddFailureMessage(update)
|
|
|
|
|
}
|
|
|
|
|
case profileMsg.MatchString(text):
|
|
|
|
|
log.Printf("Profile posted!")
|
2017-10-18 09:39:50 +04:00
|
|
|
|
status := c.Parsers.ParseProfile(update, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
switch status {
|
|
|
|
|
case "ok":
|
|
|
|
|
c.Talkers.ProfileAddSuccessMessage(update)
|
|
|
|
|
case "fail":
|
|
|
|
|
c.Talkers.ProfileAddFailureMessage(update)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
log.Printf(text)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.Talkers.AnyMessageUnauthorized(update)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Direct messages from user
|
|
|
|
|
switch {
|
|
|
|
|
case helloMsg.MatchString(text):
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if playerRaw.ID != 0 {
|
|
|
|
|
c.Talkers.HelloMessageAuthorized(update, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
} else {
|
|
|
|
|
c.Talkers.HelloMessageUnauthorized(update)
|
|
|
|
|
}
|
|
|
|
|
// Help
|
|
|
|
|
case helpMsg.MatchString(text):
|
|
|
|
|
c.Talkers.HelpMessage(update)
|
|
|
|
|
// Pokememes info
|
|
|
|
|
case pokedexMsg.MatchString(text):
|
|
|
|
|
if strings.HasSuffix(text, "1") {
|
|
|
|
|
c.Talkers.PokememesList(update, 1)
|
|
|
|
|
} else if strings.HasSuffix(text, "2") {
|
|
|
|
|
c.Talkers.PokememesList(update, 2)
|
|
|
|
|
} else if strings.HasSuffix(text, "3") {
|
|
|
|
|
c.Talkers.PokememesList(update, 3)
|
|
|
|
|
} else if strings.HasSuffix(text, "4") {
|
|
|
|
|
c.Talkers.PokememesList(update, 4)
|
|
|
|
|
} else if strings.HasSuffix(text, "5") {
|
|
|
|
|
c.Talkers.PokememesList(update, 5)
|
|
|
|
|
} else {
|
|
|
|
|
c.Talkers.PokememesList(update, 1)
|
|
|
|
|
}
|
|
|
|
|
case pokememeInfoMsg.MatchString(text):
|
2017-10-18 09:39:50 +04:00
|
|
|
|
c.Talkers.PokememeInfo(update, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Profile info
|
|
|
|
|
case meMsg.MatchString(text):
|
2017-10-18 09:39:50 +04:00
|
|
|
|
if playerRaw.ID != 0 {
|
|
|
|
|
c.Talkers.ProfileMessage(update, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
} else {
|
|
|
|
|
c.Talkers.AnyMessageUnauthorized(update)
|
|
|
|
|
}
|
|
|
|
|
// Suggestions
|
|
|
|
|
case bestMsg.MatchString(text):
|
2017-10-18 09:39:50 +04:00
|
|
|
|
c.Talkers.BestPokememesList(update, playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Easter eggs
|
|
|
|
|
case huMsg.MatchString(text):
|
|
|
|
|
c.Talkers.MatMessage(update)
|
|
|
|
|
case blMsg.MatchString(text):
|
|
|
|
|
c.Talkers.MatMessage(update)
|
|
|
|
|
case ebMsg.MatchString(text):
|
|
|
|
|
c.Talkers.MatMessage(update)
|
|
|
|
|
case piMsg.MatchString(text):
|
|
|
|
|
c.Talkers.MatMessage(update)
|
|
|
|
|
case durakMsg.MatchString(text):
|
|
|
|
|
c.Talkers.DurakMessage(update)
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("User posted unknown command.")
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return "ok"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|