-
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.
* API v7.0 * readme
- Loading branch information
Showing
21 changed files
with
1,123 additions
and
321 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,96 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// ChatBoostUpdated https://core.telegram.org/bots/api#chatboostupdated | ||
type ChatBoostUpdated struct { | ||
Chat Chat `json:"chat"` | ||
Boost ChatBoost `json:"boost"` | ||
} | ||
|
||
// ChatBoostRemoved https://core.telegram.org/bots/api#chatboostremoved | ||
type ChatBoostRemoved struct { | ||
Chat Chat `json:"chat"` | ||
BoostID string `json:"boost_id"` | ||
RemoveDate int `json:"remove_date"` | ||
Source ChatBoostSource `json:"source"` | ||
} | ||
|
||
// UserChatBoosts https://core.telegram.org/bots/api#userchatboosts | ||
type UserChatBoosts struct { | ||
Boosts []ChatBoost `json:"boosts"` | ||
} | ||
|
||
// ChatBoost https://core.telegram.org/bots/api#chatboost | ||
type ChatBoost struct { | ||
BoostID string `json:"boost_id"` | ||
AddDate int `json:"add_date"` | ||
ExpirationDate int `json:"expiration_date"` | ||
Source ChatBoostSource `json:"source"` | ||
} | ||
|
||
// ChatBoostSource https://core.telegram.org/bots/api#chatboostsource | ||
type ChatBoostSource struct { | ||
Source ChatBoostSourceType | ||
|
||
ChatBoostSourcePremium *ChatBoostSourcePremium | ||
ChatBoostSourceGiftCode *ChatBoostSourceGiftCode | ||
ChatBoostSourceGiveaway *ChatBoostSourceGiveaway | ||
} | ||
|
||
func (cbs *ChatBoostSource) UnmarshalJSON(data []byte) error { | ||
v := struct { | ||
Source string `json:"source"` | ||
}{} | ||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch v.Source { | ||
case "premium": | ||
cbs.Source = ChatBoostSourceTypePremium | ||
cbs.ChatBoostSourcePremium = &ChatBoostSourcePremium{} | ||
return json.Unmarshal(data, cbs.ChatBoostSourcePremium) | ||
case "gift_code": | ||
cbs.Source = ChatBoostSourceTypeGiftCode | ||
cbs.ChatBoostSourceGiftCode = &ChatBoostSourceGiftCode{} | ||
return json.Unmarshal(data, cbs.ChatBoostSourceGiftCode) | ||
case "giveaway": | ||
cbs.Source = ChatBoostSourceTypeGiveaway | ||
cbs.ChatBoostSourceGiveaway = &ChatBoostSourceGiveaway{} | ||
return json.Unmarshal(data, cbs.ChatBoostSourceGiveaway) | ||
} | ||
|
||
return fmt.Errorf("unsupported ChatBoostSource type") | ||
} | ||
|
||
// ChatBoostSourceType https://core.telegram.org/bots/api#chatboostsource | ||
type ChatBoostSourceType int | ||
|
||
const ( | ||
ChatBoostSourceTypePremium ChatBoostSourceType = iota | ||
ChatBoostSourceTypeGiftCode | ||
ChatBoostSourceTypeGiveaway | ||
) | ||
|
||
// ChatBoostSourcePremium https://core.telegram.org/bots/api#chatboostsourcepremium | ||
type ChatBoostSourcePremium struct { | ||
Source string `json:"source"` // always “premium” | ||
User User `json:"user"` | ||
} | ||
|
||
// ChatBoostSourceGiftCode https://core.telegram.org/bots/api#chatboostsourcegiftcode | ||
type ChatBoostSourceGiftCode struct { | ||
Source string `json:"source"` // always “gift_code” | ||
User User `json:"user"` | ||
} | ||
|
||
// ChatBoostSourceGiveaway https://core.telegram.org/bots/api#chatboostsourcegiveaway | ||
type ChatBoostSourceGiveaway struct { | ||
Source string `json:"source"` // always “giveaway” | ||
User User `json:"user"` | ||
} |
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,72 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestChatBoostSource_premium(t *testing.T) { | ||
src := `{"source":"premium","user":{"id":123}}` | ||
|
||
var cbs ChatBoostSource | ||
err := json.Unmarshal([]byte(src), &cbs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cbs.Source != ChatBoostSourceTypePremium { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cbs.ChatBoostSourcePremium == nil { | ||
t.Fatal("invalid ChatBoostSourcePremium") | ||
} | ||
|
||
if cbs.ChatBoostSourcePremium.User.ID != 123 { | ||
t.Fatal("invalid user id") | ||
} | ||
} | ||
|
||
func TestChatBoostSource_gift_code(t *testing.T) { | ||
src := `{"source":"gift_code","user":{"id":123}}` | ||
|
||
var cbs ChatBoostSource | ||
err := json.Unmarshal([]byte(src), &cbs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cbs.Source != ChatBoostSourceTypeGiftCode { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cbs.ChatBoostSourceGiftCode == nil { | ||
t.Fatal("invalid ChatBoostSourceGiftCode") | ||
} | ||
|
||
if cbs.ChatBoostSourceGiftCode.User.ID != 123 { | ||
t.Fatal("invalid user id") | ||
} | ||
} | ||
|
||
func TestChatBoostSource_giveaway(t *testing.T) { | ||
src := `{"source":"giveaway","user":{"id":123}}` | ||
|
||
var cbs ChatBoostSource | ||
err := json.Unmarshal([]byte(src), &cbs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cbs.Source != ChatBoostSourceTypeGiveaway { | ||
t.Fatal("invalid type") | ||
} | ||
|
||
if cbs.ChatBoostSourceGiveaway == nil { | ||
t.Fatal("invalid ChatBoostSourceGiveaway") | ||
} | ||
|
||
if cbs.ChatBoostSourceGiveaway.User.ID != 123 { | ||
t.Fatal("invalid user id") | ||
} | ||
} |
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
Oops, something went wrong.