wechat

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 13 Imported by: 0

README

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/buugaaga/go-notify"
  "github.com/buugaaga/go-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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AppID          string
	AppSecret      string
	Token          string
	EncodingAESKey string
	Cache          cache.Cache
}

Config is the Service configuration.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service encapsulates the WeChat client along with internal state for storing users.

func New

func New(cfg *Config) *Service

New returns a new instance of a WeChat notification service.

func (*Service) AddReceivers

func (s *Service) AddReceivers(userIDs ...string)

AddReceivers takes user ids and adds them to the internal users list. The Send method will send a given message to all those users.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, content string) error

Send takes a message subject and a message content and sends them to all previously set users.

func (*Service) WaitForOneOffVerification

func (s *Service) WaitForOneOffVerification(serverURL string, devMode bool, callback verificationCallbackFunc) error

WaitForOneOffVerification waits for the verification call from the WeChat backend. It uses an internal ReadHeaderTimeout of 20 seconds to avoid blocking the caller for too long (potential slow loris attack). In case that you want to use a different timeout, you can use the WaitForOneOffVerificationWithServer method instead. It allows you to specify a custom server.

Should be running when (re-)applying settings in wechat configuration.

Set devMode to true when using the sandbox.

See https://developers.weixin.qq.com/doc/offiaccount/en/Basic_Information/Access_Overview.html

func (*Service) WaitForOneOffVerificationWithServer

func (s *Service) WaitForOneOffVerificationWithServer(server *http.Server, devMode bool, callback verificationCallbackFunc) error

WaitForOneOffVerificationWithServer allows you to use WaitForOneOffVerification with a fully custom HTTP server.

Should be running when (re-)applying settings in wechat configuration.

Set devMode to true when using the sandbox.

See https://developers.weixin.qq.com/doc/offiaccount/en/Basic_Information/Access_Overview.html

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL