refactor(logging): use context logger in MsgContext
This commit is contained in:
+2
-3
@@ -22,7 +22,6 @@ func (bot *Bot[T]) handle(u *tgapi.Update) {
|
|||||||
|
|
||||||
ctx := &MsgContext{
|
ctx := &MsgContext{
|
||||||
Update: *u, Api: bot.api,
|
Update: *u, Api: bot.api,
|
||||||
botLogger: bot.logger,
|
|
||||||
errorTemplate: bot.errorTemplate,
|
errorTemplate: bot.errorTemplate,
|
||||||
l10n: bot.l10n,
|
l10n: bot.l10n,
|
||||||
draftProvider: bot.draftProvider,
|
draftProvider: bot.draftProvider,
|
||||||
@@ -100,7 +99,7 @@ func (bot *Bot[T]) handleMessage(update *tgapi.Update, ctx *MsgContext) {
|
|||||||
|
|
||||||
ctx.Logger = plugin.logger
|
ctx.Logger = plugin.logger
|
||||||
if ctx.Logger == nil {
|
if ctx.Logger == nil {
|
||||||
ctx.Logger = ctx.botLogger
|
ctx.Logger = bot.logger
|
||||||
}
|
}
|
||||||
plugin.executeCmd(cmd, ctx, bot.dbContext)
|
plugin.executeCmd(cmd, ctx, bot.dbContext)
|
||||||
return
|
return
|
||||||
@@ -138,7 +137,7 @@ func (bot *Bot[T]) handleCallback(update *tgapi.Update, ctx *MsgContext) {
|
|||||||
}
|
}
|
||||||
ctx.Logger = plugin.logger
|
ctx.Logger = plugin.logger
|
||||||
if ctx.Logger == nil {
|
if ctx.Logger == nil {
|
||||||
ctx.Logger = ctx.botLogger
|
ctx.Logger = bot.logger
|
||||||
}
|
}
|
||||||
plugin.executePayload(data.Command, ctx, bot.dbContext)
|
plugin.executePayload(data.Command, ctx, bot.dbContext)
|
||||||
return
|
return
|
||||||
|
|||||||
+19
-20
@@ -32,7 +32,6 @@ type MsgContext struct {
|
|||||||
Args []string
|
Args []string
|
||||||
|
|
||||||
errorTemplate string
|
errorTemplate string
|
||||||
botLogger *slog.Logger
|
|
||||||
l10n *L10n
|
l10n *L10n
|
||||||
draftProvider *DraftProvider
|
draftProvider *DraftProvider
|
||||||
payloadType BotPayloadType
|
payloadType BotPayloadType
|
||||||
@@ -61,7 +60,7 @@ func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard
|
|||||||
case ctx.InlineMsgId != "":
|
case ctx.InlineMsgId != "":
|
||||||
params.InlineMessageID = ctx.InlineMsgId
|
params.InlineMessageID = ctx.InlineMsgId
|
||||||
default:
|
default:
|
||||||
ctx.botLogger.Errorln("Can't edit message: no valid message target")
|
ctx.Logger.Errorln("Can't edit message: no valid message target")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if keyboard != nil {
|
if keyboard != nil {
|
||||||
@@ -69,7 +68,7 @@ func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard
|
|||||||
}
|
}
|
||||||
msg, _, err := ctx.Api.EditMessageText(params)
|
msg, _, err := ctx.Api.EditMessageText(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
resultMessageID := messageId
|
resultMessageID := messageId
|
||||||
@@ -99,7 +98,7 @@ func (m *AnswerMessage) EditMarkdown(text string) *AnswerMessage {
|
|||||||
// Supports both regular callback messages and inline callback messages.
|
// Supports both regular callback messages and inline callback messages.
|
||||||
func (ctx *MsgContext) editCallback(text string, keyboard *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
func (ctx *MsgContext) editCallback(text string, keyboard *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
||||||
if ctx.CallbackMsgId == 0 && ctx.InlineMsgId == "" {
|
if ctx.CallbackMsgId == 0 && ctx.InlineMsgId == "" {
|
||||||
ctx.botLogger.Errorln("Can't edit non-callback update message")
|
ctx.Logger.Errorln("Can't edit non-callback update message")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return ctx.edit(ctx.CallbackMsgId, text, keyboard, parseMode)
|
return ctx.edit(ctx.CallbackMsgId, text, keyboard, parseMode)
|
||||||
@@ -143,7 +142,7 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo
|
|||||||
case ctx.InlineMsgId != "":
|
case ctx.InlineMsgId != "":
|
||||||
params.InlineMessageID = ctx.InlineMsgId
|
params.InlineMessageID = ctx.InlineMsgId
|
||||||
default:
|
default:
|
||||||
ctx.botLogger.Errorln("Can't edit caption: no valid message target")
|
ctx.Logger.Errorln("Can't edit caption: no valid message target")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if kb != nil {
|
if kb != nil {
|
||||||
@@ -152,7 +151,7 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo
|
|||||||
|
|
||||||
msg, _, err := ctx.Api.EditMessageCaption(params)
|
msg, _, err := ctx.Api.EditMessageCaption(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
resultMessageID := messageId
|
resultMessageID := messageId
|
||||||
@@ -192,7 +191,7 @@ func (m *AnswerMessage) EditCaptionKeyboardMarkdown(text string, kb *InlineKeybo
|
|||||||
// Uses API limiter to respect Telegram rate limits per chat.
|
// Uses API limiter to respect Telegram rate limits per chat.
|
||||||
func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
||||||
if ctx.Msg == nil {
|
if ctx.Msg == nil {
|
||||||
ctx.botLogger.Errorln("Can't answer message without a message")
|
ctx.Logger.Errorln("Can't answer message without a message")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
params := tgapi.SendMessageP{
|
params := tgapi.SendMessageP{
|
||||||
@@ -212,7 +211,7 @@ func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard, parseMode t
|
|||||||
|
|
||||||
msg, err := ctx.Api.SendMessage(params)
|
msg, err := ctx.Api.SendMessage(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &AnswerMessage{
|
return &AnswerMessage{
|
||||||
@@ -259,7 +258,7 @@ func (ctx *MsgContext) KeyboardMarkdown(text string, keyboard *InlineKeyboard) *
|
|||||||
// answerPhoto sends a photo with optional caption and keyboard.
|
// answerPhoto sends a photo with optional caption and keyboard.
|
||||||
func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard, parseMode tgapi.ParseMode) *AnswerMessage {
|
||||||
if ctx.Msg == nil {
|
if ctx.Msg == nil {
|
||||||
ctx.botLogger.Errorln("Can't answer message without a message")
|
ctx.Logger.Errorln("Can't answer message without a message")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
params := tgapi.SendPhotoP{
|
params := tgapi.SendPhotoP{
|
||||||
@@ -280,7 +279,7 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard, par
|
|||||||
|
|
||||||
msg, err := ctx.Api.SendPhoto(params)
|
msg, err := ctx.Api.SendPhoto(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &AnswerMessage{
|
return &AnswerMessage{
|
||||||
@@ -327,11 +326,11 @@ func (ctx *MsgContext) AnswerPhotofMarkdown(photoId, template string, args ...an
|
|||||||
// delete removes a message by ID.
|
// delete removes a message by ID.
|
||||||
func (ctx *MsgContext) delete(messageId int) {
|
func (ctx *MsgContext) delete(messageId int) {
|
||||||
if messageId == 0 {
|
if messageId == 0 {
|
||||||
ctx.botLogger.Errorln("Can't delete message: message ID zero")
|
ctx.Logger.Errorln("Can't delete message: message ID zero")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if ctx.Msg == nil {
|
if ctx.Msg == nil {
|
||||||
ctx.botLogger.Errorln("Can't delete message: no chat message context")
|
ctx.Logger.Errorln("Can't delete message: no chat message context")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err := ctx.Api.DeleteMessage(tgapi.DeleteMessageP{
|
_, err := ctx.Api.DeleteMessage(tgapi.DeleteMessageP{
|
||||||
@@ -339,7 +338,7 @@ func (ctx *MsgContext) delete(messageId int) {
|
|||||||
MessageID: messageId,
|
MessageID: messageId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +348,7 @@ func (m *AnswerMessage) Delete() { m.ctx.delete(m.MessageID) }
|
|||||||
// CallbackDelete deletes the message that triggered the callback query.
|
// CallbackDelete deletes the message that triggered the callback query.
|
||||||
func (ctx *MsgContext) CallbackDelete() {
|
func (ctx *MsgContext) CallbackDelete() {
|
||||||
if ctx.CallbackMsgId == 0 {
|
if ctx.CallbackMsgId == 0 {
|
||||||
ctx.botLogger.Errorln("Can't delete callback message: no callback message ID")
|
ctx.Logger.Errorln("Can't delete callback message: no callback message ID")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.delete(ctx.CallbackMsgId)
|
ctx.delete(ctx.CallbackMsgId)
|
||||||
@@ -366,7 +365,7 @@ func (ctx *MsgContext) answerCallbackQuery(url, text string, showAlert bool) {
|
|||||||
Text: text, ShowAlert: showAlert, URL: url,
|
Text: text, ShowAlert: showAlert, URL: url,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +384,7 @@ func (ctx *MsgContext) AnswerCbQueryUrl(u string) { ctx.answerCallbackQuery(u, "
|
|||||||
// SendAction sends a chat action (typing, uploading_photo, etc.) to indicate bot activity.
|
// SendAction sends a chat action (typing, uploading_photo, etc.) to indicate bot activity.
|
||||||
func (ctx *MsgContext) SendAction(action tgapi.ChatActionType) {
|
func (ctx *MsgContext) SendAction(action tgapi.ChatActionType) {
|
||||||
if ctx.Msg == nil {
|
if ctx.Msg == nil {
|
||||||
ctx.botLogger.Errorln("Can't send action without chat message context")
|
ctx.Logger.Errorln("Can't send action without chat message context")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := tgapi.SendChatActionP{
|
params := tgapi.SendChatActionP{
|
||||||
@@ -396,7 +395,7 @@ func (ctx *MsgContext) SendAction(action tgapi.ChatActionType) {
|
|||||||
}
|
}
|
||||||
_, err := ctx.Api.SendChatAction(params)
|
_, err := ctx.Api.SendChatAction(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,7 +411,7 @@ func (ctx *MsgContext) error(err error) {
|
|||||||
} else {
|
} else {
|
||||||
ctx.answer(text, nil, tgapi.ParseNone)
|
ctx.answer(text, nil, tgapi.ParseNone)
|
||||||
}
|
}
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error is an alias for error().
|
// Error is an alias for error().
|
||||||
@@ -420,14 +419,14 @@ func (ctx *MsgContext) Error(err error) { ctx.error(err) }
|
|||||||
|
|
||||||
func (ctx *MsgContext) newDraft(parseMode tgapi.ParseMode) *Draft {
|
func (ctx *MsgContext) newDraft(parseMode tgapi.ParseMode) *Draft {
|
||||||
if ctx.Msg == nil {
|
if ctx.Msg == nil {
|
||||||
ctx.botLogger.Errorln("can't create draft: ctx.Msg is nil")
|
ctx.Logger.Errorln("can't create draft: ctx.Msg is nil")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
c, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := ctx.Api.Limiter.Wait(c, ctx.Msg.Chat.ID); err != nil {
|
if err := ctx.Api.Limiter.Wait(c, ctx.Msg.Chat.ID); err != nil {
|
||||||
ctx.botLogger.Errorln(err)
|
ctx.Logger.Errorln(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -48,7 +48,7 @@ func TestAnswerPhotoIncludesDirectMessagesTopicID(t *testing.T) {
|
|||||||
Chat: &tgapi.Chat{ID: 42, Type: string(tgapi.ChatTypePrivate)},
|
Chat: &tgapi.Chat{ID: 42, Type: string(tgapi.ChatTypePrivate)},
|
||||||
DirectMessageTopic: &tgapi.DirectMessageTopic{TopicID: 77},
|
DirectMessageTopic: &tgapi.DirectMessageTopic{TopicID: 77},
|
||||||
},
|
},
|
||||||
botLogger: slog.CreateLogger(),
|
Logger: slog.CreateLogger(),
|
||||||
}
|
}
|
||||||
|
|
||||||
answer := ctx.AnswerPhoto("photo-id", "caption")
|
answer := ctx.AnswerPhoto("photo-id", "caption")
|
||||||
|
|||||||
+2
-2
@@ -1,9 +1,9 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VersionString = "1.0.0-rc.7"
|
VersionString = "1.0.0-rc.8"
|
||||||
VersionMajor = 1
|
VersionMajor = 1
|
||||||
VersionMinor = 0
|
VersionMinor = 0
|
||||||
VersionPatch = 0
|
VersionPatch = 0
|
||||||
VersionBeta = 7
|
VersionBeta = 8
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user