Skip to content

Commit b6df6c2

Browse files
committed
Add params from develop branch, config for poll
1 parent bcbe534 commit b6df6c2

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

configs.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ type BaseChat struct {
7272
DisableNotification bool
7373
}
7474

75+
func (chat *BaseChat) params() (Params, error) {
76+
params := make(Params)
77+
78+
params.AddFirstValid("chat_id", chat.ChatID, chat.ChannelUsername)
79+
params.AddNonZero("reply_to_message_id", chat.ReplyToMessageID)
80+
params.AddBool("disable_notification", chat.DisableNotification)
81+
82+
err := params.AddInterface("reply_markup", chat.ReplyMarkup)
83+
84+
return params, err
85+
}
86+
7587
// values returns url.Values representation of BaseChat
7688
func (chat *BaseChat) values() (url.Values, error) {
7789
v := url.Values{}
@@ -765,6 +777,47 @@ func (config ContactConfig) method() string {
765777
return "sendContact"
766778
}
767779

780+
// SendPollConfig allows you to send a poll.
781+
type SendPollConfig struct {
782+
BaseChat
783+
Question string
784+
Options []string
785+
IsAnonymous bool
786+
Type string
787+
AllowsMultipleAnswers bool
788+
CorrectOptionID int64
789+
Explanation string
790+
ExplanationParseMode string
791+
OpenPeriod int
792+
CloseDate int
793+
IsClosed bool
794+
}
795+
796+
func (config SendPollConfig) values() (url.Values, error) {
797+
params, err := config.BaseChat.params()
798+
if err != nil {
799+
return params.toValues(), err
800+
}
801+
802+
params["question"] = config.Question
803+
err = params.AddInterface("options", config.Options)
804+
params["is_anonymous"] = strconv.FormatBool(config.IsAnonymous)
805+
params.AddNonEmpty("type", config.Type)
806+
params["allows_multiple_answers"] = strconv.FormatBool(config.AllowsMultipleAnswers)
807+
params["correct_option_id"] = strconv.FormatInt(config.CorrectOptionID, 10)
808+
params.AddBool("is_closed", config.IsClosed)
809+
params.AddNonEmpty("explanation", config.Explanation)
810+
params.AddNonEmpty("explanation_parse_mode", config.ExplanationParseMode)
811+
params.AddNonZero("open_period", config.OpenPeriod)
812+
params.AddNonZero("close_date", config.CloseDate)
813+
814+
return params.toValues(), err
815+
}
816+
817+
func (SendPollConfig) method() string {
818+
return "sendPoll"
819+
}
820+
768821
// GameConfig allows you to send a game.
769822
type GameConfig struct {
770823
BaseChat

params.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package tgbotapi
2+
3+
import (
4+
"encoding/json"
5+
"net/url"
6+
"reflect"
7+
"strconv"
8+
)
9+
10+
// Params represents a set of parameters that gets passed to a request.
11+
type Params map[string]string
12+
13+
func newParams(values url.Values) Params {
14+
params := Params{}
15+
for k, v := range values {
16+
if len(v) > 0 {
17+
params[k] = v[0]
18+
}
19+
}
20+
return params
21+
}
22+
23+
func (p Params) toValues() url.Values {
24+
values := url.Values{}
25+
for k, v := range p {
26+
values[k] = []string{v}
27+
}
28+
return values
29+
}
30+
31+
// AddNonEmpty adds a value if it not an empty string.
32+
func (p Params) AddNonEmpty(key, value string) {
33+
if value != "" {
34+
p[key] = value
35+
}
36+
}
37+
38+
// AddNonZero adds a value if it is not zero.
39+
func (p Params) AddNonZero(key string, value int) {
40+
if value != 0 {
41+
p[key] = strconv.Itoa(value)
42+
}
43+
}
44+
45+
// AddNonZero64 is the same as AddNonZero except uses an int64.
46+
func (p Params) AddNonZero64(key string, value int64) {
47+
if value != 0 {
48+
p[key] = strconv.FormatInt(value, 10)
49+
}
50+
}
51+
52+
// AddBool adds a value of a bool if it is true.
53+
func (p Params) AddBool(key string, value bool) {
54+
if value {
55+
p[key] = strconv.FormatBool(value)
56+
}
57+
}
58+
59+
// AddNonZeroFloat adds a floating point value that is not zero.
60+
func (p Params) AddNonZeroFloat(key string, value float64) {
61+
if value != 0 {
62+
p[key] = strconv.FormatFloat(value, 'f', 6, 64)
63+
}
64+
}
65+
66+
// AddInterface adds an interface if it is not nill and can be JSON marshalled.
67+
func (p Params) AddInterface(key string, value interface{}) error {
68+
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
69+
return nil
70+
}
71+
72+
b, err := json.Marshal(value)
73+
if err != nil {
74+
return err
75+
}
76+
77+
p[key] = string(b)
78+
79+
return nil
80+
}
81+
82+
// AddFirstValid attempts to add the first item that is not a default value.
83+
//
84+
// For example, AddFirstValid(0, "", "test") would add "test".
85+
func (p Params) AddFirstValid(key string, args ...interface{}) error {
86+
for _, arg := range args {
87+
switch v := arg.(type) {
88+
case int:
89+
if v != 0 {
90+
p[key] = strconv.Itoa(v)
91+
return nil
92+
}
93+
case int64:
94+
if v != 0 {
95+
p[key] = strconv.FormatInt(v, 10)
96+
return nil
97+
}
98+
case string:
99+
if v != "" {
100+
p[key] = v
101+
return nil
102+
}
103+
case nil:
104+
default:
105+
b, err := json.Marshal(arg)
106+
if err != nil {
107+
return err
108+
}
109+
110+
p[key] = string(b)
111+
return nil
112+
}
113+
}
114+
115+
return nil
116+
}

0 commit comments

Comments
 (0)