all repos — telegram-bot-api @ f9ab9c0a4df305dcfc111e96a8b92c6fda442a39

Golang bindings for the Telegram Bot API

helper_structs.go (view raw)

  1package tgbotapi
  2
  3// ChatConfig is a base type for all chat identifiers
  4type ChatConfig struct {
  5	ChatID             int64
  6	ChannelUsername    string
  7	SuperGroupUsername string
  8}
  9
 10func (base ChatConfig) params() (Params, error) {
 11	return base.paramsWithKey("chat_id")
 12}
 13
 14func (base ChatConfig) paramsWithKey(key string) (Params, error) {
 15	params := make(Params)
 16	return params, params.AddFirstValid(key, base.ChatID, base.ChannelUsername, base.SuperGroupUsername)
 17}
 18
 19// BaseChat is base type for all chat config types.
 20type BaseChat struct {
 21	ChatConfig
 22	BusinessConnectionID BusinessConnectionID
 23	MessageThreadID      int
 24	ProtectContent       bool
 25	ReplyMarkup          interface{}
 26	DisableNotification  bool
 27	MessageEffectID      string // for private chats only
 28	ReplyParameters      ReplyParameters
 29}
 30
 31func (chat *BaseChat) params() (Params, error) {
 32	params, err := chat.ChatConfig.params()
 33	if err != nil {
 34		return params, err
 35	}
 36	p1, err := chat.BusinessConnectionID.params()
 37	if err != nil {
 38		return params, err
 39	}
 40	params.Merge(p1)
 41
 42	params.AddNonZero("message_thread_id", chat.MessageThreadID)
 43	params.AddBool("disable_notification", chat.DisableNotification)
 44	params.AddBool("protect_content", chat.ProtectContent)
 45	params.AddNonEmpty("message_effect_id", chat.MessageEffectID)
 46
 47	err = params.AddInterface("reply_markup", chat.ReplyMarkup)
 48	if err != nil {
 49		return params, err
 50	}
 51	err = params.AddInterface("reply_parameters", chat.ReplyParameters)
 52	return params, err
 53}
 54
 55// BaseFile is a base type for all file config types.
 56type BaseFile struct {
 57	BaseChat
 58	File RequestFileData
 59}
 60
 61func (file BaseFile) params() (Params, error) {
 62	return file.BaseChat.params()
 63}
 64
 65// BaseEdit is base type of all chat edits.
 66type BaseEdit struct {
 67	BaseChatMessage
 68	InlineMessageID      string
 69	ReplyMarkup          *InlineKeyboardMarkup
 70}
 71
 72func (edit BaseEdit) params() (Params, error) {
 73	params := make(Params)
 74
 75	if edit.InlineMessageID != "" {
 76		params["inline_message_id"] = edit.InlineMessageID
 77	} else {
 78		p1, err := edit.BaseChatMessage.params()
 79		if err != nil {
 80			return params, err
 81		}
 82		params.Merge(p1)
 83	}
 84
 85	err := params.AddInterface("reply_markup", edit.ReplyMarkup)
 86
 87	return params, err
 88}
 89
 90// BaseSpoiler is base type of structures with spoilers.
 91type BaseSpoiler struct {
 92	HasSpoiler bool
 93}
 94
 95func (spoiler BaseSpoiler) params() (Params, error) {
 96	params := make(Params)
 97
 98	if spoiler.HasSpoiler {
 99		params.AddBool("has_spoiler", true)
100	}
101
102	return params, nil
103}
104
105// BaseChatMessage is a base type for all messages in chats.
106type BaseChatMessage struct {
107	ChatConfig
108	MessageID            int
109	BusinessConnectionID BusinessConnectionID
110}
111
112func (base BaseChatMessage) params() (Params, error) {
113	params, err := base.ChatConfig.params()
114	if err != nil {
115		return params, err
116	}
117	p1, err := base.BusinessConnectionID.params()
118	if err != nil {
119		return params, err
120	}
121	params.Merge(p1)
122	params.AddNonZero("message_id", base.MessageID)
123
124	return params, err
125}
126
127// BaseChatMessages is a base type for all messages in chats.
128type BaseChatMessages struct {
129	ChatConfig
130	MessageIDs []int
131}
132
133func (base BaseChatMessages) params() (Params, error) {
134	params, err := base.ChatConfig.params()
135	if err != nil {
136		return params, err
137	}
138	err = params.AddInterface("message_ids", base.MessageIDs)
139
140	return params, err
141}