-
Notifications
You must be signed in to change notification settings - Fork 68
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
Showing
11 changed files
with
303 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## v1.3.0 (2024-05-20) | ||
|
||
- support API v7.3 | ||
|
||
## v1.2.2 (2024-04-25) | ||
|
||
- fix race in test | ||
|
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
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,141 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// BackgroundType https://core.telegram.org/bots/api#backgroundtype | ||
type BackgroundType string | ||
|
||
const ( | ||
ChatBackgroundTypeFill BackgroundType = "fill" | ||
ChatBackgroundTypeWallpaper BackgroundType = "wallpaper" | ||
ChatBackgroundTypePattern BackgroundType = "pattern" | ||
ChatBackgroundTypeChatTheme BackgroundType = "chat_theme" | ||
) | ||
|
||
// ChatBackground https://core.telegram.org/bots/api#chatbackground | ||
type ChatBackground struct { | ||
Type BackgroundType | ||
Fill *BackgroundTypeFill | ||
Wallpaper *BackgroundTypeWallpaper | ||
Pattern *BackgroundTypePattern | ||
Theme *BackgroundTypeChatTheme | ||
} | ||
|
||
func (cb *ChatBackground) UnmarshalJSON(data []byte) error { | ||
v := struct { | ||
Type string `json:"type"` | ||
}{} | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
|
||
switch v.Type { | ||
case "fill": | ||
cb.Type = ChatBackgroundTypeFill | ||
cb.Fill = &BackgroundTypeFill{} | ||
return json.Unmarshal(data, cb.Fill) | ||
case "wallpaper": | ||
cb.Type = ChatBackgroundTypeWallpaper | ||
cb.Wallpaper = &BackgroundTypeWallpaper{} | ||
return json.Unmarshal(data, cb.Wallpaper) | ||
case "pattern": | ||
cb.Type = ChatBackgroundTypePattern | ||
cb.Pattern = &BackgroundTypePattern{} | ||
return json.Unmarshal(data, cb.Pattern) | ||
case "chat_theme": | ||
cb.Type = ChatBackgroundTypeChatTheme | ||
cb.Theme = &BackgroundTypeChatTheme{} | ||
return json.Unmarshal(data, cb.Theme) | ||
} | ||
|
||
return fmt.Errorf("unsupported ChatBackground type") | ||
} | ||
|
||
// BackgroundTypeFill https://core.telegram.org/bots/api#backgroundtypefill | ||
type BackgroundTypeFill struct { | ||
Fill BackgroundFill `json:"fill"` | ||
DarkThemeDimming int `json:"dark_theme_dimming"` | ||
} | ||
|
||
// BackgroundTypeWallpaper https://core.telegram.org/bots/api#backgroundtypewallpaper | ||
type BackgroundTypeWallpaper struct { | ||
Document Document `json:"document"` | ||
DarkThemeDimming int `json:"dark_theme_dimming"` | ||
IsBlurred bool `json:"is_blurred,omitempty"` | ||
IsMoving bool `json:"is_moving,omitempty"` | ||
} | ||
|
||
// BackgroundTypePattern https://core.telegram.org/bots/api#backgroundtypepattern | ||
type BackgroundTypePattern struct { | ||
Document Document `json:"document"` | ||
Fill BackgroundFill `json:"fill"` | ||
Intensity int `json:"intensity"` | ||
IsInverted bool `json:"is_inverted,omitempty"` | ||
IsMoving bool `json:"is_moving,omitempty"` | ||
} | ||
|
||
// BackgroundTypeChatTheme https://core.telegram.org/bots/api#backgroundtypechattheme | ||
type BackgroundTypeChatTheme struct { | ||
ThemeName string `json:"theme_name"` | ||
} | ||
|
||
type BackgroundFillType string | ||
|
||
const ( | ||
BackgroundFillTypeSolid BackgroundFillType = "solid" | ||
BackgroundFillTypeGradient BackgroundFillType = "gradient" | ||
BackgroundFillTypeFreeformGradient BackgroundFillType = "freeform_gradient" | ||
) | ||
|
||
type BackgroundFill struct { | ||
Type BackgroundFillType `json:"type"` | ||
Solid *BackgroundFillSolid | ||
Gradient *BackgroundFillGradient | ||
FreeformGradient *BackgroundFillFreeformGradient | ||
} | ||
|
||
func (bf *BackgroundFill) UnmarshalJSON(data []byte) error { | ||
v := struct { | ||
Type string `json:"type"` | ||
}{} | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
|
||
switch v.Type { | ||
case "solid": | ||
bf.Type = BackgroundFillTypeSolid | ||
bf.Solid = &BackgroundFillSolid{} | ||
return json.Unmarshal(data, bf.Solid) | ||
case "gradient": | ||
bf.Type = BackgroundFillTypeGradient | ||
bf.Gradient = &BackgroundFillGradient{} | ||
return json.Unmarshal(data, bf.Gradient) | ||
case "freeform_gradient": | ||
bf.Type = BackgroundFillTypeFreeformGradient | ||
bf.FreeformGradient = &BackgroundFillFreeformGradient{} | ||
return json.Unmarshal(data, bf.FreeformGradient) | ||
} | ||
|
||
return fmt.Errorf("unsupported BackgroundFill type") | ||
} | ||
|
||
// BackgroundFillSolid https://core.telegram.org/bots/api#backgroundfillsolid | ||
type BackgroundFillSolid struct { | ||
Color int `json:"color"` | ||
} | ||
|
||
// BackgroundFillGradient https://core.telegram.org/bots/api#backgroundfillgradient | ||
type BackgroundFillGradient struct { | ||
TopColor int `json:"top_color"` | ||
BottomColor int `json:"bottom_color"` | ||
RotationAngle int `json:"rotation_angle"` | ||
} | ||
|
||
// BackgroundFillFreeformGradient https://core.telegram.org/bots/api#backgroundfillfreeformgradient | ||
type BackgroundFillFreeformGradient struct { | ||
Colors []int `json:"colors"` | ||
} |
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,102 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestChatBackground_UnmarshalJSON_fill(t *testing.T) { | ||
src := `{"type":"fill","fill":{"type":"solid","color":123},"dark_theme_dimming":2}` | ||
|
||
var cb ChatBackground | ||
err := json.Unmarshal([]byte(src), &cb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cb.Type != ChatBackgroundTypeFill { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cb.Fill == nil { | ||
t.Fatal("invalid Fill") | ||
} | ||
|
||
if cb.Fill.Fill.Type != BackgroundFillTypeSolid { | ||
t.Fatal("invalid fill type") | ||
} | ||
|
||
if cb.Fill.Fill.Solid.Color != 123 { | ||
t.Fatalf("invalid color %d, expect 123", cb.Fill.Fill.Solid.Color) | ||
} | ||
|
||
if cb.Fill.DarkThemeDimming != 2 { | ||
t.Fatalf("invalid dark theme dimming %d, expect 2", cb.Fill.DarkThemeDimming) | ||
} | ||
} | ||
|
||
func TestChatBackground_UnmarshalJSON_wallpaper(t *testing.T) { | ||
src := `{"type":"wallpaper","document":{"file_id":"test","file_unique_id":"test2"},"dark_theme_dimming":2,"is_blurred":true,"is_moving":true}` | ||
|
||
var cb ChatBackground | ||
err := json.Unmarshal([]byte(src), &cb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cb.Type != ChatBackgroundTypeWallpaper { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cb.Wallpaper == nil { | ||
t.Fatal("invalid Wallpaper") | ||
} | ||
|
||
if cb.Wallpaper.Document.FileID != "test" { | ||
t.Fatal("invalid document file id") | ||
} | ||
} | ||
|
||
func TestChatBackground_UnmarshalJSON_pattern(t *testing.T) { | ||
src := `{"type":"pattern","document":{"file_id":"test","file_unique_id":"test","file_size":123,"file_path":"test"},"fill":{"type":"solid","solid":{"color":123}},"intensity":1,"is_inverted":true,"is_moving":true}` | ||
|
||
var cb ChatBackground | ||
err := json.Unmarshal([]byte(src), &cb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cb.Type != ChatBackgroundTypePattern { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cb.Pattern == nil { | ||
t.Fatal("invalid Pattern") | ||
} | ||
|
||
if cb.Pattern.Document.FileID != "test" { | ||
t.Fatal("invalid document file id") | ||
} | ||
} | ||
|
||
func TestChatBackground_UnmarshalJSON_chat_theme(t *testing.T) { | ||
src := `{"type":"chat_theme","theme_name":"test"}` | ||
|
||
var cb ChatBackground | ||
err := json.Unmarshal([]byte(src), &cb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cb.Type != ChatBackgroundTypeChatTheme { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cb.Theme == nil { | ||
t.Fatal("invalid Theme") | ||
} | ||
|
||
if cb.Theme.ThemeName != "test" { | ||
t.Fatal("invalid theme name") | ||
} | ||
} |
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
Oops, something went wrong.