/
options.go
158 lines (133 loc) · 3.73 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package telepher
import (
"encoding/json"
"fmt"
"github.com/goTelegramBot/telepher/types"
"net/url"
"reflect"
"strconv"
"strings"
)
type Options struct {
ParseMode string
Entities []types.MessageEntity
DisableWebPagePreview bool
DisableNotification bool
ReplyToMessageID int
AllowSendingWithoutReply bool
ReplyMarkup *types.ReplyMarkup
Caption string
CaptionEntities []types.MessageEntity
Params url.Values
}
func (telepher Bot) addOptions(values url.Values, option interface{}) error {
switch option.(type) {
case url.Values:
for key := range option.(url.Values) {
values.Add(key, option.(url.Values).Get(key))
}
return nil
case *Options:
err := telepher.extractOptions(values, option.(*Options))
if err != nil {
return err
}
return nil
case map[string]string:
for key, value := range option.(map[string]string) {
values.Add(key, value)
}
return nil
case nil:
return nil
default:
return fmt.Errorf("unsupported options type %s", reflect.TypeOf(option))
}
}
func (telepher Bot) extractOptions(values url.Values, option *Options) error {
if option != nil {
if option.ParseMode == "" {
option.ParseMode = telepher.ParseMode
}
validParseMode := validParseMode(PARSE_MODES, strings.ToLower(option.ParseMode))
if validParseMode {
values.Add("parse_mode", option.ParseMode)
} else {
return fmt.Errorf("unsupported parse_mode %s", option.ParseMode)
}
values.Add("disable_web_page_preview", strconv.FormatBool(option.DisableWebPagePreview))
values.Add("disable_notification", strconv.FormatBool(option.DisableNotification))
if option.ReplyToMessageID != 0 {
values.Add("reply_to_message_id", strconv.Itoa(option.ReplyToMessageID))
}
if option.ReplyMarkup != nil {
err := addKeyboard(values, option.ReplyMarkup)
if err != nil {
return err
}
}
values.Add("caption", option.Caption)
/* if option.Thumb != nil {
switch option.Thumb.(type) {
case string:
values.Add("thumb", m)
case NamedReader:
values.Add("thumb", "attach://thumb")
data["thumb"] = m
case io.Reader:
values.Add("thumb", "attach://thumb")
data["thumb"] = NamedFile{File: m}
case []byte:
values.Add("thumb", "attach://thumb")
data["thumb"] = NamedFile{File: bytes.NewReader(m)}
default:
return nil, fmt.Errorf("unknown type for InputFile: %T", opts.Thumb)
}
}*/
if option.Entities != nil {
entities, err := json.Marshal(option.CaptionEntities)
if err != nil {
return fmt.Errorf("failed to marshal field caption_entities: %w", err)
}
values.Add("entities", string(entities))
}
if option.CaptionEntities != nil {
caption_entities, err := json.Marshal(option.CaptionEntities)
if err != nil {
return fmt.Errorf("failed to marshal field caption_entities: %w", err)
}
values.Add("caption_entities", string(caption_entities))
}
if option.Params != nil {
for key := range option.Params {
values.Add(key, option.Params.Get(key))
}
}
}
return nil
}
func validParseMode(array []string, item string) bool {
for i := range array {
if array[i] == item {
return true
}
}
return false
}
func addKeyboard(values url.Values, replyMarkup *types.ReplyMarkup) error {
if replyMarkup.InlineKeyboardMarkup != nil {
inline_keyboard_markup, err := json.Marshal(replyMarkup.InlineKeyboardMarkup)
if err != nil {
return err
}
values.Add("reply_markup", string(inline_keyboard_markup))
}
if replyMarkup.ReplyKeyboardMarkup != nil {
reply_keyboard_markup, err := json.Marshal(replyMarkup.ReplyKeyboardMarkup)
if err != nil {
return err
}
values.Add("reply_markup", string(reply_keyboard_markup))
}
return nil
}