all repos — telegram-bot-api @ da026b435e78db6e13663d1af658db812e868cdc

Golang bindings for the Telegram Bot API

Totally new, universal API
Gleb Sinyavsky zhulik.gleb@gmail.com
Fri, 20 Nov 2015 17:55:32 +0300
commit

da026b435e78db6e13663d1af658db812e868cdc

parent

d3f7ac7197622f37672b08a9fec9743132cbf802

2 files changed, 70 insertions(+), 136 deletions(-)

jump to
M bot.gobot.go

@@ -90,7 +90,7 @@

var message Message json.Unmarshal(resp.Result, &message) - bot.DebugLog(endpoint, params, message) + bot.debugLog(endpoint, params, message) return message, nil }

@@ -188,11 +188,16 @@

return user, nil } -func (bot *BotAPI) Send(c BaseChat) error { - return nil +func (bot *BotAPI) Send(c Chattable) (Message, error) { + switch c.(type) { + case Fileable: + return bot.sendFile(c.(Fileable)) + default: + return bot.sendChattable(c) + } } -func (bot *BotAPI) DebugLog(context string, v url.Values, message interface{}) { +func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { if bot.Debug { log.Printf("%s req : %+v\n", context, v) log.Printf("%s resp: %+v\n", context, message)

@@ -237,25 +242,21 @@

return message, nil } -func (bot *BotAPI) sendFile(method string, config Fileable) (Message, error) { +func (bot *BotAPI) sendFile(config Fileable) (Message, error) { if config.UseExistingFile() { - return bot.sendExisting(method, config) + return bot.sendExisting(config.Method(), config) } - return bot.uploadAndSend(method, config) + return bot.uploadAndSend(config.Method(), config) } -// SendMessage sends a Message to a chat. -// -// Requires ChatID and Text. -// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional. -func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { +func (bot *BotAPI) sendChattable(config Chattable) (Message, error) { v, err := config.Values() if err != nil { return Message{}, err } - message, err := bot.MakeMessageRequest("SendMessage", v) + message, err := bot.MakeMessageRequest(config.Method(), v) if err != nil { return Message{}, err

@@ -264,120 +265,6 @@

return message, nil } -// ForwardMessage forwards a message from one chat to another. -// -// Requires ChatID (destination), FromChatID (source), and MessageID. -func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { - v, err := config.Values() - if err != nil { - return Message{}, err - } - - message, err := bot.MakeMessageRequest("forwardMessage", v) - if err != nil { - return Message{}, err - } - - return message, nil -} - -// SendLocation sends a location to a chat. -// -// Requires ChatID, Latitude, and Longitude. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { - v, err := config.Values() - if err != nil { - return Message{}, err - } - - message, err := bot.MakeMessageRequest("sendLocation", v) - if err != nil { - return Message{}, err - } - - return message, nil -} - -// SendPhoto sends or uploads a photo to a chat. -// -// Requires ChatID and FileID OR File. -// Caption, ReplyToMessageID, and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { - return bot.sendFile("SendPhoto", config) -} - -// SendAudio sends or uploads an audio clip to a chat. -// If using a file, the file must be in the .mp3 format. -// -// When the fields title and performer are both empty and -// the mime-type of the file to be sent is not audio/mpeg, -// the file must be an .ogg file encoded with OPUS. -// You may use the tgutils.EncodeAudio func to assist you with this, if needed. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { - return bot.sendFile("sendAudio", config) -} - -// SendDocument sends or uploads a document to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { - return bot.sendFile("sendDocument", config) -} - -// SendVoice sends or uploads a playable voice to a chat. -// If using a file, the file must be encoded as an .ogg with OPUS. -// You may use the tgutils.EncodeAudio func to assist you with this, if needed. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { - return bot.sendFile("sendVoice", config) -} - -// SendSticker sends or uploads a sticker to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { - return bot.sendFile("sendSticker", config) -} - -// SendVideo sends or uploads a video to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { - return bot.sendFile("sendVideo", config) -} - -// SendChatAction sets a current action in a chat. -// -// Requires ChatID and a valid Action (see Chat constants). -func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { - v, err := config.Values() - if err != nil { - return err - } - - _, err = bot.MakeRequest("sendChatAction", v) - if err != nil { - return err - } - - return nil -} - // GetUserProfilePhotos gets a user's profile photos. // // Requires UserID.

@@ -400,7 +287,7 @@

var profilePhotos UserProfilePhotos json.Unmarshal(resp.Result, &profilePhotos) - bot.DebugLog("GetUserProfilePhoto", v, profilePhotos) + bot.debugLog("GetUserProfilePhoto", v, profilePhotos) return profilePhotos, nil }

@@ -420,7 +307,7 @@

var file File json.Unmarshal(resp.Result, &file) - bot.DebugLog("GetFile", v, file) + bot.debugLog("GetFile", v, file) return file, nil }

@@ -530,3 +417,20 @@

bot.Updates <- update }) } + +// SendChatAction sets a current action in a chat. +// +// Requires ChatID and a valid Action (see Chat constants). +func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { + v, err := config.Values() + if err != nil { + return err + } + + _, err = bot.MakeRequest("sendChatAction", v) + if err != nil { + return err + } + + return nil +}
M configs.goconfigs.go

@@ -40,6 +40,7 @@ )

type Chattable interface { Values() (url.Values, error) + Method() string } type Fileable interface {

@@ -133,6 +134,10 @@

return v, nil } +func (config MessageConfig) Method() string { + return "SendMessage" +} + // ForwardConfig contains information about a ForwardMessage request. type ForwardConfig struct { BaseChat

@@ -143,15 +148,12 @@ }

func (config ForwardConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() - - if config.FromChannelUsername != "" { - v.Add("chat_id", config.FromChannelUsername) - } else { - v.Add("chat_id", strconv.Itoa(config.FromChatID)) - } v.Add("message_id", strconv.Itoa(config.MessageID)) + return v, nil +} - return v, nil +func (config ForwardConfig) Method() string { + return "forwardMessage" } // PhotoConfig contains information about a SendPhoto request.

@@ -207,6 +209,10 @@ }

func (config PhotoConfig) Name() string { return "photo" +} + +func (config PhotoConfig) Method() string { + return "SendPhoto" } // AudioConfig contains information about a SendAudio request.

@@ -278,6 +284,10 @@ func (config AudioConfig) Name() string {

return "audio" } +func (config AudioConfig) Method() string { + return "SendAudio" +} + // DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile

@@ -324,6 +334,10 @@ }

func (config DocumentConfig) Name() string { return "document" +} + +func (config DocumentConfig) Method() string { + return "sendDocument" } // StickerConfig contains information about a SendSticker request.

@@ -374,6 +388,10 @@ func (config StickerConfig) Name() string {

return "sticker" } +func (config StickerConfig) Method() string { + return "sendSticker" +} + // VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile

@@ -430,6 +448,10 @@ func (config VideoConfig) Name() string {

return "viceo" } +func (config VideoConfig) Method() string { + return "sendVideo" +} + // VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile

@@ -485,6 +507,10 @@ func (config VoiceConfig) Name() string {

return "voice" } +func (config VoiceConfig) Method() string { + return "sendVoice" +} + // LocationConfig contains information about a SendLocation request. type LocationConfig struct { BaseChat

@@ -513,6 +539,10 @@ v.Add("reply_markup", string(data))

} return v, nil +} + +func (config LocationConfig) Method() string { + return "sendLocation" } // ChatActionConfig contains information about a SendChatAction request.