telegram-bot-api/configs.go

393 lines
8.7 KiB
Go
Raw Normal View History

2015-11-20 11:42:26 +01:00
package tgbotapi
import (
2015-11-20 12:19:37 +01:00
"encoding/json"
2015-11-20 11:42:26 +01:00
"io"
"net/url"
2015-11-20 12:06:51 +01:00
"strconv"
2015-11-20 11:42:26 +01:00
)
// Telegram constants
const (
// APIEndpoint is the endpoint for all API methods, with formatting for Sprintf
APIEndpoint = "https://api.telegram.org/bot%s/%s"
// FileEndpoint is the endpoint for downloading a file from Telegram
FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
)
// Constant values for ChatActions
const (
ChatTyping = "typing"
ChatUploadPhoto = "upload_photo"
ChatRecordVideo = "record_video"
ChatUploadVideo = "upload_video"
ChatRecordAudio = "record_audio"
ChatUploadAudio = "upload_audio"
ChatUploadDocument = "upload_document"
ChatFindLocation = "find_location"
)
// API errors
const (
// APIForbidden happens when a token is bad
APIForbidden = "forbidden"
)
// Constant values for ParseMode in MessageConfig
const (
ModeMarkdown = "Markdown"
)
// Base struct for all chat event(Message, Photo and so on)
type Chattable struct {
ChatID int
ChannelUsername string
}
2015-11-20 13:15:34 +01:00
type Fileable struct {
FilePath string
File interface{}
FileID string
}
2015-11-20 12:19:37 +01:00
func (chattable *Chattable) Values() (url.Values, error) {
2015-11-20 12:06:51 +01:00
v := url.Values{}
if chattable.ChannelUsername != "" {
v.Add("chat_id", chattable.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(chattable.ChatID))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// MessageConfig contains information about a SendMessage request.
type MessageConfig struct {
Chattable
Text string
ParseMode string
DisableWebPagePreview bool
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 12:06:51 +01:00
func (config *MessageConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("text", config.Text)
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
if config.ParseMode != "" {
v.Add("parse_mode", config.ParseMode)
}
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// ForwardConfig contains information about a ForwardMessage request.
type ForwardConfig struct {
Chattable
FromChatID int
FromChannelUsername string
MessageID int
}
2015-11-20 12:06:51 +01:00
func (config *ForwardConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
2015-11-20 11:42:26 +01:00
// PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
Caption string
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingPhoto bool
}
2015-11-20 12:06:51 +01:00
func (config *PhotoConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("photo", config.FileID)
if config.Caption != "" {
v.Add("caption", config.Caption)
}
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// AudioConfig contains information about a SendAudio request.
type AudioConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
Duration int
Performer string
Title string
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingAudio bool
}
2015-11-20 12:06:51 +01:00
func (config *AudioConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("audio", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
if config.Performer != "" {
v.Add("performer", config.Performer)
}
if config.Title != "" {
v.Add("title", config.Title)
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// DocumentConfig contains information about a SendDocument request.
type DocumentConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingDocument bool
}
2015-11-20 12:06:51 +01:00
func (config *DocumentConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("document", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// StickerConfig contains information about a SendSticker request.
type StickerConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingSticker bool
}
2015-11-20 12:06:51 +01:00
func (config *StickerConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("sticker", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// VideoConfig contains information about a SendVideo request.
type VideoConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
Duration int
Caption string
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingVideo bool
}
2015-11-20 12:06:51 +01:00
func (config *VideoConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("video", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}
if config.Caption != "" {
v.Add("caption", config.Caption)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct {
Chattable
2015-11-20 13:15:34 +01:00
Fileable
2015-11-20 11:42:26 +01:00
Duration int
ReplyToMessageID int
ReplyMarkup interface{}
UseExistingVoice bool
}
2015-11-20 12:06:51 +01:00
func (config *VoiceConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("voice", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// LocationConfig contains information about a SendLocation request.
type LocationConfig struct {
Chattable
Latitude float64
Longitude float64
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 12:06:51 +01:00
func (config *LocationConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
2015-11-20 12:19:37 +01:00
2015-11-20 12:06:51 +01:00
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil
}
2015-11-20 11:42:26 +01:00
// ChatActionConfig contains information about a SendChatAction request.
type ChatActionConfig struct {
Chattable
Action string
}
2015-11-20 12:06:51 +01:00
func (config *ChatActionConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("action", config.Action)
return v, nil
}
2015-11-20 11:42:26 +01:00
// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
type UserProfilePhotosConfig struct {
UserID int
Offset int
Limit int
}
// FileConfig has information about a file hosted on Telegram
type FileConfig struct {
FileID string
}
// UpdateConfig contains information about a GetUpdates request.
type UpdateConfig struct {
Offset int
Limit int
Timeout int
}
// WebhookConfig contains information about a SetWebhook request.
type WebhookConfig struct {
Clear bool
URL *url.URL
Certificate interface{}
}
// FileBytes contains information about a set of bytes to upload as a File.
type FileBytes struct {
Name string
Bytes []byte
}
// FileReader contains information about a reader to upload as a File.
// If Size is -1, it will read the entire Reader into memory to calculate a Size.
type FileReader struct {
Name string
Reader io.Reader
Size int64
}