Ref #2 This commit adds a link to a Go Telegram API to send messages to a given telegram channel provided with a JSON config file. At the moment the program sends a simple status message to the bot when starting up, but this allows testing whether the link to Telegram is correctly working.
This commit is contained in:
parent
56f909d229
commit
54de3b84cd
5 changed files with 133 additions and 3 deletions
37
tracker/bot/bot.go
Normal file
37
tracker/bot/bot.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
|
||||
"github.com/ahugues/go-telegram-api/bot"
|
||||
)
|
||||
|
||||
type Notifier struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
tgBot *bot.ConcreteBot
|
||||
notifChannel int64
|
||||
}
|
||||
|
||||
func (n *Notifier) SendInitMessage() error {
|
||||
initMsg := fmt.Sprintf("Public IP tracked initialized at %v", time.Now())
|
||||
if err := n.tgBot.SendMessage(n.ctx, n.notifChannel, initMsg); err != nil {
|
||||
return fmt.Errorf("failed to send initialization message: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(ctx context.Context, config *config.Config) *Notifier {
|
||||
subCtx, cancel := context.WithCancel(ctx)
|
||||
tgBot := bot.New(config.Telegram.Token)
|
||||
|
||||
return &Notifier{
|
||||
ctx: subCtx,
|
||||
cancel: cancel,
|
||||
tgBot: tgBot,
|
||||
notifChannel: config.Telegram.ChannelID,
|
||||
}
|
||||
}
|
28
tracker/config/config.go
Normal file
28
tracker/config/config.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type TelegramConfig struct {
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Telegram *TelegramConfig `json:"telegram"`
|
||||
}
|
||||
|
||||
func New(filepath string) (*Config, error) {
|
||||
content, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
|
||||
}
|
||||
var conf Config
|
||||
if err := json.Unmarshal(content, &conf); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
return &conf, nil
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
module git.faercol.me/faercol/public-ip-tracker/tracker
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/ahugues/go-telegram-api v0.0.0-20230125191847-f1f02f942580
|
||||
|
|
27
tracker/go.sum
Normal file
27
tracker/go.sum
Normal file
|
@ -0,0 +1,27 @@
|
|||
github.com/ahugues/go-telegram-api v0.0.0-20230125191847-f1f02f942580 h1:0EzaHeqTet8yPON8P6kHfHVl9Zb8+eWON0wPco93N7Y=
|
||||
github.com/ahugues/go-telegram-api v0.0.0-20230125191847-f1f02f942580/go.mod h1:8I/JWxd9GYM7dHOgGmkRI3Ei1u+nGvzeR2knIMmFw7E=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
@ -1,11 +1,47 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"git.faercol.me/faercol/public-ip-tracker/tracker/bot"
|
||||
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
|
||||
)
|
||||
|
||||
func testMethod(a int) int {
|
||||
return a + 2
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("public ip tracker")
|
||||
type cliArgs struct {
|
||||
configPath string
|
||||
}
|
||||
|
||||
func parseArgs() *cliArgs {
|
||||
configPath := flag.String("config", "", "Path to the JSON configuration file")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
return &cliArgs{
|
||||
configPath: *configPath,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Parsing arguments")
|
||||
args := parseArgs()
|
||||
|
||||
fmt.Println("Parsing config")
|
||||
conf, err := config.New(args.configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Initializing bot")
|
||||
notifBot := bot.New(context.Background(), conf)
|
||||
if err := notifBot.SendInitMessage(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("OK")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue