123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package main
- import (
- "fmt"
- log "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/logger"
- "bufio"
- "os"
- "time"
- "strings"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
- "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/cmd/settings"
- "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/database"
- "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/cmd/handler"
- )
- func main() {
- var dBConnector database.Connection
- var globalHandler *handler.ActionHandler
- settingsPath := "config.json"
- LoadSettings(settingsPath)
- log.LoggersInit()
- log.LogInfo.Println("App started")
- botClient, err := tgbotapi.NewBotAPI(settings.Token())
- if err != nil {
- log.LogError.Panic(err)
- }
- var me = botClient.Self
- if len(os.Args[1:]) >= 2 {
- settings.SetDbUser(os.Args[1])
- settings.SetDbPassword(os.Args[2])
- } else {
- var username string
- fmt.Print("DataBase Username:")
- _, err := fmt.Scanln(&username)
- if err != nil {
- log.LogError.Println(err)
- return
- }
- settings.SetDbUser(username)
- var password string
- fmt.Print("DataBase Password:")
- _, err = fmt.Scanln(&password)
- if err != nil {
- log.LogError.Println(err)
- return
- }
- settings.SetDbPassword(password)
- }
- dBConnector.Init()
- globalHandler = handler.New(&dBConnector)
- go Updater(&dBConnector)
- go Listener(botClient, globalHandler)
- log.LogInfo.Printf("Started at @%s\n", me.UserName)
- fmt.Println("Press Enter to exit")
- reader := bufio.NewReader(os.Stdin)
- reader.ReadString('\n')
- }
- // Listens for comming messages.
- func Listener(botClient *tgbotapi.BotAPI, handler *handler.ActionHandler) {
- u := tgbotapi.NewUpdate(0)
- u.Timeout = 60
- updates, _ := botClient.GetUpdatesChan(u)
- for update := range updates {
- // Ignore any non-Message Updates
- if update.Message == nil {
- continue
- }
- go handler.HandleMessage(update.Message, botClient)
- }
- }
- // Starts DataUpdater goroutine every 4 minutes
- func Updater(c *database.Connection) {
- log.LogInfo.Println("Scheduler started!")
- for {
- go c.UpdateData()
- time.Sleep(4 * time.Minute)
- }
- }
- // LoadSettings load settings from settingsPath
- // and then saves them again for keep up-to-date struct
- func LoadSettings(settingsPath string) {
- err := settings.LoadFromFile(settingsPath)
- if err != nil {
- if !strings.Contains(err.Error(), "The system cannot find the file specified") {
- fmt.Println(err)
- os.Exit(1)
- }
- path, err := os.Getwd()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- path += "\\"+ settingsPath
- fmt.Printf("The system cannot find the config file.\nCreating new config file at:\n%s\n", path)
- settings.SaveToFile(path)
- fmt.Println("Please fill the config file")
- os.Exit(0)
- }
- settings.SaveToFile(settingsPath)
- }
|