telegram-bot-api/configs.go

599 lines
13 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"
)
2015-11-20 15:08:53 +01:00
type Chattable interface {
Values() (url.Values, error)
2015-11-20 15:55:32 +01:00
Method() string
2015-11-20 15:08:53 +01:00
}
type Fileable interface {
Chattable
Params() (map[string]string, error)
2015-11-20 15:31:01 +01:00
Name() string
2015-11-20 15:08:53 +01:00
GetFile() interface{}
2015-11-20 15:31:01 +01:00
UseExistingFile() bool
2015-11-20 15:08:53 +01:00
}
2015-11-20 11:42:26 +01:00
// Base struct for all chat event(Message, Photo and so on)
2015-11-20 15:08:53 +01:00
type BaseChat struct {
2015-11-20 11:42:26 +01:00
ChatID int
ChannelUsername string
}
2015-11-20 15:08:53 +01:00
func (chat *BaseChat) Values() (url.Values, error) {
2015-11-20 12:06:51 +01:00
v := url.Values{}
2015-11-20 15:08:53 +01:00
if chat.ChannelUsername != "" {
v.Add("chat_id", chat.ChannelUsername)
2015-11-20 12:06:51 +01:00
} else {
2015-11-20 15:08:53 +01:00
v.Add("chat_id", strconv.Itoa(chat.ChatID))
2015-11-20 12:06:51 +01:00
}
return v, nil
}
2015-11-20 15:08:53 +01:00
type BaseFile struct {
BaseChat
FilePath string
File interface{}
FileID string
UseExisting bool
}
func (file BaseFile) Params() (map[string]string, error) {
params := make(map[string]string)
if file.ChannelUsername != "" {
params["chat_id"] = file.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(file.ChatID)
}
return params, nil
}
func (file BaseFile) GetFile() interface{} {
var result interface{}
if file.FilePath == "" {
result = file.File
} else {
result = file.FilePath
}
return result
}
2015-11-20 15:31:01 +01:00
func (file BaseFile) UseExistingFile() bool {
return file.UseExisting
}
2015-11-20 11:42:26 +01:00
// MessageConfig contains information about a SendMessage request.
type MessageConfig struct {
2015-11-20 15:08:53 +01:00
BaseChat
2015-11-20 11:42:26 +01:00
Text string
ParseMode string
DisableWebPagePreview bool
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config MessageConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:55:32 +01:00
func (config MessageConfig) Method() string {
return "SendMessage"
}
2015-11-20 11:42:26 +01:00
// ForwardConfig contains information about a ForwardMessage request.
type ForwardConfig struct {
2015-11-20 15:08:53 +01:00
BaseChat
2015-11-20 11:42:26 +01:00
FromChatID int
FromChannelUsername string
MessageID int
}
2015-11-20 15:08:53 +01:00
func (config ForwardConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
v.Add("message_id", strconv.Itoa(config.MessageID))
return v, nil
}
2015-11-20 15:55:32 +01:00
func (config ForwardConfig) Method() string {
return "forwardMessage"
}
2015-11-20 11:42:26 +01:00
// PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
2015-11-20 11:42:26 +01:00
Caption string
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config PhotoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.Caption != "" {
params["caption"] = config.Caption
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
func (config PhotoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:31:01 +01:00
func (config PhotoConfig) Name() string {
return "photo"
}
2015-11-20 15:55:32 +01:00
func (config PhotoConfig) Method() string {
return "SendPhoto"
}
2015-11-20 11:42:26 +01:00
// AudioConfig contains information about a SendAudio request.
type AudioConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
2015-11-20 11:42:26 +01:00
Duration int
Performer string
Title string
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config AudioConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:08:53 +01:00
func (config AudioConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
if config.Performer != "" {
params["performer"] = config.Performer
}
if config.Title != "" {
params["title"] = config.Title
}
return params, nil
}
2015-11-20 15:31:01 +01:00
func (config AudioConfig) Name() string {
return "audio"
}
2015-11-20 15:55:32 +01:00
func (config AudioConfig) Method() string {
return "SendAudio"
}
2015-11-20 11:42:26 +01:00
// DocumentConfig contains information about a SendDocument request.
type DocumentConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
2015-11-20 11:42:26 +01:00
}
2015-11-20 15:08:53 +01:00
func (config DocumentConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:08:53 +01:00
func (config DocumentConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
2015-11-20 15:31:01 +01:00
func (config DocumentConfig) Name() string {
return "document"
}
2015-11-20 15:55:32 +01:00
func (config DocumentConfig) Method() string {
return "sendDocument"
}
2015-11-20 11:42:26 +01:00
// StickerConfig contains information about a SendSticker request.
type StickerConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
2015-11-20 11:42:26 +01:00
}
2015-11-20 15:08:53 +01:00
func (config StickerConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:08:53 +01:00
func (config StickerConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
2015-11-20 15:31:01 +01:00
func (config StickerConfig) Name() string {
return "sticker"
}
2015-11-20 15:55:32 +01:00
func (config StickerConfig) Method() string {
return "sendSticker"
}
2015-11-20 11:42:26 +01:00
// VideoConfig contains information about a SendVideo request.
type VideoConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
2015-11-20 11:42:26 +01:00
Duration int
Caption string
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config VideoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:08:53 +01:00
func (config VideoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
2015-11-20 15:31:01 +01:00
func (config VideoConfig) Name() string {
return "viceo"
}
2015-11-20 15:55:32 +01:00
func (config VideoConfig) Method() string {
return "sendVideo"
}
2015-11-20 11:42:26 +01:00
// VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct {
2015-11-20 15:08:53 +01:00
BaseFile
2015-11-20 11:42:26 +01:00
Duration int
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config VoiceConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:08:53 +01:00
func (config VoiceConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
2015-11-20 15:31:01 +01:00
func (config VoiceConfig) Name() string {
return "voice"
}
2015-11-20 15:55:32 +01:00
func (config VoiceConfig) Method() string {
return "sendVoice"
}
2015-11-20 11:42:26 +01:00
// LocationConfig contains information about a SendLocation request.
type LocationConfig struct {
2015-11-20 15:08:53 +01:00
BaseChat
2015-11-20 11:42:26 +01:00
Latitude float64
Longitude float64
ReplyToMessageID int
ReplyMarkup interface{}
}
2015-11-20 15:08:53 +01:00
func (config LocationConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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 15:55:32 +01:00
func (config LocationConfig) Method() string {
return "sendLocation"
}
2015-11-20 11:42:26 +01:00
// ChatActionConfig contains information about a SendChatAction request.
type ChatActionConfig struct {
2015-11-20 15:08:53 +01:00
BaseChat
2015-11-20 11:42:26 +01:00
Action string
}
2015-11-20 15:08:53 +01:00
func (config ChatActionConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
2015-11-20 12:06:51 +01:00
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
}