mirror of
https://github.com/nikoksr/notify.git
synced 2025-02-07 13:08:12 +02:00
build-passing commit
This commit is contained in:
parent
2b791ae0ba
commit
da3ffb0a7d
@ -33,49 +33,50 @@ You also need a user subscribed to your Official Account. You can use your own:
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/wechat"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"log"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/wechat"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
wechatSvc := wechat.New(wechat.Config{
|
||||
AppID: "abcdefghi",
|
||||
AppSecret: "jklmnopqr",
|
||||
Token: "mytoken",
|
||||
EncodingAESKey: "IGNORED-IN-SANDBOX",
|
||||
})
|
||||
wechatSvc := wechat.New(&wechat.Config{
|
||||
AppID: "abcdefghi",
|
||||
AppSecret: "jklmnopqr",
|
||||
Token: "mytoken",
|
||||
EncodingAESKey: "IGNORED-IN-SANDBOX",
|
||||
Cache: cache.NewMemory(),
|
||||
})
|
||||
|
||||
// do this only once, or when settings are updated
|
||||
devMode := true
|
||||
wechatSvc.WaitForOneOffVerification(":7999", devMode, func(r *http.Request, verified bool) {
|
||||
if !verified {
|
||||
fmt.Println("unknown or failed verification call")
|
||||
} else {
|
||||
fmt.Println("verification call done")
|
||||
}
|
||||
})
|
||||
// do this only once, or when settings are updated
|
||||
devMode := true
|
||||
wechatSvc.WaitForOneOffVerification(":7999", devMode, func(r *http.Request, verified bool) {
|
||||
if !verified {
|
||||
fmt.Println("unknown or failed verification call")
|
||||
} else {
|
||||
fmt.Println("verification call done")
|
||||
}
|
||||
})
|
||||
|
||||
wechatSvc.AddReceivers("some-user-openid")
|
||||
wechatSvc.AddReceivers("some-user-openid")
|
||||
|
||||
notifier := notify.New()
|
||||
notifier.UseServices(wechatSvc)
|
||||
notifier := notify.New()
|
||||
notifier.UseServices(wechatSvc)
|
||||
|
||||
err := notifier.Send(context.Background(), "subject", "message")
|
||||
if err != nil {
|
||||
log.Fatalf("notifier.Send() failed: %s", err.Error())
|
||||
}
|
||||
err := notifier.Send(context.Background(), "subject", "message")
|
||||
if err != nil {
|
||||
log.Fatalf("notifier.Send() failed: %s", err.Error())
|
||||
}
|
||||
|
||||
log.Println("notification sent")
|
||||
log.Println("notification sent")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -20,6 +20,7 @@ type Config struct {
|
||||
AppSecret string
|
||||
Token string
|
||||
EncodingAESKey string
|
||||
Cache cache.Cache
|
||||
}
|
||||
|
||||
// wechatMessageManager abstracts go-wechat's message.Manager for writing unit tests
|
||||
@ -29,15 +30,15 @@ type wechatMessageManager interface {
|
||||
|
||||
// Service encapsulates the WeChat client along with internal state for storing users.
|
||||
type Service struct {
|
||||
config Config
|
||||
config *Config
|
||||
messageManager wechatMessageManager
|
||||
userIDs []string
|
||||
}
|
||||
|
||||
// New returns a new instance of a WeChat notification service.
|
||||
func New(cfg Config) *Service {
|
||||
client := wechat.NewWechat()
|
||||
clientConfig := &config.Config{
|
||||
func New(cfg *Config) *Service {
|
||||
wc := wechat.NewWechat()
|
||||
wcCfg := &config.Config{
|
||||
AppID: cfg.AppID,
|
||||
AppSecret: cfg.AppSecret,
|
||||
Token: cfg.Token,
|
||||
@ -45,7 +46,7 @@ func New(cfg Config) *Service {
|
||||
Cache: cfg.Cache,
|
||||
}
|
||||
|
||||
account := wc.GetOfficialAccount(wcCfg)
|
||||
oa := wc.GetOfficialAccount(wcCfg)
|
||||
|
||||
return &Service{
|
||||
config: cfg,
|
||||
|
Loading…
x
Reference in New Issue
Block a user