Change project structure a bit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ownerChatId == 0 {
|
||||
logger.Info(fmt.Sprintf("owner chat ID not set. set to %d to use the last chat", update.Message.Chat.ID))
|
||||
return
|
||||
}
|
||||
|
||||
params := &bot.CopyMessageParams{
|
||||
ChatID: ownerChatId,
|
||||
FromChatID: update.Message.Chat.ID,
|
||||
MessageID: update.Message.ID,
|
||||
}
|
||||
_, err := b.CopyMessage(ctx, params)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
sendParams := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: messageSentMessage,
|
||||
}
|
||||
_, err = b.SendMessage(ctx, sendParams)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package handler
|
||||
|
||||
const (
|
||||
greetingMessage = "Жду твоих сообщений!!"
|
||||
messageSentMessage = "Сообщение отправлено!!"
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
func NewStartCommandHandler(logger jsonlog.Logger) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
}
|
||||
|
||||
params := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: greetingMessage,
|
||||
}
|
||||
_, err := b.SendMessage(ctx, params)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
const (
|
||||
chatIDField = "chat_id"
|
||||
usernameField = "username"
|
||||
)
|
||||
|
||||
func NewLoggingMiddleware(logger jsonlog.Logger) bot.Middleware {
|
||||
return func(next bot.HandlerFunc) bot.HandlerFunc {
|
||||
return func(ctx context.Context, bot *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
}
|
||||
|
||||
chatLogger := logger.
|
||||
WithField(chatIDField, update.Message.Chat.ID).
|
||||
WithField(usernameField, update.Message.From.Username)
|
||||
|
||||
text := update.Message.Text
|
||||
if len(update.Message.Caption) > 0 {
|
||||
text = update.Message.Caption
|
||||
}
|
||||
|
||||
chatLogger.Info(fmt.Sprintf("new message: %s", text))
|
||||
|
||||
next(ctx, bot, update)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user