generated from petuhovskiy/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f78aa12
commit 935b162
Showing
8 changed files
with
76 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
module github.com/petuhovskiy/go-template | ||
module github.com/rwlist/bot | ||
|
||
go 1.16 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/caarlos0/env/v6 v6.6.0 | ||
github.com/prometheus/client_golang v1.10.0 | ||
github.com/sirupsen/logrus v1.8.1 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.1 // indirect | ||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 | ||
github.com/golang/protobuf v1.4.3 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/common v0.18.0 // indirect | ||
github.com/prometheus/procfs v0.6.0 // indirect | ||
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect | ||
google.golang.org/protobuf v1.23.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mlog | ||
|
||
import "log/slog" | ||
|
||
func Err(err error) slog.Attr { | ||
return slog.String("err", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mlog | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func Fatal(msg string, attrs ...any) { | ||
slog.Error(msg, attrs...) | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/petuhovskiy/go-template/pkg/conf" | ||
"github.com/rwlist/bot/internal/conf" | ||
"github.com/rwlist/bot/internal/mlog" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
log "github.com/sirupsen/logrus" | ||
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||
) | ||
|
||
func main() { | ||
log.SetFormatter(&log.JSONFormatter{}) | ||
log.SetReportCaller(true) | ||
log.SetLevel(log.DebugLevel) | ||
|
||
cfg, err := conf.ParseEnv() | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to parse config from env") | ||
mlog.Fatal("failed to parse config from env", mlog.Err(err)) | ||
} | ||
|
||
go func() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
err := http.ListenAndServe(cfg.PrometheusBind, mux) | ||
if err != nil && err != http.ErrServerClosed { | ||
log.WithError(err).Fatal("prometheus server error") | ||
mlog.Fatal("prometheus server error", mlog.Err(err)) | ||
} | ||
}() | ||
|
||
select {} | ||
bot, err := tgbotapi.NewBotAPI(cfg.TelegramToken) | ||
if err != nil { | ||
mlog.Fatal("failed to create bot", mlog.Err(err)) | ||
} | ||
bot.Debug = true | ||
slog.Info("Authorized on account", slog.String("username", bot.Self.UserName)) | ||
|
||
u := tgbotapi.NewUpdate(0) | ||
u.Timeout = 60 | ||
|
||
updates := bot.GetUpdatesChan(u) | ||
|
||
for update := range updates { | ||
if update.Message != nil { // If we got a message | ||
slog.Info("message from user", slog.String("username", update.Message.From.UserName), slog.String("message", update.Message.Text)) | ||
|
||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) | ||
msg.ReplyToMessageID = update.Message.MessageID | ||
|
||
bot.Send(msg) | ||
} | ||
} | ||
} |