package middleware import ( "context" "github.com/go-telegram/bot" "github.com/go-telegram/bot/models" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "trle5.xyz/trbot/utils" "trle5.xyz/trbot/utils/type/message_utils" "trle5.xyz/trbot/utils/type/update_utils" ) func ZeroLog(logger zerolog.Logger) func (next bot.HandlerFunc) bot.HandlerFunc { return func(next bot.HandlerFunc) bot.HandlerFunc { return func(ctx context.Context, bot *bot.Bot, update *models.Update) { ctx = logger.WithContext(ctx) next(ctx, bot, update) } } } func ZeroLogRequestLog(next bot.HandlerFunc) bot.HandlerFunc { return func(ctx context.Context, bot *bot.Bot, update *models.Update) { logger := zerolog.Ctx(ctx) if logger.GetLevel() == zerolog.Disabled { logger = &log.Logger } updateType := update_utils.GetUpdateType(update) switch updateType.AsType() { case update_utils.Message: // 正常消息 if update.Message.Photo != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Str("caption", update.Message.Caption). Str("photoID", update.Message.Photo[len(update.Message.Photo)-1].FileID). Msg("photoMessage") } else if update.Message.Sticker != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Dict("sticker", zerolog.Dict(). Str("emoji", update.Message.Sticker.Emoji). Str("setname", update.Message.Sticker.SetName). Str("fileID", update.Message.Sticker.FileID), ). Msg("stickerMessage") } else if update.Message.Video != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Str("caption", update.Message.Caption). Dict("video", zerolog.Dict(). Str("type", update.Message.Video.MimeType). Int("duration", update.Message.Video.Duration). Str("fileID", update.Message.Video.FileID), ). Msg("videoMessage") } else if update.Message.Animation != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Dict("animation", zerolog.Dict(). Int("duration", update.Message.Animation.Duration). Str("fileID", update.Message.Animation.FileID), ). Msg("gifMessage") } else if update.Message.Document != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Str("caption", update.Message.Caption). Dict("document", zerolog.Dict(). Str("fileName", update.Message.Document.FileName). Str("fileID", update.Message.Document.FileID), ). Msg("documentMessage") } else if update.Message.PinnedMessage != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Msg("pinnedMessage") } else if update.Message.NewChatMembers != nil { for _, chatMember := range update.Message.NewChatMembers { logger.Info(). Dict(utils.GetUserDict(&chatMember)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Msg("newChatMemberMessage") } } else if update.Message.LeftChatMember != nil { logger.Info(). Dict(utils.GetUserDict(update.Message.LeftChatMember)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Msg("leftChatMemberMessage") } else { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.Message)). Dict(utils.GetChatDict(&update.Message.Chat)). Int("messageID", update.Message.ID). Str("text", update.Message.Text). Str("type", message_utils.GetMessageType(update.Message).Str()). Msg("normalMessage") } case update_utils.EditedMessage: // 私聊或群组消息被编辑 if update.EditedMessage.Caption != "" { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.EditedMessage)). Dict(utils.GetChatDict(&update.EditedMessage.Chat)). Int("messageID", update.EditedMessage.ID). Str("editedCaption", update.EditedMessage.Caption). Msg("editedMessage") } else { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.EditedMessage)). Dict(utils.GetChatDict(&update.EditedMessage.Chat)). Int("messageID", update.EditedMessage.ID). Str("editedText", update.EditedMessage.Text). Msg("editedMessage") } case update_utils.InlineQuery: // inline 查询 logger.Info(). Dict(utils.GetUserDict(update.InlineQuery.From)). Str("query", update.InlineQuery.Query). Msg("inline request") case update_utils.ChosenInlineResult: // inline 查询结果被选择 logger.Info(). Dict(utils.GetUserDict(&update.ChosenInlineResult.From)). Str("query", update.ChosenInlineResult.Query). Str("resultID", update.ChosenInlineResult.ResultID). Msg("chosen inline result") case update_utils.CallbackQuery: // replymarkup 回调 var chat = zerolog.Dict() if update.CallbackQuery.Message.Message != nil { // some time `update.CallbackQuery.Message` will be nil _, chat = utils.GetChatDict(&update.CallbackQuery.Message.Message.Chat) } logger.Info(). Dict(utils.GetUserDict(&update.CallbackQuery.From)). Dict("chat", chat). Str("callbackQueryData", update.CallbackQuery.Data). Msg("callback query") case update_utils.MessageReaction: // 私聊或群组表情回应 if len(update.MessageReaction.OldReaction) > 0 { for i, oldReaction := range update.MessageReaction.OldReaction { if oldReaction.ReactionTypeEmoji != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("removedEmoji", oldReaction.ReactionTypeEmoji.Emoji). Str("emojiType", string(oldReaction.ReactionTypeEmoji.Type)). Int("count", i+1). Msg("removed emoji reaction") } else if oldReaction.ReactionTypeCustomEmoji != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("removedEmojiID", oldReaction.ReactionTypeCustomEmoji.CustomEmojiID). Str("emojiType", string(oldReaction.ReactionTypeCustomEmoji.Type)). Int("count", i+1). Msg("removed custom emoji reaction") } else if oldReaction.ReactionTypePaid != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("emojiType", string(oldReaction.ReactionTypePaid.Type)). Int("count", i+1). Msg("removed paid emoji reaction") } } } if len(update.MessageReaction.NewReaction) > 0 { for i, newReaction := range update.MessageReaction.NewReaction { if newReaction.ReactionTypeEmoji != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("addEmoji", newReaction.ReactionTypeEmoji.Emoji). Str("emojiType", string(newReaction.ReactionTypeEmoji.Type)). Int("count", i+1). Msg("add emoji reaction") } else if newReaction.ReactionTypeCustomEmoji != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("addEmojiID", newReaction.ReactionTypeCustomEmoji.CustomEmojiID). Str("emojiType", string(newReaction.ReactionTypeCustomEmoji.Type)). Int("count", i+1). Msg("add custom emoji reaction") } else if newReaction.ReactionTypePaid != nil { logger.Info(). Dict(utils.GetUserDict(update.MessageReaction.User)). Dict(utils.GetChatDict(&update.MessageReaction.Chat)). Int("messageID", update.MessageReaction.MessageID). Str("emojiType", string(newReaction.ReactionTypePaid.Type)). Int("count", i+1). Msg("add paid emoji reaction") } } } case update_utils.MessageReactionCount: // 频道消息表情回应数量 var emoji = zerolog.Dict() var customEmoji = zerolog.Dict() var paid = zerolog.Dict() for _, n := range update.MessageReactionCount.Reactions { switch n.Type.Type { case models.ReactionTypeTypeEmoji: emoji.Dict(n.Type.ReactionTypeEmoji.Emoji, zerolog.Dict(). // Str("type", string(n.Type.ReactionTypeEmoji.Type)). // Str("emoji", n.Type.ReactionTypeEmoji.Emoji). Int("count", n.TotalCount), ) case models.ReactionTypeTypeCustomEmoji: customEmoji.Dict(n.Type.ReactionTypeCustomEmoji.CustomEmojiID, zerolog.Dict(). // Str("type", string(n.Type.ReactionTypeCustomEmoji.Type)). // Str("customEmojiID", n.Type.ReactionTypeCustomEmoji.CustomEmojiID). Int("count", n.TotalCount), ) case models.ReactionTypeTypePaid: paid.Dict(n.Type.ReactionTypePaid.Type, zerolog.Dict(). // Str("type", n.Type.ReactionTypePaid.Type). Int("count", n.TotalCount), ) } } logger.Info(). Dict(utils.GetChatDict(&update.MessageReactionCount.Chat)). Dict("reactions", zerolog.Dict(). Dict("emoji", emoji). Dict("customEmoji", customEmoji). Dict("paid", paid), ). Int("messageID", update.MessageReactionCount.MessageID). Msg("emoji reaction count updated") case update_utils.ChannelPost: // 频道信息 if update.ChannelPost.Photo != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Str("caption", update.ChannelPost.Caption). Str("photoID", update.ChannelPost.Photo[len(update.ChannelPost.Photo)-1].FileID). Msg("channelPhoto") } else if update.ChannelPost.Sticker != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Dict("sticker", zerolog.Dict(). Str("emoji", update.ChannelPost.Sticker.Emoji). Str("setname", update.ChannelPost.Sticker.SetName). Str("fileID", update.ChannelPost.Sticker.FileID), ). Msg("channelSticker") } else if update.ChannelPost.Video != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Str("caption", update.ChannelPost.Caption). Dict("video", zerolog.Dict(). Str("type", update.ChannelPost.Video.MimeType). Int("duration", update.ChannelPost.Video.Duration). Str("fileID", update.ChannelPost.Video.FileID), ). Msg("channelVideo") } else if update.ChannelPost.Animation != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Dict("animation", zerolog.Dict(). Int("duration", update.ChannelPost.Animation.Duration). Str("fileID", update.ChannelPost.Animation.FileID), ). Msg("channelGIF") } else if update.ChannelPost.Document != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Str("caption", update.ChannelPost.Caption). Dict("document", zerolog.Dict(). Str("fileName", update.ChannelPost.Document.FileName). Str("fileID", update.ChannelPost.Document.FileID), ). Msg("channelDocument") } else if update.ChannelPost.PinnedMessage != nil { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Msg("channelPinnedMessage") } else { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.ChannelPost)). Dict(utils.GetChatDict(&update.ChannelPost.Chat)). Int("messageID", update.ChannelPost.ID). Str("text", update.ChannelPost.Text). Str("type", message_utils.GetMessageType(update.ChannelPost).Str()). Msg("channelPost") } case update_utils.EditedChannelPost: // 频道中编辑过的消息 if update.EditedChannelPost.Caption != "" { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.EditedChannelPost)). Dict(utils.GetChatDict(&update.EditedChannelPost.Chat)). Int("messageID", update.EditedChannelPost.ID). Str("editedCaption", update.EditedChannelPost.Caption). Msg("edited channel post caption") } else { logger.Info(). Dict(utils.GetUserOrSenderChatDict(update.EditedChannelPost)). Dict(utils.GetChatDict(&update.EditedChannelPost.Chat)). Int("messageID", update.EditedChannelPost.ID). Str("editedText", update.EditedChannelPost.Text). Msg("edited channel post") } case update_utils.ChatMember: logger.Info(). Dict(utils.GetUserDict(&update.ChatMember.From)). Dict(utils.GetChatDict(&update.ChatMember.Chat)). Str("oldType", string(update.ChatMember.OldChatMember.Type)). Str("newType", string(update.ChatMember.NewChatMember.Type)). Str("notice", "the user field may not be the actual user whose status was changed"). Msg("chatMemberUpdated") default: // 其他没有加入的更新类型 logger.Warn(). Str("updateType", updateType.Str()). Msg("Receive a no tagged update type") } next(ctx, bot, update) } }