1
bot/models/chat_background.go

185 lines
5.8 KiB
Go
Raw Permalink Normal View History

2024-05-20 17:07:43 +08:00
package models
import (
"encoding/json"
"fmt"
)
2025-01-10 16:01:04 +08:00
type BackgroundTypeType string
2024-05-20 17:07:43 +08:00
const (
2025-01-10 16:01:04 +08:00
ChatBackgroundTypeFill BackgroundTypeType = "fill"
ChatBackgroundTypeWallpaper BackgroundTypeType = "wallpaper"
ChatBackgroundTypePattern BackgroundTypeType = "pattern"
ChatBackgroundTypeChatTheme BackgroundTypeType = "chat_theme"
2024-05-20 17:07:43 +08:00
)
// ChatBackground https://core.telegram.org/bots/api#chatbackground
type ChatBackground struct {
2025-01-10 16:01:04 +08:00
Type BackgroundType `json:"type"`
}
// BackgroundType https://core.telegram.org/bots/api#backgroundtype
type BackgroundType struct {
Type BackgroundTypeType
2024-05-20 17:07:43 +08:00
Fill *BackgroundTypeFill
Wallpaper *BackgroundTypeWallpaper
Pattern *BackgroundTypePattern
Theme *BackgroundTypeChatTheme
}
2025-01-10 16:01:04 +08:00
func (cb *BackgroundType) UnmarshalJSON(data []byte) error {
2024-05-20 17:07:43 +08:00
v := struct {
2025-01-10 16:01:04 +08:00
Type BackgroundTypeType `json:"type"`
2024-05-20 17:07:43 +08:00
}{}
2025-01-10 16:01:04 +08:00
2024-05-20 17:07:43 +08:00
if err := json.Unmarshal(data, &v); err != nil {
return err
}
2025-01-10 16:01:04 +08:00
switch v.Type {
case ChatBackgroundTypeFill:
2024-05-20 17:07:43 +08:00
cb.Type = ChatBackgroundTypeFill
2025-01-10 16:01:04 +08:00
return json.Unmarshal(data, &cb.Fill)
case ChatBackgroundTypeWallpaper:
2024-05-20 17:07:43 +08:00
cb.Type = ChatBackgroundTypeWallpaper
2025-01-10 16:01:04 +08:00
return json.Unmarshal(data, &cb.Wallpaper)
case ChatBackgroundTypePattern:
2024-05-20 17:07:43 +08:00
cb.Type = ChatBackgroundTypePattern
2025-01-10 16:01:04 +08:00
return json.Unmarshal(data, &cb.Pattern)
case ChatBackgroundTypeChatTheme:
2024-05-20 17:07:43 +08:00
cb.Type = ChatBackgroundTypeChatTheme
2025-01-10 16:01:04 +08:00
return json.Unmarshal(data, &cb.Theme)
2024-05-20 17:07:43 +08:00
}
return fmt.Errorf("unsupported ChatBackground type")
}
2025-01-10 16:01:04 +08:00
func (cb *BackgroundType) MarshalJSON() ([]byte, error) {
switch cb.Type {
case ChatBackgroundTypeFill:
cb.Fill.Type = ChatBackgroundTypeFill
return json.Marshal(cb.Fill)
case ChatBackgroundTypeWallpaper:
cb.Wallpaper.Type = ChatBackgroundTypeWallpaper
return json.Marshal(cb.Wallpaper)
case ChatBackgroundTypePattern:
cb.Pattern.Type = ChatBackgroundTypePattern
return json.Marshal(cb.Pattern)
case ChatBackgroundTypeChatTheme:
cb.Theme.Type = ChatBackgroundTypeChatTheme
return json.Marshal(cb.Theme)
}
return nil, fmt.Errorf("unsupported ChatBackground type")
}
2024-05-20 17:07:43 +08:00
// BackgroundTypeFill https://core.telegram.org/bots/api#backgroundtypefill
type BackgroundTypeFill struct {
2025-01-10 16:01:04 +08:00
Type BackgroundTypeType `json:"type"`
Fill BackgroundFill `json:"fill"`
DarkThemeDimming int `json:"dark_theme_dimming"`
2024-05-20 17:07:43 +08:00
}
// BackgroundTypeWallpaper https://core.telegram.org/bots/api#backgroundtypewallpaper
type BackgroundTypeWallpaper struct {
2025-01-10 16:01:04 +08:00
Type BackgroundTypeType `json:"type"`
Document Document `json:"document"`
DarkThemeDimming int `json:"dark_theme_dimming"`
IsBlurred bool `json:"is_blurred,omitempty"`
IsMoving bool `json:"is_moving,omitempty"`
2024-05-20 17:07:43 +08:00
}
// BackgroundTypePattern https://core.telegram.org/bots/api#backgroundtypepattern
type BackgroundTypePattern struct {
2025-01-10 16:01:04 +08:00
Type BackgroundTypeType `json:"type"`
Document Document `json:"document"`
Fill BackgroundFill `json:"fill"`
Intensity int `json:"intensity"`
IsInverted bool `json:"is_inverted,omitempty"`
IsMoving bool `json:"is_moving,omitempty"`
2024-05-20 17:07:43 +08:00
}
// BackgroundTypeChatTheme https://core.telegram.org/bots/api#backgroundtypechattheme
type BackgroundTypeChatTheme struct {
2025-01-10 16:01:04 +08:00
Type BackgroundTypeType `json:"type"`
ThemeName string `json:"theme_name"`
2024-05-20 17:07:43 +08:00
}
type BackgroundFillType string
const (
BackgroundFillTypeSolid BackgroundFillType = "solid"
BackgroundFillTypeGradient BackgroundFillType = "gradient"
BackgroundFillTypeFreeformGradient BackgroundFillType = "freeform_gradient"
)
type BackgroundFill struct {
Type BackgroundFillType
2024-05-20 17:07:43 +08:00
Solid *BackgroundFillSolid
Gradient *BackgroundFillGradient
FreeformGradient *BackgroundFillFreeformGradient
}
func (bf *BackgroundFill) UnmarshalJSON(data []byte) error {
v := struct {
Type BackgroundFillType `json:"type"`
2024-05-20 17:07:43 +08:00
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
switch v.Type {
case BackgroundFillTypeSolid:
2024-05-20 17:07:43 +08:00
bf.Type = BackgroundFillTypeSolid
bf.Solid = &BackgroundFillSolid{}
return json.Unmarshal(data, bf.Solid)
case BackgroundFillTypeGradient:
2024-05-20 17:07:43 +08:00
bf.Type = BackgroundFillTypeGradient
bf.Gradient = &BackgroundFillGradient{}
return json.Unmarshal(data, bf.Gradient)
case BackgroundFillTypeFreeformGradient:
2024-05-20 17:07:43 +08:00
bf.Type = BackgroundFillTypeFreeformGradient
bf.FreeformGradient = &BackgroundFillFreeformGradient{}
return json.Unmarshal(data, bf.FreeformGradient)
}
return fmt.Errorf("unsupported BackgroundFill type")
}
func (bf *BackgroundFill) MarshalJSON() ([]byte, error) {
switch bf.Type {
case BackgroundFillTypeSolid:
bf.Solid.Type = BackgroundFillTypeSolid
return json.Marshal(bf.Solid)
case BackgroundFillTypeGradient:
bf.Gradient.Type = BackgroundFillTypeGradient
return json.Marshal(bf.Gradient)
case BackgroundFillTypeFreeformGradient:
bf.FreeformGradient.Type = BackgroundFillTypeFreeformGradient
return json.Marshal(bf.FreeformGradient)
}
return nil, fmt.Errorf("unsupported BackgroundFill type")
}
2024-05-20 17:07:43 +08:00
// BackgroundFillSolid https://core.telegram.org/bots/api#backgroundfillsolid
type BackgroundFillSolid struct {
Type BackgroundFillType `json:"type"`
Color int `json:"color"`
2024-05-20 17:07:43 +08:00
}
// BackgroundFillGradient https://core.telegram.org/bots/api#backgroundfillgradient
type BackgroundFillGradient struct {
Type BackgroundFillType `json:"type"`
TopColor int `json:"top_color"`
BottomColor int `json:"bottom_color"`
RotationAngle int `json:"rotation_angle"`
2024-05-20 17:07:43 +08:00
}
// BackgroundFillFreeformGradient https://core.telegram.org/bots/api#backgroundfillfreeformgradient
type BackgroundFillFreeformGradient struct {
Type BackgroundFillType `json:"type"`
Colors []int `json:"colors"`
2024-05-20 17:07:43 +08:00
}