1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-24 08:22:18 +02:00
notify/service/wechat
Niko Köser f433aedcb5
refactor(sec): add ReadHeaderTimeout to verification server
WaitForOneOffVerification now uses a ReadHeaderTimeout of 20 seconds by
default. This is to avoid potential slow loris attacks which means
blocking a connection for too long. In case that the user wants to
provide a custom timeout, I added the
WaitForOneOffVerificationWithServer method. It works identical to
WaitForOneOffVerification with the subtle difference that the user can
pass in a fully customized http.Server instance.

This closes gosec G112.
2022-08-04 15:49:58 +02:00
..
img [ImgBot] Optimize images 2021-04-27 05:47:17 +00:00
mock_wechatMessageManager.go feat: added support for WeChat 2021-03-21 18:54:10 +02:00
README.md build-passing commit 2021-04-15 12:12:28 +03:00
wechat_test.go test(go): allow for parallel tests 2022-04-22 20:45:29 +02:00
wechat.go refactor(sec): add ReadHeaderTimeout to verification server 2022-08-04 15:49:58 +02:00

WeChat

Prerequisites

To use the service you need to apply for a WeChat Official Account. Application approval is a manual process and might take a while. In the meantime, you can test your code using the sandbox.

You need the following configuration information to sent out text messages with an Official Account:

  1. AppID
  2. AppSecret
  3. Token
  4. EncodingAESKey
  5. Verification URL

The AppID and AppSecret are provided to you by WeChat. The Token, EncodingAESKey and the Verifications URL, you set yourself and are needed by the authentication method. More on this here.

Getting Started

Until your application is approved, sign in to the sandbox to get an AppID, an AppSecret and set the Token and the Verification URL. Typically, you need a service like ngrok for the latter. You don't need to/cannot set the EncodingAESKey, because it's not required in the sandbox environment:

wechat_sandbox_1.png

You also need a user subscribed to your Official Account. You can use your own:

wechat_sandbox_2.png

Usage

package main

import (
  "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",
    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")
    }
  })

  wechatSvc.AddReceivers("some-user-openid")

  notifier := notify.New()
  notifier.UseServices(wechatSvc)

  err := notifier.Send(context.Background(), "subject", "message")
  if err != nil {
    log.Fatalf("notifier.Send() failed: %s", err.Error())
  }

  log.Println("notification sent")
}

wechat_usage.png