all repos — telegram-bot-api @ b16df76f2eb1f7a91d953450401afa1ba4a93add

Golang bindings for the Telegram Bot API

Merge pull request #307 from go-telegram-bot-api/resolve-develop-conflicts

Resolve develop conflicts
Kirill Zhuharev zhuharev@users.noreply.github.com
Sat, 15 Feb 2020 17:17:37 +0300
commit

b16df76f2eb1f7a91d953450401afa1ba4a93add

parent

3635031d7473f14e6fbdbd8d2837bb26de087650

6 files changed, 342 insertions(+), 14 deletions(-)

jump to
M bot.gobot.go

@@ -27,6 +27,8 @@

Self User `json:"-"` Client *http.Client `json:"-"` shutdownChannel chan interface{} + + apiEndpoint string } // NewBotAPI creates a new BotAPI instance.

@@ -46,6 +48,8 @@ Token: token,

Client: client, Buffer: 100, shutdownChannel: make(chan interface{}), + + apiEndpoint: APIEndpoint, } self, err := bot.GetMe()

@@ -58,6 +62,10 @@

return bot, nil } +func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) { + b.apiEndpoint = apiEndpoint +} + func buildParams(in Params) (out url.Values) { if in == nil { return url.Values{}

@@ -78,7 +86,7 @@ if bot.Debug {

log.Printf("Endpoint: %s, params: %v\n", endpoint, params) } - method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) + method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) values := buildParams(params)

@@ -106,6 +114,7 @@ parameters = *apiResp.Parameters

} return apiResp, Error{ + Code: apiResp.ErrorCode, Message: apiResp.Description, ResponseParameters: parameters, }

@@ -199,7 +208,7 @@ if bot.Debug {

log.Printf("Endpoint: %s, fieldname: %s, params: %v, file: %T\n", endpoint, fieldname, params, file) } - method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) + method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) req, err := http.NewRequest("POST", method, nil) if err != nil {

@@ -439,6 +448,7 @@ ch := make(chan Update, bot.Buffer)

http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { bytes, _ := ioutil.ReadAll(r.Body) + r.Body.Close() var update Update json.Unmarshal(bytes, &update)
M go.modgo.mod

@@ -1,5 +1,8 @@

module github.com/go-telegram-bot-api/telegram-bot-api/v5 -require github.com/technoweenie/multipartstreamer v1.0.1 +require ( + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect + github.com/technoweenie/multipartstreamer v1.0.1 +) go 1.13
M go.sumgo.sum

@@ -1,2 +1,4 @@

+github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
M helpers.gohelpers.go

@@ -491,6 +491,15 @@ URL: url,

} } +// NewInlineQueryResultCachedGIF create a new inline query with cached photo. +func NewInlineQueryResultCachedGIF(id, gifID string) InlineQueryResultCachedGIF { + return InlineQueryResultCachedGIF{ + Type: "gif", + ID: id, + GifID: gifID, + } +} + // NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF. func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF { return InlineQueryResultMPEG4GIF{

@@ -500,6 +509,15 @@ URL: url,

} } +// NewInlineQueryResultCachedPhoto create a new inline query with cached photo. +func NewInlineQueryResultCachedMPEG4GIF(id, MPEG4GifID string) InlineQueryResultCachedMpeg4Gif { + return InlineQueryResultCachedMpeg4Gif{ + Type: "mpeg4_gif", + ID: id, + MGifID: MPEG4GifID, + } +} + // NewInlineQueryResultPhoto creates a new inline query photo. func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto { return InlineQueryResultPhoto{

@@ -519,6 +537,15 @@ ThumbURL: thumb,

} } +// NewInlineQueryResultCachedPhoto create a new inline query with cached photo. +func NewInlineQueryResultCachedPhoto(id, photoID string) InlineQueryResultCachedPhoto { + return InlineQueryResultCachedPhoto{ + Type: "photo", + ID: id, + PhotoID: photoID, + } +} + // NewInlineQueryResultVideo creates a new inline query video. func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo { return InlineQueryResultVideo{

@@ -528,6 +555,16 @@ URL: url,

} } +// NewInlineQueryResultCachedVideo create a new inline query with cached video. +func NewInlineQueryResultCachedVideo(id, videoID, title string) InlineQueryResultCachedVideo { + return InlineQueryResultCachedVideo{ + Type: "video", + ID: id, + VideoID: videoID, + Title: title, + } +} + // NewInlineQueryResultAudio creates a new inline query audio. func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio { return InlineQueryResultAudio{

@@ -538,6 +575,15 @@ Title: title,

} } +// NewInlineQueryResultCachedAudio create a new inline query with cached photo. +func NewInlineQueryResultCachedAudio(id, audioID string) InlineQueryResultCachedAudio { + return InlineQueryResultCachedAudio{ + Type: "audio", + ID: id, + AudioID: audioID, + } +} + // NewInlineQueryResultVoice creates a new inline query voice. func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice { return InlineQueryResultVoice{

@@ -548,6 +594,16 @@ Title: title,

} } +// NewInlineQueryResultCachedVoice create a new inline query with cached photo. +func NewInlineQueryResultCachedVoice(id, voiceID, title string) InlineQueryResultCachedVoice { + return InlineQueryResultCachedVoice{ + Type: "voice", + ID: id, + VoiceID: voiceID, + Title: title, + } +} + // NewInlineQueryResultDocument creates a new inline query document. func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument { return InlineQueryResultDocument{

@@ -559,12 +615,34 @@ MimeType: mimeType,

} } +// NewInlineQueryResultCachedDocument create a new inline query with cached photo. +func NewInlineQueryResultCachedDocument(id, documentID, title string) InlineQueryResultCachedDocument { + return InlineQueryResultCachedDocument{ + Type: "document", + ID: id, + DocumentID: documentID, + Title: title, + } +} + // NewInlineQueryResultLocation creates a new inline query location. func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation { return InlineQueryResultLocation{ Type: "location", ID: id, Title: title, + Latitude: latitude, + Longitude: longitude, + } +} + +// NewInlineQueryResultVenue creates a new inline query venue. +func NewInlineQueryResultVenue(id, title, address string, latitude, longitude float64) InlineQueryResultVenue { + return InlineQueryResultVenue{ + Type: "venue", + ID: id, + Title: title, + Address: address, Latitude: latitude, Longitude: longitude, }
M types.gotypes.go

@@ -216,7 +216,7 @@ return false

} entity := m.Entities[0] - return entity.Offset == 0 && entity.Type == "bot_command" + return entity.Offset == 0 && entity.IsCommand() } // Command checks if the message was a command and if it was, returns the

@@ -284,12 +284,62 @@ Language string `json:"language"` // optional

} // ParseURL attempts to parse a URL contained within a MessageEntity. -func (entity MessageEntity) ParseURL() (*url.URL, error) { - if entity.URL == "" { +func (e MessageEntity) ParseURL() (*url.URL, error) { + if e.URL == "" { return nil, errors.New(ErrBadURL) } - return url.Parse(entity.URL) + return url.Parse(e.URL) +} + +// IsMention returns true if the type of the message entity is "mention" (@username). +func (e MessageEntity) IsMention() bool { + return e.Type == "mention" +} + +// IsHashtag returns true if the type of the message entity is "hashtag". +func (e MessageEntity) IsHashtag() bool { + return e.Type == "hashtag" +} + +// IsCommand returns true if the type of the message entity is "bot_command". +func (e MessageEntity) IsCommand() bool { + return e.Type == "bot_command" +} + +// IsUrl returns true if the type of the message entity is "url". +func (e MessageEntity) IsUrl() bool { + return e.Type == "url" +} + +// IsEmail returns true if the type of the message entity is "email". +func (e MessageEntity) IsEmail() bool { + return e.Type == "email" +} + +// IsBold returns true if the type of the message entity is "bold" (bold text). +func (e MessageEntity) IsBold() bool { + return e.Type == "bold" +} + +// IsItalic returns true if the type of the message entity is "italic" (italic text). +func (e MessageEntity) IsItalic() bool { + return e.Type == "italic" +} + +// IsCode returns true if the type of the message entity is "code" (monowidth string). +func (e MessageEntity) IsCode() bool { + return e.Type == "code" +} + +// IsPre returns true if the type of the message entity is "pre" (monowidth block). +func (e MessageEntity) IsPre() bool { + return e.Type == "pre" +} + +// IsTextLink returns true if the type of the message entity is "text_link" (clickable text URL). +func (e MessageEntity) IsTextLink() bool { + return e.Type == "text_link" } // PhotoSize contains information about photos.

@@ -671,17 +721,42 @@ ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`

InputMessageContent interface{} `json:"input_message_content,omitempty"` } +// InlineQueryResultCachedPhoto is an inline query response with cached photo. +type InlineQueryResultCachedPhoto struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + PhotoID string `json:"photo_file_id"` // required + Title string `json:"title"` + Description string `json:"description"` + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultGIF is an inline query response GIF. type InlineQueryResultGIF struct { - Type string `json:"type"` // required - ID string `json:"id"` // required - URL string `json:"gif_url"` // required - Width int `json:"gif_width"` - Height int `json:"gif_height"` - Duration int `json:"gif_duration"` - ThumbURL string `json:"thumb_url"` + Type string `json:"type"` // required + ID string `json:"id"` // required + URL string `json:"gif_url"` // required + ThumbURL string `json:"thumb_url"` // required + Width int `json:"gif_width,omitempty"` + Height int `json:"gif_height,omitempty"` + Duration int `json:"gif_duration,omitempty"` + Title string `json:"title,omitempty"` + Caption string `json:"caption,omitempty"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + +// InlineQueryResultCachedGIF is an inline query response with cached gif. +type InlineQueryResultCachedGIF struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + GifID string `json:"gif_file_id"` // required Title string `json:"title"` Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` InputMessageContent interface{} `json:"input_message_content,omitempty"` }

@@ -701,6 +776,19 @@ ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`

InputMessageContent interface{} `json:"input_message_content,omitempty"` } +// InlineQueryResultCachedMpeg4Gif is an inline query response with cached +// H.264/MPEG-4 AVC video without sound gif. +type InlineQueryResultCachedMpeg4Gif struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + MGifID string `json:"mpeg4_file_id"` // required + Title string `json:"title"` + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultVideo is an inline query response video. type InlineQueryResultVideo struct { Type string `json:"type"` // required

@@ -718,6 +806,19 @@ ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`

InputMessageContent interface{} `json:"input_message_content,omitempty"` } +// InlineQueryResultCachedVideo is an inline query response with cached video. +type InlineQueryResultCachedVideo struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + VideoID string `json:"video_file_id"` // required + Title string `json:"title"` // required + Description string `json:"description"` + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultAudio is an inline query response audio. type InlineQueryResultAudio struct { Type string `json:"type"` // required

@@ -731,6 +832,17 @@ ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`

InputMessageContent interface{} `json:"input_message_content,omitempty"` } +// InlineQueryResultCachedAudio is an inline query response with cached audio. +type InlineQueryResultCachedAudio struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + AudioID string `json:"audio_file_id"` // required + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultVoice is an inline query response voice. type InlineQueryResultVoice struct { Type string `json:"type"` // required

@@ -743,6 +855,18 @@ ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`

InputMessageContent interface{} `json:"input_message_content,omitempty"` } +// InlineQueryResultCachedVoice is an inline query response with cached voice. +type InlineQueryResultCachedVoice struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + VoiceID string `json:"voice_file_id"` // required + Title string `json:"title"` // required + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultDocument is an inline query response document. type InlineQueryResultDocument struct { Type string `json:"type"` // required

@@ -759,6 +883,19 @@ ThumbWidth int `json:"thumb_width"`

ThumbHeight int `json:"thumb_height"` } +// InlineQueryResultCachedDocument is an inline query response with cached document. +type InlineQueryResultCachedDocument struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + DocumentID string `json:"document_file_id"` // required + Title string `json:"title"` // required + Caption string `json:"caption"` + Description string `json:"description"` + ParseMode string `json:"parse_mode"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` +} + // InlineQueryResultLocation is an inline query response location. type InlineQueryResultLocation struct { Type string `json:"type"` // required

@@ -782,6 +919,23 @@ PhoneNumber string `json:"phone_number"` // required

FirstName string `json:"first_name"` // required LastName string `json:"last_name"` VCard string `json:"vcard"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` + InputMessageContent interface{} `json:"input_message_content,omitempty"` + ThumbURL string `json:"thumb_url"` + ThumbWidth int `json:"thumb_width"` + ThumbHeight int `json:"thumb_height"` +} + +// InlineQueryResultVenue is an inline query response venue. +type InlineQueryResultVenue struct { + Type string `json:"type"` // required + ID string `json:"id"` // required + Latitude float64 `json:"latitude"` // required + Longitude float64 `json:"longitude"` // required + Title string `json:"title"` // required + Address string `json:"address"` // required + FoursquareID string `json:"foursquare_id"` + FoursquareType string `json:"foursquare_type"` ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` InputMessageContent interface{} `json:"input_message_content,omitempty"` ThumbURL string `json:"thumb_url"`

@@ -964,6 +1118,7 @@ }

// Error is an error containing extra information returned by the Telegram API. type Error struct { + Code int Message string ResponseParameters }
M types_test.gotypes_test.go

@@ -189,6 +189,86 @@ t.Fail()

} } +func TestMessageEntityIsMention(t *testing.T) { + entity := MessageEntity{Type: "mention"} + + if !entity.IsMention() { + t.Fail() + } +} + +func TestMessageEntityIsHashtag(t *testing.T) { + entity := MessageEntity{Type: "hashtag"} + + if !entity.IsHashtag() { + t.Fail() + } +} + +func TestMessageEntityIsBotCommand(t *testing.T) { + entity := MessageEntity{Type: "bot_command"} + + if !entity.IsCommand() { + t.Fail() + } +} + +func TestMessageEntityIsUrl(t *testing.T) { + entity := MessageEntity{Type: "url"} + + if !entity.IsUrl() { + t.Fail() + } +} + +func TestMessageEntityIsEmail(t *testing.T) { + entity := MessageEntity{Type: "email"} + + if !entity.IsEmail() { + t.Fail() + } +} + +func TestMessageEntityIsBold(t *testing.T) { + entity := MessageEntity{Type: "bold"} + + if !entity.IsBold() { + t.Fail() + } +} + +func TestMessageEntityIsItalic(t *testing.T) { + entity := MessageEntity{Type: "italic"} + + if !entity.IsItalic() { + t.Fail() + } +} + +func TestMessageEntityIsCode(t *testing.T) { + entity := MessageEntity{Type: "code"} + + if !entity.IsCode() { + t.Fail() + } +} + +func TestMessageEntityIsPre(t *testing.T) { + entity := MessageEntity{Type: "pre"} + + if !entity.IsPre() { + t.Fail() + } +} + +func TestMessageEntityIsTextLink(t *testing.T) { + entity := MessageEntity{Type: "text_link"} + + if !entity.IsTextLink() { + t.Fail() + } +} + func TestFileLink(t *testing.T) { file := File{FilePath: "test/test.txt"}