Open source

The project is open source on Github and is not yet fully developed, but it works fine. For everyone god’s star ah, this is my first relatively complete Go program. (^ __ ^)

How does it work?

The first step, of course, is go get

  • go get github.com/hundredlee/wechat_pusher.git
  • Of course, you can clone the entire project and run it directly into the IDE

The project structure

├ ─ ─ the README. Md ├ ─ ─ the config │ └ ─ ─ config. Go ├ ─ ─ config. Conf. Example ├ ─ ─ glide. Lock ├ ─ ─ glide. The yaml ├ ─ ─ main. Go. Example ├ ─ ─ Models │ ├ ─ ─ message. Go │ ├ ─ ─ task. Go │ └ ─ ─ token. Go ├ ─ ─ the statics │ └ ─ ─ global. Go ├ ─ ─ utils │ ├ ─ ─ access_token. Go │ └ ─ ─ Push. Go └ ─ ─ vendorCopy the code

The configuration file

  • We can see that the root directory has a config.conf.example. Rename it to config.conf
  • As follows:
[WeChat]
APPID=
SECRET=
TOKEN=
TEMPLATE=Copy the code
  • I won’t tell you how to fill it in. This is contact wechat development of children’s shoes all know things.

How to configure a template

  • Let’s look at the message.go file in the Models folder, which is actually the template format.
  • See the main.go.example file for an example to see how this works.
package main
import (
    "fmt"
    "github.com/hundredlee/wechat_pusher/models"
    "github.com/hundredlee/wechat_pusher/utils"
    "runtime"
)
func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    var tasks []models.Task
    tasks = make([]models.Task, 100)
    mess := models.Message{
        ToUser: "openid",
        TemplateId: "templateId",
        Url: "http://baidu.com",
        Data: models.Data{
            First: models.Raw{"xxx", "#173177"},
            Subject: models.Raw{"xxx", "#173177"},
            Sender: models.Raw{"xxx", "#173177"},
            Remark: models.Raw{"xxx", "#173177"}}}
    task := models.Task{Message: mess}
    for i := 0; i < 100; i++ {
        task.Message.Data.First.Value = fmt.Sprintf("%d", i)
        tasks[i] = task
    }
    utils.NewPush(tasks).SetRetries(4).SetBufferNum(10).Run()
}Copy the code

Run

  • It’s easy, once you’ve assembled all your tasks, just run a single sentence.
  • utils.NewPush(tasks).SetRetries(4).SetBufferNum(10).Run()

Going to do?

  • At present, it is still relatively simple to push, and then the log is relatively complete. But the scheduled task function is missing. You can star and wait for me to update the function of scheduling tasks.