From 8306698f9aae5ae9e96203657cd8f42d787320b9 Mon Sep 17 00:00:00 2001 From: Matt Burchett Date: Tue, 21 Jan 2020 13:38:02 -0600 Subject: [PATCH] Adding rewrite --- cmd/main.go | 79 ++++++++++++++++++ go.mod | 5 ++ go.sum | 4 + .../config-example.json | 0 go_telegram => old/go_telegram | Bin main.go => old/main.go | 0 pkg/config/config.go | 40 +++++++++ 7 files changed, 128 insertions(+) create mode 100644 cmd/main.go create mode 100644 go.mod create mode 100644 go.sum rename config-example.json => old/config-example.json (100%) rename go_telegram => old/go_telegram (100%) rename main.go => old/main.go (100%) create mode 100644 pkg/config/config.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..e33e489 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" + "log" + "time" + + "github.com/mattburchett/go_telegram/pkg/config" + tb "gopkg.in/tucnak/telebot.v2" +) + +func main() { + + cfg, err := config.GetConfig("config.json") + if err != nil { + log.Fatal("Failed to load config.") + } + + b, err := tb.NewBot(tb.Settings{ + Token: cfg.TelegramToken, + Poller: &tb.LongPoller{Timeout: 10 * time.Second}, + }) + + if err != nil { + log.Fatal(err) + return + } + + test := make([][]tb.InlineButton, 0) + test = append(test, []tb.InlineButton{{Unique: "1", Text: "test"}}) + + fmt.Println(test) + fmt.Println(len(test)) + fmt.Println(len(test[0])) + + b.Handle(test[1][1], func(c *tb.Callback) { + b.Respond(c, &tb.CallbackResponse{Text: c.ID}) + }) + + b.Handle("/ping", func(m *tb.Message) { + b.Send(m.Sender, "pong") + }) + + b.Handle("/test", func(m *tb.Message) { + b.Send(m.Sender, "Inline test.", &tb.ReplyMarkup{ + InlineKeyboard: test, + }) + }) + + b.Start() + +} + +func test(b *tb.Bot) { + inlineBtns := []tb.InlineButton{tb.InlineButton{Unique: "1", Text: "Ping"}, tb.InlineButton{Unique: "2", Text: "Is"}, tb.InlineButton{Unique: "3", Text: "Stupid"}} + inlineKeys := [][]tb.InlineButton{inlineBtns} + + for _, btn := range inlineBtns { + b.Handle(&btn, func(c *tb.Callback) { + b.Respond(c, &tb.CallbackResponse{Text: c.Message.Text}) + }) + } + + b.Handle("/test", func(m *tb.Message) { + b.Send(m.Sender, "Inline test.", &tb.ReplyMarkup{ + InlineKeyboard: inlineKeys, + }) + }) +} + +func inlineButton(txt string) [][]tb.InlineButton { + return [][]tb.InlineButton{ + { + tb.InlineButton{ + Text: txt, + }, + }, + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c8b24dd --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/mattburchett/go_telegram + +go 1.13 + +require gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ae4710a --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99 h1:EMQ2hWPVLRFE/1jEruLFkhy1ACshSEg3X3PeCtGM8mA= +gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99/go.mod h1:+//wyPtHTeW2kfyEBwB05Hqnxev7AGrsLIyylSH++KU= diff --git a/config-example.json b/old/config-example.json similarity index 100% rename from config-example.json rename to old/config-example.json diff --git a/go_telegram b/old/go_telegram similarity index 100% rename from go_telegram rename to old/go_telegram diff --git a/main.go b/old/main.go similarity index 100% rename from main.go rename to old/main.go diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..034b259 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,40 @@ +package config + +import ( + "encoding/json" + "fmt" + "log" + "os" +) + +// Config - This struct will hold configuration components. +type Config struct { + TelegramToken string `json:"telegramToken"` + TelegramChatID string `json:"telegramChatID"` +} + +//GetConfig gets the configuration values for the api using the file in the supplied configPath. +func GetConfig(configPath string) (Config, error) { + if _, err := os.Stat(configPath); os.IsNotExist(err) { + return Config{}, fmt.Errorf("could not find the config file at path %s", configPath) + } + log.Println("Loading Configuration File: " + configPath) + return loadConfigFromFile(configPath) +} + +//if the config loaded from the file errors, no defaults will be loaded and the app will exit. +func loadConfigFromFile(configPath string) (conf Config, err error) { + file, err := os.Open(configPath) + if err != nil { + log.Printf("Error opening config file: %v", err) + } else { + defer file.Close() + + err = json.NewDecoder(file).Decode(&conf) + if err != nil { + log.Printf("Error decoding config file: %v", err) + } + } + + return conf, err +}