all repos — telegram-bot-api @ bece330506ef0674ff37c1bc068794f18c1b4bd9

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}
110
111func (base BaseChatMessage) params() (Params, error) {
112	params, err := base.ChatConfig.params()
113	if err != nil {
114		return params, err
115	}
116	params.AddNonZero("message_id", base.MessageID)
117
118	return params, nil
119}
120
121// BaseChatMessages is a base type for all messages in chats.
122type BaseChatMessages struct {
123	ChatConfig
124	MessageIDs []int
125}
126
127func (base BaseChatMessages) params() (Params, error) {
128	params, err := base.ChatConfig.params()
129	if err != nil {
130		return params, err
131	}
132	err = params.AddInterface("message_ids", base.MessageIDs)
133
134	return params, err
135}