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 MessageThreadID int
23 ProtectContent bool
24 ReplyMarkup interface{}
25 DisableNotification bool
26 ReplyParameters ReplyParameters
27}
28
29func (chat *BaseChat) params() (Params, error) {
30 params, err := chat.ChatConfig.params()
31 if err != nil {
32 return params, err
33 }
34
35 params.AddNonZero("message_thread_id", chat.MessageThreadID)
36 params.AddBool("disable_notification", chat.DisableNotification)
37 params.AddBool("protect_content", chat.ProtectContent)
38
39 err = params.AddInterface("reply_markup", chat.ReplyMarkup)
40 if err != nil {
41 return params, err
42 }
43 err = params.AddInterface("reply_parameters", chat.ReplyParameters)
44 return params, err
45}
46
47// BaseFile is a base type for all file config types.
48type BaseFile struct {
49 BaseChat
50 File RequestFileData
51}
52
53func (file BaseFile) params() (Params, error) {
54 return file.BaseChat.params()
55}
56
57// BaseEdit is base type of all chat edits.
58type BaseEdit struct {
59 BaseChatMessage
60 InlineMessageID string
61 ReplyMarkup *InlineKeyboardMarkup
62}
63
64func (edit BaseEdit) params() (Params, error) {
65 params := make(Params)
66
67 if edit.InlineMessageID != "" {
68 params["inline_message_id"] = edit.InlineMessageID
69 } else {
70 p1, err := edit.BaseChatMessage.params()
71 if err != nil {
72 return params, err
73 }
74 params.Merge(p1)
75 }
76
77 err := params.AddInterface("reply_markup", edit.ReplyMarkup)
78
79 return params, err
80}
81
82// BaseSpoiler is base type of structures with spoilers.
83type BaseSpoiler struct {
84 HasSpoiler bool
85}
86
87func (spoiler BaseSpoiler) params() (Params, error) {
88 params := make(Params)
89
90 if spoiler.HasSpoiler {
91 params.AddBool("has_spoiler", true)
92 }
93
94 return params, nil
95}
96
97// BaseChatMessage is a base type for all messages in chats.
98type BaseChatMessage struct {
99 ChatConfig
100 MessageID int
101}
102
103func (base BaseChatMessage) params() (Params, error) {
104 params, err := base.ChatConfig.params()
105 if err != nil {
106 return params, err
107 }
108 params.AddNonZero("message_id", base.MessageID)
109
110 return params, nil
111}
112
113// BaseChatMessages is a base type for all messages in chats.
114type BaseChatMessages struct {
115 ChatConfig
116 MessageIDs []int
117}
118
119func (base BaseChatMessages) params() (Params, error) {
120 params, err := base.ChatConfig.params()
121 if err != nil {
122 return params, err
123 }
124 err = params.AddInterface("message_ids", base.MessageIDs)
125
126 return params, err
127}