2017-10-04 17:56:18 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
// stdlib
|
2017-10-06 02:56:06 +04:00
|
|
|
|
"fmt"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
"log"
|
|
|
|
|
"regexp"
|
2017-10-07 02:23:25 +04:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
// 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
|
|
|
|
// local
|
2017-10-06 02:56:06 +04:00
|
|
|
|
"../dbmappings"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Router struct {}
|
|
|
|
|
|
|
|
|
|
// This function will route requests to appropriative modules
|
|
|
|
|
// It will return "ok" or "fail"
|
|
|
|
|
// If command doesn't exist, it's "fail"
|
|
|
|
|
func (r *Router) RouteRequest(update tgbotapi.Update) string {
|
|
|
|
|
text := update.Message.Text
|
2017-10-06 02:56:06 +04:00
|
|
|
|
user_id := update.Message.From.ID
|
|
|
|
|
|
|
|
|
|
player_raw := dbmappings.Players{}
|
|
|
|
|
err := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), user_id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Message user not found in database.")
|
|
|
|
|
log.Printf(err.Error())
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
|
|
|
|
// Create "nobody" user
|
|
|
|
|
player_raw.Telegram_id = user_id
|
|
|
|
|
player_raw.League_id = 0
|
|
|
|
|
player_raw.Squad_id = 0
|
|
|
|
|
player_raw.Status = "nobody"
|
|
|
|
|
player_raw.Created_at = time.Now().UTC()
|
|
|
|
|
player_raw.Updated_at = time.Now().UTC()
|
|
|
|
|
_, erradd := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &player_raw)
|
|
|
|
|
if erradd != nil {
|
|
|
|
|
log.Printf(erradd.Error())
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Message user found in database.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(player_raw)
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
|
|
|
|
// Regular expressions
|
|
|
|
|
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
|
|
|
|
|
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
|
2017-10-07 18:40:40 +04:00
|
|
|
|
var blMsg = regexp.MustCompile("(\\s|^)(Б|б)(Л|л)(Я|я)(Т|т|Д|д)")
|
|
|
|
|
var ebMsg = regexp.MustCompile("(\\s|^|ЗА|За|зА|за)(Е|е|Ё|ё)(Б|б)(\\s|Л|л|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)")
|
2017-10-04 17:56:18 +04:00
|
|
|
|
var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)")
|
2017-10-07 02:23:25 +04:00
|
|
|
|
|
|
|
|
|
// Commands
|
2017-10-04 17:56:18 +04:00
|
|
|
|
var helpMsg = regexp.MustCompile("/help\\z")
|
2017-10-06 02:56:06 +04:00
|
|
|
|
var helloMsg = regexp.MustCompile("/start\\z")
|
2017-10-07 02:23:25 +04:00
|
|
|
|
var pokedexMsg = regexp.MustCompile("/pokede(x|ks)\\d?\\z")
|
2017-10-07 19:58:14 +04:00
|
|
|
|
var pokememeInfoMsg = regexp.MustCompile("/pk(\\d+)")
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-07 02:23:25 +04:00
|
|
|
|
// Forwards
|
2017-10-06 02:56:06 +04:00
|
|
|
|
var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)")
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-06 02:56:06 +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...")
|
|
|
|
|
if pokememeMsg.MatchString(text) {
|
|
|
|
|
if player_raw.Id != 0 {
|
|
|
|
|
log.Printf("Pokememe posted!")
|
2017-10-07 02:23:25 +04:00
|
|
|
|
status := c.Parsers.ParsePokememe(text, player_raw)
|
|
|
|
|
switch status {
|
|
|
|
|
case "ok":
|
|
|
|
|
c.Talkers.PokememeAddSuccessMessage(update)
|
|
|
|
|
case "dup":
|
|
|
|
|
c.Talkers.PokememeAddDuplicateMessage(update)
|
|
|
|
|
case "fail":
|
|
|
|
|
c.Talkers.PokememeAddFailureMessage(update)
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
} else {
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.AnyMessageUnauthorized(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Direct messages from user
|
|
|
|
|
switch {
|
|
|
|
|
case helloMsg.MatchString(text):
|
|
|
|
|
if player_raw.Id != 0 {
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.HelloMessageAuthorized(update, player_raw)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
} else {
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.HelloMessageUnauthorized(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
// Help
|
|
|
|
|
case helpMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
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)
|
|
|
|
|
}
|
2017-10-07 19:58:14 +04:00
|
|
|
|
case pokememeInfoMsg.MatchString(text):
|
|
|
|
|
c.Talkers.PokememeInfo(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
// Easter eggs
|
|
|
|
|
case huMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.MatMessage(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
case blMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.MatMessage(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
case ebMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.MatMessage(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
case piMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.MatMessage(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
case durakMsg.MatchString(text):
|
2017-10-07 02:23:25 +04:00
|
|
|
|
c.Talkers.DurakMessage(update)
|
2017-10-06 02:56:06 +04:00
|
|
|
|
default:
|
|
|
|
|
log.Printf("User posted unknown command.")
|
|
|
|
|
return "fail"
|
|
|
|
|
}
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|