Archived
1

Initial commit

This commit is contained in:
Vladimir Hodakov
2017-10-04 17:56:18 +04:00
commit 4fec8f0fe7
14 changed files with 449 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package appcontext
import (
// 3rd-party
"github.com/jmoiron/sqlx"
"gopkg.in/telegram-bot-api.v4"
// local
"../config"
"../connections"
// interfaces
"../router/routerinterface"
)
type Context struct {
Cfg *config.Config
Bot *tgbotapi.BotAPI
Router routerinterface.RouterInterface
Db *sqlx.DB
}
func (c *Context) Init() {
c.Cfg = config.New()
c.Cfg.Init()
c.Bot = connections.BotInit(c.Cfg)
c.Db = connections.DBInit(c.Cfg)
}
func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) {
c.Router = ri
c.Router.Init()
}

View File

@@ -0,0 +1,13 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package appcontext
var (
a *Context
)
func New() *Context {
c := &Context{}
return c
}

50
lib/config/config.go Normal file
View File

@@ -0,0 +1,50 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package config
import (
// stdlib
"io/ioutil"
"log"
"path/filepath"
// 3rd-party
"gopkg.in/yaml.v2"
)
const VERSION = "0.03"
type DatabaseConnection struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
}
type TelegramConnection struct {
APIToken string `yaml:"api_token"`
}
type Config struct {
Telegram TelegramConnection `yaml:"telegram_connection"`
Database DatabaseConnection `yaml:"database_connection"`
}
func (c *Config) Init() {
fname, _ := filepath.Abs("./config.yml")
yamlFile, yerr := ioutil.ReadFile(fname)
if yerr != nil {
log.Fatal("Can't read config file")
}
yperr := yaml.Unmarshal(yamlFile, c)
if yperr != nil {
log.Fatal("Can't parse config file")
}
}
func New() *Config {
c := &Config{}
return c
}

View File

@@ -0,0 +1,38 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package connections
import (
// stdlib
"log"
// 3rd-party
"gopkg.in/telegram-bot-api.v4"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
// local
"../config"
)
func BotInit(cfg *config.Config) *tgbotapi.BotAPI {
bot, err := tgbotapi.NewBotAPI(cfg.Telegram.APIToken)
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Bot version: " + config.VERSION)
log.Printf("Authorized on account %s", bot.Self.UserName)
return bot
}
func DBInit(cfg *config.Config) *sqlx.DB {
database, err := sqlx.Connect("mysql", cfg.Database.User + ":" + cfg.Database.Password + "@tcp(" + cfg.Database.Host + ":" + cfg.Database.Port + ")/" + cfg.Database.Database + "?parseTime=true&charset=utf8mb4,utf8")
if err != nil {
log.Fatal(err)
}
log.Printf("Database connection established!")
return database
}

26
lib/router/exported.go Normal file
View File

@@ -0,0 +1,26 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package router
import (
// stdlib
"log"
// local
"../appcontext"
)
var (
c *appcontext.Context
r *Router
)
func New(ac *appcontext.Context) {
c = ac
rh := RouterHandler{}
c.RegisterRouterInterface(rh)
}
func (r *Router) Init() {
log.Printf("Initialized request router...")
}

19
lib/router/handler.go Normal file
View File

@@ -0,0 +1,19 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package router
import (
// 3rd party
"gopkg.in/telegram-bot-api.v4"
)
type RouterHandler struct {}
func (rh RouterHandler) Init() {
r.Init()
}
func (rh RouterHandler) RouteRequest(update tgbotapi.Update) string {
return r.RouteRequest(update)
}

51
lib/router/router.go Normal file
View File

@@ -0,0 +1,51 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package router
import (
// stdlib
"log"
"regexp"
// 3rd party
"gopkg.in/telegram-bot-api.v4"
// local
"../talkers"
)
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
// Regular expressions
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
var blMsg = regexp.MustCompile("\\s(Б|б)(Л|л)(Я|я)(Т|т|Д|д)")
var ebMsg = regexp.MustCompile("(Е|е|Ё|ё)(Б|б)(\\s|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)")
var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)")
var helpMsg = regexp.MustCompile("/help\\z")
switch {
case helpMsg.MatchString(text):
talkers.HelpMessage(c.Bot, update)
case huMsg.MatchString(text):
talkers.MatMessage(c.Bot, update)
case blMsg.MatchString(text):
talkers.MatMessage(c.Bot, update)
case ebMsg.MatchString(text):
talkers.MatMessage(c.Bot, update)
case piMsg.MatchString(text):
talkers.MatMessage(c.Bot, update)
case durakMsg.MatchString(text):
talkers.DurakMessage(c.Bot, update)
default:
log.Printf("User posted unknown command.")
return "fail"
}
return "ok"
}

View File

@@ -0,0 +1,14 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package routerinterface
import (
// 3rd party
"gopkg.in/telegram-bot-api.v4"
)
type RouterInterface interface {
Init()
RouteRequest(update tgbotapi.Update) string
}

48
lib/talkers/easter.go Normal file
View File

@@ -0,0 +1,48 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package talkers
import (
// stdlib
"log"
"math/rand"
"time"
// 3rd party
"gopkg.in/telegram-bot-api.v4"
)
func DurakMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
reactions := make([]string, 0)
reactions = append(reactions, "Сам такой!",
"А ты типа нет?",
"Фу, как некультурно!",
"Профессор, если вы такой умный, то почему вы такой бедный? /donate",
"Попка не дурак, Попка самый непадающий бот!")
// Praise the Random Gods!
rand.Seed(time.Now().Unix())
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reactions[rand.Intn(len(reactions))])
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
func MatMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
reactions := make([]string, 0)
reactions = append(reactions, "Фу, как некультурно!",
"Иди рот с мылом помой",
"Тшшшш!",
"Да я твою мамку в кино водил!")
// Praise the Random Gods!
rand.Seed(time.Now().Unix())
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reactions[rand.Intn(len(reactions))])
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}

25
lib/talkers/help.go Normal file
View File

@@ -0,0 +1,25 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package talkers
import (
// 3rd party
"gopkg.in/telegram-bot-api.v4"
// local
"../config"
)
func HelpMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
help_message := "*Бот Инстинкта. Версия обезшпионенная и улучшенная.*\n\n"
help_message += "Текущая версия: *" + config.VERSION + "*\n\n"
help_message += "Список команд:\n\n"
help_message += "+ /help выводит данное сообщение\n"
help_message += "\n\n"
help_message += "Связаться с автором: @fat0troll\n"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, help_message)
msg.ParseMode = "Markdown"
bot.Send(msg)
}