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 BusinessConnectionID string
71}
72
73func (edit BaseEdit) params() (Params, error) {
74 params := make(Params)
75
76 if edit.InlineMessageID != "" {
77 params["inline_message_id"] = edit.InlineMessageID
78 } else {
79 p1, err := edit.BaseChatMessage.params()
80 if err != nil {
81 return params, err
82 }
83 params.Merge(p1)
84 }
85
86 err := params.AddInterface("reply_markup", edit.ReplyMarkup)
87 params.AddNonEmpty("business_connection_id", edit.BusinessConnectionID)
88
89 return params, err
90}
91
92// BaseSpoiler is base type of structures with spoilers.
93type BaseSpoiler struct {
94 HasSpoiler bool
95}
96
97func (spoiler BaseSpoiler) params() (Params, error) {
98 params := make(Params)
99
100 if spoiler.HasSpoiler {
101 params.AddBool("has_spoiler", true)
102 }
103
104 return params, nil
105}
106
107// BaseChatMessage is a base type for all messages in chats.
108type BaseChatMessage struct {
109 ChatConfig
110 MessageID int
111 BusinessConnectionID BusinessConnectionID
112}
113
114func (base BaseChatMessage) params() (Params, error) {
115 params, err := base.ChatConfig.params()
116 if err != nil {
117 return params, err
118 }
119 params.AddNonZero("message_id", base.MessageID)
120 err = params.AddInterface("business_connection_id", base.BusinessConnectionID)
121
122 return params, err
123}
124
125// BaseChatMessages is a base type for all messages in chats.
126type BaseChatMessages struct {
127 ChatConfig
128 MessageIDs []int
129}
130
131func (base BaseChatMessages) params() (Params, error) {
132 params, err := base.ChatConfig.params()
133 if err != nil {
134 return params, err
135 }
136 err = params.AddInterface("message_ids", base.MessageIDs)
137
138 return params, err
139}