Bladeren bron

wechat auto reply

ls 5 jaren geleden
bovenliggende
commit
f4e1a188ff
3 gewijzigde bestanden met toevoegingen van 155 en 19 verwijderingen
  1. 39 15
      wechat/const.go
  2. 40 1
      wechat/response.go
  3. 76 3
      wechat/structure.go

+ 39 - 15
wechat/const.go

@@ -15,19 +15,43 @@ const (
 	// ErrReqOk request ok
 	ErrReqOk = 0
 
-	// PayURLUnifiedOrder pay 付款
-	PayURLUnifiedOrder = `/pay/unifiedorder`
-	// PayURLPayRefund pay refund 退款
-	PayURLPayRefund = `/secapi/pay/refund`
-	// PayURLPapPay 委托代扣申请扣款
-	PayURLPapPay = `/pay/pappayapply`
-	// PayURLPapayEntrust H5 纯签约
-	PayURLPapayEntrust = `/papay/h5entrustweb`
-
-	// PayTradeTypeJS JSAPI 公众号支付
-	PayTradeTypeJS = `JSAPI`
-	// PayTradeTypeNative NATIVE 扫码支付
-	PayTradeTypeNative = `NATIVE`
-	// PayTradeTypeAPP APP APP支付
-	PayTradeTypeAPP = `APP`
+	/*
+		// PayURLUnifiedOrder pay 付款
+		PayURLUnifiedOrder = `/pay/unifiedorder`
+		// PayURLPayRefund pay refund 退款
+		PayURLPayRefund = `/secapi/pay/refund`
+		// PayURLPapPay 委托代扣申请扣款
+		PayURLPapPay = `/pay/pappayapply`
+		// PayURLPapayEntrust H5 纯签约
+		PayURLPapayEntrust = `/papay/h5entrustweb`
+
+		// PayTradeTypeJS JSAPI 公众号支付
+		PayTradeTypeJS = `JSAPI`
+		// PayTradeTypeNative NATIVE 扫码支付
+		PayTradeTypeNative = `NATIVE`
+		// PayTradeTypeAPP APP APP支付
+		PayTradeTypeAPP = `APP`
+		// */
+
+	// MsgText text message
+	MsgText = `text`
+	// MsgImage image message
+	MsgImage = `image`
+	// MsgVoice voice message
+	MsgVoice = `voice`
+	// MsgVideo video message
+	MsgVideo = `video`
+	// MsgShortVideo shortvideo message
+	MsgShortVideo = `shortvideo`
+	// MsgLocation location message
+	MsgLocation = `location`
+	// MsgLink link message
+	MsgLink = `link`
+
+	// MsgNews news
+	MsgNews = `news`
+	// MsgMusic music
+	MsgMusic = `music`
 )
+
+// text/image/voice/video/shortvideo/location/link

+ 40 - 1
wechat/response.go

@@ -1,7 +1,46 @@
 package wechat
 
-import "sync"
+import (
+	"fmt"
+	"sync"
+	"time"
+
+	"encoding/xml"
+)
 
 var (
 	cache sync.Map
 )
+
+// ToXML reply message to xml
+func (m ReplyMessage) ToXML() (data []byte, err error) {
+	return xml.Marshal(m)
+}
+
+// ReplyText 回复文本信息
+func ReplyText(msg Message, text string) (reply ReplyMessage) {
+	reply.ToUserName = msg.FromUserName
+	reply.FromUserName = msg.ToUserName
+	reply.CreateTime = time.Now().Unix()
+	reply.MsgType = MsgText
+	reply.Content = text
+	return
+}
+
+// ReplyNews 回复图文信息
+func ReplyNews(msg Message, items []ArticleItem) (reply ReplyMessage) {
+	reply.ToUserName = msg.FromUserName
+	reply.FromUserName = msg.ToUserName
+	reply.CreateTime = time.Now().Unix()
+	reply.MsgType = MsgNews
+	reply.ArticleCount = len(items)
+
+	articles := &articleItems{}
+	articles.Items = items
+	reply.Articles = articles
+
+	bs, _ := xml.Marshal(reply)
+	fmt.Println(string(bs))
+
+	return
+}

+ 76 - 3
wechat/structure.go

@@ -74,11 +74,84 @@ type Message struct {
 	XMLName      xml.Name `xml:"xml"`
 	ToUserName   string   `xml:"ToUserName" json:"ToUserName"`
 	FromUserName string   `xml:"FromUserName" json:"FromUserName"`
-	CreateTime   int32    `xml:"CreateTime" json:"CreateTime"`
-	MsgType      string   `xml:"MsgType" json:"MsgType"`
-	MsgID        int64    `xml:"MsgId" json:"MsgId"`
+	CreateTime   int64    `xml:"CreateTime" json:"CreateTime"`
+	MsgType      string   `xml:"MsgType" json:"MsgType"` // text/image/voice/video/shortvideo/location/link
+
+	// 文本 Content Text message
+	Content string `xml:"Content,omitempty" json:"Content,omitempty"`
+
+	// 图片 PicURL / MediaID image messgae
+	PicURL  string `xml:"PicUrl,omitempty" json:"PicUrl,omitempty"`
+	MediaID string `xml:"MediaId,omitempty" json:"MediaId,omitempty"`
+
+	// 语音 MediaID / Format voice message
+	Format string `xml:"Format,omitempty" json:"Format,omitempty"`
+
+	// 视频/小视频 ThumbMediaID / MediaID video/shortvideo message
+	ThumbMediaID string `xml:"ThumbMediaId,omitempty" json:"ThumbMediaId,omitempty"`
+
+	// 地理位置 location message
+	LocationX string `xml:"Location_X,omitempty" json:"Location_X,omitempty"` // 经度
+	LocationY string `xml:"Location_Y,omitempty" json:"Location_Y,omitempty"` // 纬度
+	Scale     string `xml:"Scale,omitempty" json:"Scale,omitempty"`           // 地图缩放大小
+	Label     string `xml:"Label,omitempty" json:"Label,omitempty"`           // 地理位置信息
+
+	// 链接消息 link
+	Title       string `xml:"Title,omitempty" json:"Title,omitempty"`             // 消息标题
+	Description string `xml:"Description,omitempty" json:"Description,omitempty"` // 消息描述
+	URL         string `xml:"Url,omitempty" json:"Url,omitempty"`                 // 消息链接
+
+	Event string `xml:"Event,omitempty" json:"Event,omitempty"` // 事件 subscribe(订阅)、unsubscribe(取消订阅)
+
+	MsgID int64 `xml:"MsgId,omitempty" json:"MsgId,omitempty"` // 消息id
+}
+
+// ArticleItem article item
+type ArticleItem struct {
+	Title       string `xml:"Title" json:"Title"`
+	Description string `xml:"Description" json:"Description"`
+	PicURL      string `xml:"PicUrl" json:"PicUrl"`
+	URL         string `xml:"Url" json:"Url"`
+}
+
+type articleItems struct {
+	Items []ArticleItem `xml:"item" json:"item"`
 }
 
+// ReplyMessage reply message
+type ReplyMessage struct {
+	XMLName      xml.Name `xml:"xml"`
+	ToUserName   string   `xml:"ToUserName" json:"ToUserName"`
+	FromUserName string   `xml:"FromUserName" json:"FromUserName"`
+	CreateTime   int64    `xml:"CreateTime" json:"CreateTime"`
+	MsgType      string   `xml:"MsgType" json:"MsgType"` // text/image/voice/video/music/news
+
+	// 文本 Content Text message
+	Content string `xml:"Content,omitempty" json:"Content,omitempty"`
+
+	// image
+	ArticleCount int           `xml:"ArticleCount,omitempty" json:"ArticleCount,omitempty"`
+	Articles     *articleItems `xml:"Articles,omitempty" json:"Articles,omitempty"`
+}
+
+/*
+<xml>
+  <ToUserName><![CDATA[toUser]]></ToUserName>
+  <FromUserName><![CDATA[fromUser]]></FromUserName>
+  <CreateTime>12345678</CreateTime>
+  <MsgType><![CDATA[news]]></MsgType>
+  <ArticleCount>1</ArticleCount>
+  <Articles>
+    <item>
+      <Title><![CDATA[title1]]></Title>
+      <Description><![CDATA[description1]]></Description>
+      <PicUrl><![CDATA[picurl]]></PicUrl>
+      <Url><![CDATA[url]]></Url>
+    </item>
+  </Articles>
+</xml>
+// */
+
 // EventTemplateReply event reply
 type EventTemplateReply struct {
 	XMLName      xml.Name `xml:"xml"`