华域联盟 Golang 使用Go语言实现微信公众平台

使用Go语言实现微信公众平台

这个不是全部的代码哦,只是一个演示可以验证跟接受post传过来的消息并且能返回消息,中间的回复逻辑就待需要各位同志们自己写了哈

复制代码 代码如下:

/*

 
*@go语言实现公众平台

 */

package main

import (

 "crypto/sha1"

 "encoding/xml"

 "fmt"

 "io"

 "io/ioutil"

 "log"

 "net/http"

 "sort"

 "strings"

 "time"

)

type Request struct {

 ToUserName   string

 FromUserName string

 CreateTime   time.Duration

 MsgType      string

 Content      string

 MsgId        int

}

type Response struct {

 ToUserName   string `xml:"xml>ToUserName"`

 FromUserName string `xml:"xml>FromUserName"`

 CreateTime   string `xml:"xml>CreateTime"`

 MsgType      string `xml:"xml>MsgType"`

 Content      string `xml:"xml>Content"`

 MsgId        int    `xml:"xml>MsgId"`

}

func str2sha1(data string) string {

 t := sha1.New()

 io.WriteString(t, data)

 return fmt.Sprintf("%x", t.Sum(nil))

}

func action(w http.ResponseWriter, r *http.Request) {

 postedMsg, err := ioutil.ReadAll(r.Body)

 if err != nil {

  log.Fatal(err)

 }

 r.Body.Close()

 v := Request{}

 xml.Unmarshal(postedMsg, &v)

 if v.MsgType == "text" {

  v := Request{v.ToUserName, v.FromUserName, v.CreateTime, v.MsgType, v.Content, v.MsgId}

  output, err := xml.MarshalIndent(v, " ", " ")

  if err != nil {

   fmt.Printf("error:%v\n", err)

  }

  fmt.Fprintf(w, string(output))

 } else if v.MsgType == "event" {

  Content := `"欢迎关注

        我的微信"`

  v := Request{v.ToUserName, v.FromUserName, v.CreateTime, v.MsgType, Content, v.MsgId}

  output, err := xml.MarshalIndent(v, " ", " ")

  if err != nil {

   fmt.Printf("error:%v\n", err)

  }

  fmt.Fprintf(w, string(output))

 }

}

func checkSignature(w http.ResponseWriter, r *http.Request) {

 r.ParseForm()

 var token string = "你的token"

 var signature string = strings.Join(r.Form["signature"], "")

 var timestamp string = strings.Join(r.Form["timestamp"], "")

 var nonce string = strings.Join(r.Form["nonce"], "")

 var echostr string = strings.Join(r.Form["echostr"], "")

 tmps := []string{token, timestamp, nonce}

 sort.Strings(tmps)

 tmpStr := tmps[0] + tmps[1] + tmps[2]
 tmp := str2sha1(tmpStr)

 if tmp == signature {

  fmt.Fprintf(w, echostr)

 }

}

func main() {

 http.HandleFunc("/check", checkSignature)

 http.HandleFunc("/", action)

 http.ListenAndServe(":8080", nil)

}

本文由 华域联盟 原创撰写:华域联盟 » 使用Go语言实现微信公众平台

转载请保留出处和原文链接:https://www.cnhackhy.com/109904.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们