init
This commit is contained in:
27
middleware/logger.go
Normal file
27
middleware/logger.go
Normal file
@ -0,0 +1,27 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
tele "git.belvedersky.ru/common/telebot"
|
||||
)
|
||||
|
||||
// Logger returns a middleware that logs incoming updates.
|
||||
// If no custom logger provided, log.Default() will be used.
|
||||
func Logger(logger ...*log.Logger) tele.MiddlewareFunc {
|
||||
var l *log.Logger
|
||||
if len(logger) > 0 {
|
||||
l = logger[0]
|
||||
} else {
|
||||
l = log.Default()
|
||||
}
|
||||
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return func(c tele.Context) error {
|
||||
data, _ := json.MarshalIndent(c.Update(), "", " ")
|
||||
l.Println(string(data))
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
62
middleware/middleware.go
Normal file
62
middleware/middleware.go
Normal file
@ -0,0 +1,62 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
tele "git.belvedersky.ru/common/telebot"
|
||||
)
|
||||
|
||||
// AutoRespond returns a middleware that automatically responds
|
||||
// to every callback.
|
||||
func AutoRespond() tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return func(c tele.Context) error {
|
||||
if c.Callback() != nil {
|
||||
defer c.Respond()
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IgnoreVia returns a middleware that ignores all the
|
||||
// "sent via" messages.
|
||||
func IgnoreVia() tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return func(c tele.Context) error {
|
||||
if msg := c.Message(); msg != nil && msg.Via != nil {
|
||||
return nil
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recover returns a middleware that recovers a panic happened in
|
||||
// the handler.
|
||||
func Recover(onError ...func(error)) tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return func(c tele.Context) error {
|
||||
var f func(error)
|
||||
if len(onError) > 0 {
|
||||
f = onError[0]
|
||||
} else {
|
||||
f = func(err error) {
|
||||
c.Bot().OnError(err, nil)
|
||||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if err, ok := r.(error); ok {
|
||||
f(err)
|
||||
} else if s, ok := r.(string); ok {
|
||||
f(errors.New(s))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
30
middleware/middleware_test.go
Normal file
30
middleware/middleware_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
tele "git.belvedersky.ru/common/telebot"
|
||||
)
|
||||
|
||||
var b, _ = tele.NewBot(tele.Settings{Offline: true})
|
||||
|
||||
func TestRecover(t *testing.T) {
|
||||
onError := func(err error) {
|
||||
require.Error(t, err, "recover test")
|
||||
}
|
||||
|
||||
h := func(c tele.Context) error {
|
||||
panic("recover test")
|
||||
}
|
||||
|
||||
assert.Panics(t, func() {
|
||||
h(nil)
|
||||
})
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
Recover(onError)(h)(nil)
|
||||
})
|
||||
}
|
65
middleware/restrict.go
Normal file
65
middleware/restrict.go
Normal file
@ -0,0 +1,65 @@
|
||||
package middleware
|
||||
|
||||
import tele "git.belvedersky.ru/common/telebot"
|
||||
|
||||
// RestrictConfig defines config for Restrict middleware.
|
||||
type RestrictConfig struct {
|
||||
// Chats is a list of chats that are going to be affected
|
||||
// by either In or Out function.
|
||||
Chats []int64
|
||||
|
||||
// In defines a function that will be called if the chat
|
||||
// of an update will be found in the Chats list.
|
||||
In tele.HandlerFunc
|
||||
|
||||
// Out defines a function that will be called if the chat
|
||||
// of an update will NOT be found in the Chats list.
|
||||
Out tele.HandlerFunc
|
||||
}
|
||||
|
||||
// Restrict returns a middleware that handles a list of provided
|
||||
// chats with the logic defined by In and Out functions.
|
||||
// If the chat is found in the Chats field, In function will be called,
|
||||
// otherwise Out function will be called.
|
||||
func Restrict(v RestrictConfig) tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
if v.In == nil {
|
||||
v.In = next
|
||||
}
|
||||
if v.Out == nil {
|
||||
v.Out = next
|
||||
}
|
||||
return func(c tele.Context) error {
|
||||
for _, chat := range v.Chats {
|
||||
if chat == c.Sender().ID {
|
||||
return v.In(c)
|
||||
}
|
||||
}
|
||||
return v.Out(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Blacklist returns a middleware that skips the update for users
|
||||
// specified in the chats field.
|
||||
func Blacklist(chats ...int64) tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return Restrict(RestrictConfig{
|
||||
Chats: chats,
|
||||
Out: next,
|
||||
In: func(c tele.Context) error { return nil },
|
||||
})(next)
|
||||
}
|
||||
}
|
||||
|
||||
// Whitelist returns a middleware that skips the update for users
|
||||
// NOT specified in the chats field.
|
||||
func Whitelist(chats ...int64) tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return Restrict(RestrictConfig{
|
||||
Chats: chats,
|
||||
In: next,
|
||||
Out: func(c tele.Context) error { return nil },
|
||||
})(next)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user