-
Notifications
You must be signed in to change notification settings - Fork 9
/
poll.go
220 lines (183 loc) 路 6.53 KB
/
poll.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package telego
import (
"errors"
objs "github.com/SakoDroid/telego/v2/objects"
)
// Polls contains the pointers to all of the created maps.
var Polls = make(map[string]*Poll)
// Poll is an automatic poll.
type Poll struct {
bot *Bot
chatIdInt, messageId, totalVoterCount int
id, question, pollType, explanation, explanationParseMode, chatIdString string
options []string
result []objs.PollOption
isClosed, isAnonymouse, allowMultipleAnswers bool
correctOptionId, openPeriod, closeDate int
updateChannel *chan bool
explanationEntities []objs.MessageEntity
}
/*
AddOption adds an option to poll.
-If the poll has been sent this method will do nothing.
*/
func (p *Poll) AddOption(option string) {
if p.id == "" {
p.options = append(p.options, option)
}
}
/*
SetExplanation sets explanation if this poll.
If the poll has been sent this method will do nothing.
*/
func (p *Poll) SetExplanation(explanation, explanationParseMode string, explanationEntities []objs.MessageEntity) {
if p.id == "" {
p.explanation = explanation
p.explanationParseMode = explanationParseMode
p.explanationEntities = explanationEntities
}
}
/*
SetCorrectOption sets the correct option id for the poll. The correct option is the index of the the true option in the options array.(0 based)
-If the type of this bot is "regular" this method will do nothing.
-If the poll has been sent this method will do nothing.
*/
func (p *Poll) SetCorrectOption(co int) {
if p.id == "" && p.pollType == "quiz" {
p.correctOptionId = co
}
}
/*
SetFlags sets the flags of this poll. Flags are "isClosed","isAnonymous" and "allowMultipleAnswers".
-If the poll has been sent this method will do nothing.
*/
func (p *Poll) SetFlags(isClosed, isAnonymous, allowMA bool) {
if p.id == "" {
p.isClosed = isClosed
p.isAnonymouse = isAnonymous
p.allowMultipleAnswers = allowMA
}
}
/*
SetOpenPeriod sets open period of this poll. According to official telegram doc, open period is amount of time in seconds the poll will be active after creation, 5-600. Can't be used together with close_date.
-If close date has been specified, this method wont set open period.
-If the poll has been sent this method will do nothing.
*/
func (p *Poll) SetOpenPeriod(op int) {
if p.id == "" && p.closeDate == 0 {
p.openPeriod = op
}
}
/*
SetCloseDate sets the close date of this poll.
-If open period has been specified, this method wont set close date for the poll.
-If the poll has been sent this method will do nothing.
*/
func (p *Poll) SetCloseDate(cd int) {
if p.id == "" && p.openPeriod == 0 {
p.closeDate = cd
}
}
/*
Update takes an poll object and extracts the poll update from it.
May return error if the update does not contain any poll or the poll in the update is not this poll
*/
func (p *Poll) Update(poll *objs.Poll) error {
if poll.Id == "" {
return errors.New("no poll found in the update")
}
if poll.Id != p.id {
return errors.New("this update dos not belong to this poll")
}
p.result = poll.Options
Polls[p.id] = p
*p.updateChannel <- true
return nil
}
/*GetType returns the poll type. Its either "regular" or "quiz".*/
func (p *Poll) GetType() string {
return p.pollType
}
/*GetId returns the id of this poll*/
func (p *Poll) GetId() string {
return p.id
}
/*GetQuestion returns the question of this poll*/
func (p *Poll) GetQuestion() string {
return p.question
}
/*GetExplanation returns the explanation of this poll*/
func (p *Poll) GetExplanation() string {
return p.explanation
}
/*GetOptions returns the options of this poll*/
func (p *Poll) GetOptions() []string {
return p.options
}
/*GetCorrectOption returns the correct option id. returnes 0 if type of the poll is "regular"*/
func (p *Poll) GetCorrectOption() int {
return p.correctOptionId
}
/*GetUpdateChannel returns the update channel for this poll. Everytime an update is received which contains update for this poll, true is passed into the channel.*/
func (p *Poll) GetUpdateChannel() *chan bool {
return p.updateChannel
}
/*GetResult returns the up to date result of the poll*/
func (p *Poll) GetResult() []objs.PollOption {
return p.result
}
/*GetTotalVoters returns the up to date total number of voters for this poll*/
func (p *Poll) GetTotalVoters() int {
return p.totalVoterCount
}
/*
Send sends the poll. If you want more options foe sending the bot, use "SendAdvanced" method.
If "silent" argument is true, the message will be sent without notification.
If "protectContent" argument is true, the message can't be forwarded or saved.
*/
func (p *Poll) Send(silent, protectContent bool, replyTo int) error {
res, err := p.bot.apiInterface.SendPoll(
p.chatIdInt, p.chatIdString, p.question, p.options, p.isClosed, p.isAnonymouse,
p.pollType, p.allowMultipleAnswers, p.correctOptionId, p.explanation, p.explanationParseMode,
p.explanationEntities, p.openPeriod, p.closeDate, replyTo,0, silent, false, protectContent, nil,
)
if err != nil {
return err
}
p.messageId = res.Result.MessageId
p.id = res.Result.Poll.Id
p.result = res.Result.Poll.Options
ch := make(chan bool)
p.updateChannel = &ch
Polls[p.id] = p
return nil
}
/*
SendAdvanced sends the poll. This method has more options than "Send" method.
If "silent" argument is true, the message will be sent without notification.
If "protectContent" argument is true, the message can't be forwarded or saved.
*/
func (p *Poll) SendAdvanced(replyTo, messageThreadId int, silent, allowSendingWithOutReply, protectContent bool, replyMarkup objs.ReplyMarkup) error {
res, err := p.bot.apiInterface.SendPoll(
p.chatIdInt, p.chatIdString, p.question, p.options, p.isClosed, p.isAnonymouse,
p.pollType, p.allowMultipleAnswers, p.correctOptionId, p.explanation, p.explanationParseMode,
p.explanationEntities, p.openPeriod, p.closeDate, replyTo, messageThreadId, silent, allowSendingWithOutReply, protectContent, replyMarkup,
)
if err != nil {
return err
}
p.messageId = res.Result.MessageId
p.id = res.Result.Poll.Id
p.result = res.Result.Poll.Options
ch := make(chan bool)
p.updateChannel = &ch
Polls[p.id] = p
return nil
}
/*Stop stops the poll*/
func (p *Poll) Stop() error {
_, err := p.bot.apiInterface.StopPoll(
p.chatIdInt, p.chatIdString, p.messageId, nil,
)
return err
}