main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "fmt"
  4. log "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/logger"
  5. "bufio"
  6. "os"
  7. "time"
  8. "strings"
  9. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
  10. "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/cmd/settings"
  11. "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/database"
  12. "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/cmd/handler"
  13. )
  14. func main() {
  15. var dBConnector database.Connection
  16. var globalHandler *handler.ActionHandler
  17. settingsPath := "config.json"
  18. LoadSettings(settingsPath)
  19. log.LoggersInit()
  20. log.LogInfo.Println("App started")
  21. botClient, err := tgbotapi.NewBotAPI(settings.Token())
  22. if err != nil {
  23. log.LogError.Panic(err)
  24. }
  25. var me = botClient.Self
  26. if len(os.Args[1:]) >= 2 {
  27. settings.SetDbUser(os.Args[1])
  28. settings.SetDbPassword(os.Args[2])
  29. } else {
  30. var username string
  31. fmt.Print("DataBase Username:")
  32. _, err := fmt.Scanln(&username)
  33. if err != nil {
  34. log.LogError.Println(err)
  35. return
  36. }
  37. settings.SetDbUser(username)
  38. var password string
  39. fmt.Print("DataBase Password:")
  40. _, err = fmt.Scanln(&password)
  41. if err != nil {
  42. log.LogError.Println(err)
  43. return
  44. }
  45. settings.SetDbPassword(password)
  46. }
  47. dBConnector.Init()
  48. globalHandler = handler.New(&dBConnector)
  49. go Updater(&dBConnector)
  50. go Listener(botClient, globalHandler)
  51. log.LogInfo.Printf("Started at @%s\n", me.UserName)
  52. fmt.Println("Press Enter to exit")
  53. reader := bufio.NewReader(os.Stdin)
  54. reader.ReadString('\n')
  55. }
  56. // Listens for comming messages.
  57. func Listener(botClient *tgbotapi.BotAPI, handler *handler.ActionHandler) {
  58. u := tgbotapi.NewUpdate(0)
  59. u.Timeout = 60
  60. updates, _ := botClient.GetUpdatesChan(u)
  61. for update := range updates {
  62. // Ignore any non-Message Updates
  63. if update.Message == nil {
  64. continue
  65. }
  66. go handler.HandleMessage(update.Message, botClient)
  67. }
  68. }
  69. // Starts DataUpdater goroutine every 4 minutes
  70. func Updater(c *database.Connection) {
  71. log.LogInfo.Println("Scheduler started!")
  72. for {
  73. go c.UpdateData()
  74. time.Sleep(4 * time.Minute)
  75. }
  76. }
  77. // LoadSettings load settings from settingsPath
  78. // and then saves them again for keep up-to-date struct
  79. func LoadSettings(settingsPath string) {
  80. err := settings.LoadFromFile(settingsPath)
  81. if err != nil {
  82. if !strings.Contains(err.Error(), "The system cannot find the file specified") {
  83. fmt.Println(err)
  84. os.Exit(1)
  85. }
  86. path, err := os.Getwd()
  87. if err != nil {
  88. fmt.Println(err)
  89. os.Exit(1)
  90. }
  91. path += "\\"+ settingsPath
  92. fmt.Printf("The system cannot find the config file.\nCreating new config file at:\n%s\n", path)
  93. settings.SaveToFile(path)
  94. fmt.Println("Please fill the config file")
  95. os.Exit(0)
  96. }
  97. settings.SaveToFile(settingsPath)
  98. }