🔨 Refactoring NewWebhookChannel method
This commit is contained in:
parent
7089a69e78
commit
f929627d77
|
@ -1,13 +1,13 @@
|
||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"bytes"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
// "net/url"
|
|
||||||
|
|
||||||
log "github.com/kirillDanshin/dlog"
|
http "github.com/erikdubbelboer/fasthttp"
|
||||||
|
"github.com/kirillDanshin/dlog"
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
http "github.com/valyala/fasthttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdatesChannel <-chan Update
|
type UpdatesChannel <-chan Update
|
||||||
|
@ -26,8 +26,8 @@ func (bot *Bot) NewLongPollingChannel(params *GetUpdatesParameters) UpdatesChann
|
||||||
for {
|
for {
|
||||||
updates, err := bot.GetUpdates(params)
|
updates, err := bot.GetUpdates(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Ln(err.Error())
|
dlog.Ln(err.Error())
|
||||||
log.Ln("Failed to get updates, retrying in 3 seconds...")
|
dlog.Ln("Failed to get updates, retrying in 3 seconds...")
|
||||||
time.Sleep(time.Second * 3)
|
time.Sleep(time.Second * 3)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -56,59 +56,35 @@ func (bot *Bot) NewWebhookChannel(
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := bot.SetWebhook(params); err != nil {
|
if _, err := bot.SetWebhook(params); err != nil {
|
||||||
panic(err.Error())
|
log.Fatalln(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
channel := make(chan Update, params.MaxConnections)
|
channel := make(chan Update, 100)
|
||||||
go func() {
|
go func() {
|
||||||
var err error
|
requiredPath := []byte(listen)
|
||||||
if certFile != "" && keyFile != "" {
|
dlog.Ln("requiredPath:", string(requiredPath))
|
||||||
log.Ln("Creating TLS router...")
|
handleFunc := func(ctx *http.RequestCtx) {
|
||||||
err = http.ListenAndServeTLS(
|
dlog.Ln("Request path:", string(ctx.Path()))
|
||||||
serve,
|
if !bytes.HasPrefix(ctx.Path(), requiredPath) {
|
||||||
certFile,
|
dlog.Ln("Unsupported request path:", string(ctx.Path()))
|
||||||
keyFile,
|
return
|
||||||
func(ctx *http.RequestCtx) {
|
}
|
||||||
if !strings.HasPrefix(string(ctx.Path()), listen) {
|
|
||||||
log.Ln("Unsupported request path:", string(ctx.Path()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Ln("Catched supported request path:", string(ctx.Path()))
|
dlog.Ln("Catched supported request path:", string(ctx.Path()))
|
||||||
var update Update
|
var update Update
|
||||||
err = json.Unmarshal(ctx.Request.Body(), &update)
|
if err := json.Unmarshal(ctx.Request.Body(), &update); err != nil {
|
||||||
if err != nil {
|
log.Fatalln(err.Error())
|
||||||
log.Ln(err.Error())
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel <- update
|
channel <- update
|
||||||
},
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
log.Ln("Creating simple router...")
|
|
||||||
err = http.ListenAndServe(
|
|
||||||
serve,
|
|
||||||
func(ctx *http.RequestCtx) {
|
|
||||||
if !strings.HasPrefix(string(ctx.Path()), listen) {
|
|
||||||
log.Ln("Unsupported request path:", string(ctx.Path()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Ln("Catched supported request path:", string(ctx.Path()))
|
|
||||||
var update Update
|
|
||||||
err = json.Unmarshal(ctx.Request.Body(), &update)
|
|
||||||
if err != nil {
|
|
||||||
log.Ln(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel <- update
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
if certFile != "" && keyFile != "" {
|
||||||
|
dlog.Ln("Creating TLS router...")
|
||||||
|
log.Fatal(http.ListenAndServeTLS(serve, certFile, keyFile, handleFunc))
|
||||||
|
} else {
|
||||||
|
dlog.Ln("Creating simple router...")
|
||||||
|
log.Fatal(http.ListenAndServe(serve, handleFunc))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user