main.go 2.5 KB

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