template

package
v0.0.0-...-71333bb Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2015 License: Apache-2.0 Imports: 4 Imported by: 0

README

发送模板消息示例

package main

import (
	"encoding/json"
	"fmt"

	"github.com/philsong/wechat2/mp"
	"github.com/philsong/wechat2/mp/message/template"
)

var TokenServer = mp.NewDefaultTokenServer("appid", "appsecret", nil)

func main() {
	type Item struct {
		Value string `json:"value"`
		Color string `json:"color"`
	}

	//{
	//    "first": {
	//        "value": "恭喜你购买成功!",
	//        "color": "#173177"
	//    },
	//    "keynote1": {
	//        "value": "巧克力",
	//        "color": "#173177"
	//    },
	//    "keynote2": {
	//        "value": "39.8元",
	//        "color": "#173177"
	//    },
	//    "keynote3": {
	//        "value": "2014年9月16日",
	//        "color": "#173177"
	//    },
	//    "remark": {
	//        "value": "欢迎再次购买!",
	//        "color": "#173177"
	//    }
	//}
	var data = struct {
		First    Item `json:"first"`
		KeyNote1 Item `json:"keynote1"`
		KeyNote2 Item `json:"keynote2"`
		KeyNote3 Item `json:"keynote3"`
		Remark   Item `json:"remark"`
	}{
		First: Item{
			Value: "恭喜你购买成功!",
			Color: "#173177",
		},
		KeyNote1: Item{
			Value: "巧克力",
			Color: "#173177",
		},
		KeyNote2: Item{
			Value: "39.8元",
			Color: "#173177",
		},
		KeyNote3: Item{
			Value: "2014年9月16日",
			Color: "#173177",
		},
		Remark: Item{
			Value: "欢迎再次购买!",
			Color: "#173177",
		},
	}

	dataJSONBytes, err := json.Marshal(&data)
	if err != nil {
		fmt.Println(err)
		return
	}

	msg := template.TemplateMessage{
		ToUser:      "touser",
		TemplateId:  "template_id",
		URL:         "http://weixin.qq.com/download",
		TopColor:    "#FF0000",
		RawJSONData: dataJSONBytes,
	}

	clt := template.NewClient(TokenServer, nil)
	msgId, err := clt.Send(&msg)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("MsgId:", msgId)
}

Documentation

Overview

模板消息接口.

Index

Constants

View Source
const (
	TemplateSendStatusSuccess            = "success"               // 送达成功时
	TemplateSendStatusFailedUserBlock    = "failed:user block"     // 送达由于用户拒收(用户设置拒绝接收公众号消息)而失败
	TemplateSendStatusFailedSystemFailed = "failed: system failed" // 送达由于其他原因失败
)
View Source
const (
	EventTypeTemplateSendJobFinish mp.EventType = "TEMPLATESENDJOBFINISH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	mp.WechatClient
}

func NewClient

func NewClient(TokenServer mp.TokenServer, HttpClient *http.Client) *Client

创建一个新的 Client.

如果 HttpClient == nil 则默认用 http.DefaultClient

func (*Client) AddTemplate

func (clt *Client) AddTemplate(templateIdShort string) (templateId string, err error)

从行业模板库选择模板添加到账号后台, 并返回模板id.

templateIdShort: 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式.

func (*Client) Send

func (clt *Client) Send(msg *TemplateMessage) (msgid int64, err error)

发送模板消息

func (*Client) SetIndustry

func (clt *Client) SetIndustry(industryId ...int64) (err error)

设置所属行业.

目前 industryId 的个数只能为 2.

type TemplateMessage

type TemplateMessage struct {
	ToUser     string `json:"touser"`             // 必须, 接受者OpenID
	TemplateId string `json:"template_id"`        // 必须, 模版ID
	URL        string `json:"url,omitempty"`      // 可选, 用户点击后跳转的URL,该URL必须处于开发者在公众平台网站中设置的域中
	TopColor   string `json:"topcolor,omitempty"` // 可选, 整个消息的颜色, 可以不设置

	// 必须, JSON 格式的 []byte, 满足特定的模板需求
	RawJSONData json.RawMessage `json:"data"`
}

type TemplateSendJobFinishEvent

type TemplateSendJobFinishEvent struct {
	XMLName struct{} `xml:"xml" json:"-"`
	mp.CommonMessageHeader

	Event  string `xml:"Event"  json:"Event"` // 事件信息,此处为 TEMPLATESENDJOBFINISH
	MsgId  int64  `xml:"MsgId"  json:"MsgId"` // 模板消息ID
	Status string `xml:"Status" json:"Status"`
}

在模版消息发送任务完成后,微信服务器会将是否送达成功作为通知,发送到开发者中心中填写的服务器配置地址中。

func GetTemplateSendJobFinishEvent

func GetTemplateSendJobFinishEvent(msg *mp.MixedMessage) *TemplateSendJobFinishEvent

Jump to

Keyboard shortcuts

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