Archived
1

Refactoring: now we respect gofmt and have some comments

This commit is contained in:
2017-10-18 09:39:50 +04:00
parent 6f374e560e
commit 8dab6c0699
47 changed files with 607 additions and 479 deletions

View File

@@ -15,12 +15,14 @@ var (
r *Router
)
// New is an initialization function for appcontext
func New(ac *appcontext.Context) {
c = ac
rh := RouterHandler{}
c.RegisterRouterInterface(rh)
}
// Init is an initialization function for package router
func (r *Router) Init() {
log.Printf("Initialized request router...")
}

View File

@@ -8,12 +8,15 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// RouterHandler is a handler for router package
type RouterHandler struct{}
// Init is an initialization function of router
func (rh RouterHandler) Init() {
r.Init()
}
// RouteRequest decides, what to do with user input
func (rh RouterHandler) RouteRequest(update tgbotapi.Update) string {
return r.RouteRequest(update)
}

View File

@@ -12,20 +12,29 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// Router is a function-handling struct for router
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"
// RouteRequest decides, what to do with user input
func (r *Router) RouteRequest(update tgbotapi.Update) string {
text := update.Message.Text
player_raw, ok := c.Getters.GetOrCreatePlayer(update.Message.From.ID)
playerRaw, ok := c.Getters.GetOrCreatePlayer(update.Message.From.ID)
if !ok {
// Silently fail
return "fail"
}
chatID := update.Message.Chat.ID
fromID := int64(update.Message.From.ID)
log.Println(chatID)
log.Println(fromID)
if chatID == fromID {
log.Println("Private chat")
} else {
log.Println("Group")
}
// Regular expressions
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
@@ -50,11 +59,11 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
log.Printf("Forward from another user or bot. Ignoring")
} else {
log.Printf("Forward from PokememBro bot! Processing...")
if player_raw.Id != 0 {
if playerRaw.ID != 0 {
switch {
case pokememeMsg.MatchString(text):
log.Printf("Pokememe posted!")
status := c.Parsers.ParsePokememe(text, player_raw)
status := c.Parsers.ParsePokememe(text, playerRaw)
switch status {
case "ok":
c.Talkers.PokememeAddSuccessMessage(update)
@@ -65,7 +74,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
}
case profileMsg.MatchString(text):
log.Printf("Profile posted!")
status := c.Parsers.ParseProfile(update, player_raw)
status := c.Parsers.ParseProfile(update, playerRaw)
switch status {
case "ok":
c.Talkers.ProfileAddSuccessMessage(update)
@@ -83,8 +92,8 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
// Direct messages from user
switch {
case helloMsg.MatchString(text):
if player_raw.Id != 0 {
c.Talkers.HelloMessageAuthorized(update, player_raw)
if playerRaw.ID != 0 {
c.Talkers.HelloMessageAuthorized(update, playerRaw)
} else {
c.Talkers.HelloMessageUnauthorized(update)
}
@@ -107,17 +116,17 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
c.Talkers.PokememesList(update, 1)
}
case pokememeInfoMsg.MatchString(text):
c.Talkers.PokememeInfo(update, player_raw)
c.Talkers.PokememeInfo(update, playerRaw)
// Profile info
case meMsg.MatchString(text):
if player_raw.Id != 0 {
c.Talkers.ProfileMessage(update, player_raw)
if playerRaw.ID != 0 {
c.Talkers.ProfileMessage(update, playerRaw)
} else {
c.Talkers.AnyMessageUnauthorized(update)
}
// Suggestions
case bestMsg.MatchString(text):
c.Talkers.BestPokememesList(update, player_raw)
c.Talkers.BestPokememesList(update, playerRaw)
// Easter eggs
case huMsg.MatchString(text):
c.Talkers.MatMessage(update)

View File

@@ -8,6 +8,7 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// RouterInterface implements Router for importing via appcontext.
type RouterInterface interface {
Init()
RouteRequest(update tgbotapi.Update) string