123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package main
- import (
- "fmt"
- "log"
- "bufio"
- "os"
- "time"
- "strings"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
- "GoEthemineTelegramBot/botsettings"
- )
- var (
- dBConnector Connection
- myBotClient tgbotapi.BotAPI
- LogInfo *log.Logger
- LogWarn *log.Logger
- LogError *log.Logger
- LogDebug *log.Logger
- )
- func main() {
- settingsPath := "config.json"
- LoadSettings(settingsPath)
- LoggersInit()
- LogInfo.Println("App started")
- botClient, err := tgbotapi.NewBotAPI(botsettings.Token())
- if err != nil {
- LogError.Panic(err)
- }
- myBotClient = *botClient
- var me = botClient.Self
- LogInfo.Printf("Hello, World! I am user %d and my name is %s.\n", me.ID, me.FirstName)
- if len(os.Args[1:]) >= 2 {
- botsettings.SetDbUser(os.Args[1])
- botsettings.SetDbPassword(os.Args[2])
- } else {
- var username string
- fmt.Print("DataBase Username:")
- _, err := fmt.Scanln(&username)
- if err != nil {
- LogError.Println(os.Stderr, err)
- return
- }
- botsettings.SetDbUser(username)
- var password string
- fmt.Print("DataBase Password:")
- _, err = fmt.Scanln(&password)
- if err != nil {
- LogError.Println(os.Stderr, err)
- return
- }
- botsettings.SetDbPassword(password)
- }
-
- dBConnector.Init()
- go Updater()
- go Listener(botClient)
- fmt.Println("Press Enter to exit")
- reader := bufio.NewReader(os.Stdin)
- reader.ReadString('\n')
- }
- // Listens for comming messages.
- func Listener(botClient *tgbotapi.BotAPI) {
- 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 HandleMessage(update.Message, botClient)
- }
- }
- // Starts DataUpdater goroutine every 4 minutes
- func Updater() {
- LogInfo.Println("Scheduler started!")
- for {
- go dBConnector.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 := botsettings.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)
- botsettings.SaveToFile(path)
- fmt.Println("Please fill the config file")
- os.Exit(0)
- }
- botsettings.SaveToFile(settingsPath)
- }
|