Adding rewrite

This commit is contained in:
Matt Burchett 2020-01-21 13:38:02 -06:00
parent f8c19d7467
commit 8306698f9a
7 changed files with 128 additions and 0 deletions

79
cmd/main.go Normal file
View File

@ -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,
},
},
}
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/mattburchett/go_telegram
go 1.13
require gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99

4
go.sum Normal file
View File

@ -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=

40
pkg/config/config.go Normal file
View File

@ -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
}