This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_update.go
172 lines (148 loc) Β· 4.4 KB
/
utils_update.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
package telegram
import (
"bytes"
"log"
"time"
"github.com/kirillDanshin/dlog"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// UpdatesChannel is a channel for reading updates of bot.
type UpdatesChannel <-chan Update
// NewLongPollingChannel creates channel for receive incoming updates using long
// polling.
func (bot *Bot) NewLongPollingChannel(params *GetUpdatesParameters) UpdatesChannel {
if params == nil {
params = &GetUpdatesParameters{
Offset: 0,
Limit: 100,
Timeout: 60,
}
}
channel := make(chan Update, params.Limit)
go func() {
for {
updates, err := bot.GetUpdates(params)
if err != nil {
dlog.Ln(err.Error())
dlog.Ln("Failed to get updates, retrying in 3 seconds...")
time.Sleep(time.Second * 3)
continue
}
for _, update := range updates {
if update.ID >= params.Offset {
params.Offset = update.ID + 1
channel <- update
}
}
}
}()
return channel
}
// NewWebhookChannel creates channel for receive incoming updates via an outgoing
// webhook.
func (bot *Bot) NewWebhookChannel(params *SetWebhookParameters, certFile, keyFile, set, listen, serve string) (updates UpdatesChannel) {
if params == nil {
params = &SetWebhookParameters{
URL: set,
MaxConnections: 40,
}
}
var err error
channel := make(chan Update, 100)
requiredPath := []byte(listen)
dlog.Ln("requiredPath:", string(requiredPath))
handleFunc := func(ctx *http.RequestCtx) {
dlog.Ln("Request path:", string(ctx.Path()))
if !bytes.HasPrefix(ctx.Path(), requiredPath) {
dlog.Ln("Unsupported request path:", string(ctx.Path()))
return
}
dlog.Ln("Catched supported request path:", string(ctx.Path()))
var update Update
if err = json.Unmarshal(ctx.Request.Body(), &update); err != nil {
return
}
channel <- update
}
go func() {
if certFile != "" && keyFile != "" {
dlog.Ln("Creating TLS router...")
err = http.ListenAndServeTLS(serve, certFile, keyFile, handleFunc)
} else {
dlog.Ln("Creating simple router...")
err = http.ListenAndServe(serve, handleFunc)
}
if err != nil {
log.Fatalln(err.Error())
}
}()
if _, err = bot.SetWebhook(params); err != nil {
log.Fatalln(err.Error())
}
return channel
}
// IsMessage checks that the current update is a message creation event.
func (upd *Update) IsMessage() bool {
return upd != nil && upd.Message != nil
}
// IsEditedMessage checks that the current update is a editing message event.
func (upd *Update) IsEditedMessage() bool {
return upd != nil && upd.EditedMessage != nil
}
// IsChannelPost checks that the current update is a post channel creation event.
func (upd *Update) IsChannelPost() bool {
return upd != nil && upd.ChannelPost != nil
}
// IsEditedChannelPost checks that the current update is a editing post channel
// event.
func (upd *Update) IsEditedChannelPost() bool {
return upd != nil && upd.EditedChannelPost != nil
}
// IsInlineQuery checks that the current update is a inline query update.
func (upd *Update) IsInlineQuery() bool {
return upd != nil && upd.InlineQuery != nil
}
// IsChosenInlineResult checks that the current update is a chosen inline result
// update.
func (upd *Update) IsChosenInlineResult() bool {
return upd != nil && upd.ChosenInlineResult != nil
}
// IsCallbackQuery checks that the current update is a callback query update.
func (upd *Update) IsCallbackQuery() bool {
return upd != nil && upd.CallbackQuery != nil
}
// IsShippingQuery checks that the current update is a shipping query update.
func (upd *Update) IsShippingQuery() bool {
return upd != nil && upd.ShippingQuery != nil
}
// IsPreCheckoutQuery checks that the current update is a pre checkout query
// update.
func (upd *Update) IsPreCheckoutQuery() bool {
return upd != nil && upd.PreCheckoutQuery != nil
}
// Type return update type for current update.
func (upd *Update) Type() string {
switch {
case upd.IsCallbackQuery():
return UpdateCallbackQuery
case upd.IsChannelPost():
return UpdateChannelPost
case upd.IsChosenInlineResult():
return UpdateChosenInlineResult
case upd.IsEditedChannelPost():
return UpdateEditedChannelPost
case upd.IsEditedMessage():
return UpdateEditedMessage
case upd.IsInlineQuery():
return UpdateInlineQuery
case upd.IsMessage():
return UpdateMessage
case upd.IsPreCheckoutQuery():
return UpdatePreCheckoutQuery
case upd.IsShippingQuery():
return UpdateShippingQuery
default:
return ""
}
}