From 90bf7ef73360f68462d899c7a4ed314ead168084 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Wed, 26 Apr 2017 16:05:24 +1000 Subject: [PATCH] building all type of outgoing msg template (not tested) --- outMsg.go | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 159 insertions(+), 11 deletions(-) diff --git a/outMsg.go b/outMsg.go index 8627c2e..044484d 100644 --- a/outMsg.go +++ b/outMsg.go @@ -2,27 +2,33 @@ package main import ( "crypto/sha1" + "errors" "fmt" + "log" "math/rand" "sort" "strings" "time" ) +//Article is one item in News Message +// one such message can contains upto 10 Articles +type Article struct { + title, description, picURL, url string +} + //BuildTextMsg Given a text message send it to wechat client -func BuildTextMsg(txt string, ToUserName string) (string, error) { +func BuildTextMsg(txt string, ToUserName string) (xml string, err error) { + if txt == "" || ToUserName == "" { + err = errors.New("Empty text body or Empty destination") + xml = "" + return + } - template := ` - - - %d - - - ` - msg := fmt.Sprintf(template, ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), txt) + msg := fmt.Sprintf(txtMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), txt) e := Encode(msg) - str, _, _, _ := signMsg(e) - return str, nil + xml, _, _, _ = signMsg(e) + return } func signMsg(content string) (xml string, timestamp int32, nonce int32, signature string) { @@ -50,3 +56,145 @@ func getSignature(token string, timestamp string, nonce string, content string) signature = fmt.Sprintf("%x", h.Sum(nil)) return } + +func buildTxtMsg(ToUserName, content string) (msg string) { + msg = fmt.Sprintf(txtMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), content) + return +} +func txtMsgTemplate() string { + return ` + + + %d + + + ` +} + +func buildPicMsg(ToUserName, mediaID string) (msg string) { + msg = fmt.Sprintf(picMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID) + return +} +func picMsgTemplate() string { + return ` + + +%d + + + + +` +} + +func buildVoiceMsg(ToUserName, mediaID string) (msg string) { + msg = fmt.Sprintf(voiceMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID) + return +} +func voiceMsgTemplate() string { + return ` + + +%d + + + + +` +} + +func buildVideoMsg(ToUserName, mediaID, title, description string) (msg string) { + msg = fmt.Sprintf(videoMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID, title, description) + return +} +func videoMsgTemplate() string { + return ` + + +%d + + +` +} + +func bulidMusicMsg(ToUserName, mediaID, title, description, url, hqURL string) (msg string) { + msg = fmt.Sprintf(musicMsgTemplate(), + ToUserName, + APIConfig.PublicAccountID, + int32(time.Now().Unix()), + title, + description, + url, + hqURL, + mediaID) + return +} +func musicMsgTemplate() string { + return ` + + +12345678 + + +<![CDATA[TITLE]]> + + + + + +` +} + +func buildNewsMsg(ToUserName, title, description string, articles []Article) (msg string) { + count := len(articles) + if count <= 0 { + log.Fatal("using empty article to build NewsMsg") + msg = "" + return + } + + items := []string{} + for _, a := range articles { + s := buildArticleItem(a) + items = append(items, s) + } + strItems := strings.Join(items, "") + + msg = fmt.Sprintf(newsMsgTemplate(), + ToUserName, + APIConfig.PublicAccountID, + int32(time.Now().Unix()), + count, + strItems) + return +} + +func buildArticleItem(item Article) (article string) { + template := ` +<![CDATA[%s]]> + + + + +` + article = fmt.Sprintf(template, + item.title, item.description, item.picURL, item.url) + return article +} + +func newsMsgTemplate() string { + return ` + + +%d + +%d + +%s + +` +}