Partial refactoring of internals.

- GetChat and LeaveChat handle error descriptions
- Documentation fix for Result type
- Wrapping structs for IQRs go inside marshal funcs.
pull/25/merge
Ian Byrd 8 years ago
parent 3a6f49efc1
commit e0d2195801
No known key found for this signature in database
GPG Key ID: 598F598CA3B8055F

@ -651,7 +651,7 @@ func (b *Bot) GetFile(fileID string) (File, error) {
return responseRecieved.Result, nil
}
// LeaveChat , Use this method for your bot to leave a group, supergroup or channel. Returns True on success.
// LeaveChat makes bot leave a group, supergroup or channel.
func (b *Bot) LeaveChat(recipient Recipient) error {
params := map[string]string{
"chat_id": recipient.Destination(),
@ -662,8 +662,9 @@ func (b *Bot) LeaveChat(recipient Recipient) error {
}
var responseRecieved struct {
Ok bool
Result bool
Ok bool
Description string
Result bool
}
err = json.Unmarshal(responseJSON, &responseRecieved)
@ -672,15 +673,17 @@ func (b *Bot) LeaveChat(recipient Recipient) error {
}
if !responseRecieved.Ok {
return fmt.Errorf("telebot: leaveChat failure %s", responseRecieved.Result)
return fmt.Errorf("telebot: leaveChat failure %s",
responseRecieved.Description)
}
return nil
}
// GetChat get up to date information
// about the chat (current name of the user for one-on-one
// conversations, current username of a user, group or channel, etc.).
// GetChat get up to date information about the chat.
//
// Including current name of the user for one-on-one conversations,
// current username of a user, group or channel, etc.
//
// Returns a Chat object on success.
func (b *Bot) GetChat(recipient Recipient) (Chat, error) {
@ -693,8 +696,9 @@ func (b *Bot) GetChat(recipient Recipient) (Chat, error) {
}
var responseRecieved struct {
Ok bool
Result Chat
Ok bool
Description string
Result Chat
}
err = json.Unmarshal(responseJSON, &responseRecieved)
@ -703,7 +707,8 @@ func (b *Bot) GetChat(recipient Recipient) (Chat, error) {
}
if !responseRecieved.Ok {
return Chat{}, fmt.Errorf("telebot: getChat failure %s", responseRecieved.Result)
return Chat{}, fmt.Errorf("telebot: getChat failure %s",
responseRecieved.Description)
}
return responseRecieved.Result, nil

@ -1,9 +1,10 @@
package telebot
import (
"github.com/mitchellh/hashstructure"
"hash/fnv"
"strconv"
"github.com/mitchellh/hashstructure"
)
// inlineQueryHashOptions sets the HashOptions to be used when hashing
@ -83,7 +84,7 @@ func hashInlineQueryResult(result InlineQueryResult) (string, error) {
return strconv.FormatUint(hash, 16), nil
}
// Deprecated interface type, superseded by InlineQueryResult.
// Result is a deprecated type, superseded by InlineQueryResult.
type Result interface {
MarshalJSON() ([]byte, error)
}

@ -44,31 +44,31 @@ type InlineQueryResultArticle struct {
ThumbHeight int `json:"thumb_height,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultArticle InlineQueryResultArticle
func (r *InlineQueryResultArticle) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultArticle
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultArticle
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultArticle: wrappedInlineQueryResultArticle(*r),
ID: id,
Type: "article",
wrapper: wrapper(*r),
ID: id,
Type: "article",
})
}
func (r *InlineQueryResultArticle) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultAudio represents a link to an mp3 audio file.
@ -97,32 +97,32 @@ type InlineQueryResultAudio struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultAudio InlineQueryResultAudio
func (r *InlineQueryResultAudio) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultAudio
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultAudio
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultAudio: wrappedInlineQueryResultAudio(*r),
ID: id,
Type: "audio",
wrapper: wrapper(*r),
ID: id,
Type: "audio",
})
}
func (r *InlineQueryResultAudio) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultContact represents a contact with a phone number.
@ -158,32 +158,32 @@ type InlineQueryResultContact struct {
ThumbHeight int `json:"thumb_height,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultContact InlineQueryResultContact
func (r *InlineQueryResultContact) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultContact
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultContact
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultContact: wrappedInlineQueryResultContact(*r),
ID: id,
Type: "contact",
wrapper: wrapper(*r),
ID: id,
Type: "contact",
})
}
func (r *InlineQueryResultContact) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultDocument represents a link to a file.
@ -226,32 +226,32 @@ type InlineQueryResultDocument struct {
ThumbHeight int `json:"thumb_height,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultDocument InlineQueryResultDocument
func (r *InlineQueryResultDocument) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultDocument
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultDocument
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultDocument: wrappedInlineQueryResultDocument(*r),
ID: id,
Type: "document",
wrapper: wrapper(*r),
ID: id,
Type: "document",
})
}
func (r *InlineQueryResultDocument) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultGif represents a link to an animated GIF file.
@ -287,31 +287,32 @@ type InlineQueryResultGif struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultGif InlineQueryResultGif
func (r *InlineQueryResultGif) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultGif
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultGif
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultGif: wrappedInlineQueryResultGif(*r),
ID: id,
Type: "gif",
wrapper: wrapper(*r),
ID: id,
Type: "gif",
})
}
func (r *InlineQueryResultGif) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultLocation represents a location on a map.
@ -348,31 +349,32 @@ type InlineQueryResultLocation struct {
ThumbHeight int `json:"thumb_height,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultLocation InlineQueryResultLocation
func (r *InlineQueryResultLocation) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultLocation
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultLocation
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultLocation: wrappedInlineQueryResultLocation(*r),
ID: id,
Type: "location",
wrapper: wrapper(*r),
ID: id,
Type: "location",
})
}
func (r *InlineQueryResultLocation) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultMpeg4Gif represents a link to a video animation
@ -409,31 +411,32 @@ type InlineQueryResultMpeg4Gif struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultMpeg4Gif InlineQueryResultMpeg4Gif
func (r *InlineQueryResultMpeg4Gif) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultMpeg4Gif
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultMpeg4Gif
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultMpeg4Gif: wrappedInlineQueryResultMpeg4Gif(*r),
ID: id,
Type: "mpeg4_gif",
wrapper: wrapper(*r),
ID: id,
Type: "mpeg4_gif",
})
}
func (r *InlineQueryResultMpeg4Gif) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultPhoto represents a link to a photo.
@ -473,31 +476,32 @@ type InlineQueryResultPhoto struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultPhoto InlineQueryResultPhoto
func (r *InlineQueryResultPhoto) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultPhoto
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultPhoto
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultPhoto: wrappedInlineQueryResultPhoto(*r),
ID: id,
Type: "photo",
wrapper: wrapper(*r),
ID: id,
Type: "photo",
})
}
func (r *InlineQueryResultPhoto) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultVenue represents a venue.
@ -539,31 +543,32 @@ type InlineQueryResultVenue struct {
ThumbHeight int `json:"thumb_height,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultVenue InlineQueryResultVenue
func (r *InlineQueryResultVenue) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultVenue
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultVenue
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultVenue: wrappedInlineQueryResultVenue(*r),
ID: id,
Type: "venue",
wrapper: wrapper(*r),
ID: id,
Type: "venue",
})
}
func (r *InlineQueryResultVenue) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultVideo represents a link to a page containing an embedded
@ -609,31 +614,32 @@ type InlineQueryResultVideo struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultVideo InlineQueryResultVideo
func (r *InlineQueryResultVideo) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultVideo
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultVideo
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultVideo: wrappedInlineQueryResultVideo(*r),
ID: id,
Type: "video",
wrapper: wrapper(*r),
ID: id,
Type: "video",
})
}
func (r *InlineQueryResultVideo) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}
// InlineQueryResultVoice represents a link to a voice recording in a
@ -662,29 +668,30 @@ type InlineQueryResultVoice struct {
InputMessageContent InputMessageContent `json:"input_message_content,omitempty"`
}
// Used to avoid endless recursion in MarshalJSON.
type wrappedInlineQueryResultVoice InlineQueryResultVoice
func (r *InlineQueryResultVoice) MarshalJSON() ([]byte, error) {
// avoiding endless self-recursion
type wrapper InlineQueryResultVoice
id, err := r.id()
if err != nil {
return nil, err
}
return json.Marshal(struct {
wrappedInlineQueryResultVoice
wrapper
Type string `json:"type"`
ID string `json:"id",hash:"ignore"`
}{
wrappedInlineQueryResultVoice: wrappedInlineQueryResultVoice(*r),
ID: id,
Type: "voice",
wrapper: wrapper(*r),
ID: id,
Type: "voice",
})
}
func (r *InlineQueryResultVoice) id() (string, error) {
if r.ID == "" {
return hashInlineQueryResult(r)
} else {
return r.ID, nil
}
return r.ID, nil
}

Loading…
Cancel
Save