1. 程式人生 > >Go調用企業微信API發送自定義信息

Go調用企業微信API發送自定義信息

tid dia char fmt charset 企業微信 standard () bin

  • 使用方法
  • # 編譯成可執行文件之後,直接用命令調用傳遞內容即可
    
    編譯:go build send_message.go
    
    使用:
    
    ./send_message "這是一條測試消息\n測試回車"
    
    • 代碼

    package main
    
    import (
        "bytes"
        "github.com/json-iterator/go"
        "io/ioutil"
        "net/http"
        "os"
        "strings"
    )
    
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    
    type JSON struct {
        Access_token string `json:"access_token"`
    }
    
    type MESSAGES struct {
        Touser string `json:"touser"`
        Toparty string `json:"toparty"`
        Msgtype string `json:"msgtype"`
        Agentid int `json:"agentid"`
        Text struct {
            //Subject string `json:"subject"`
            Content string `json:"content"`
        } `json:"text"`
        Safe int `json:"safe"`
    }
    
    func Get_AccessToken(corpid,corpsecret string) string {
        gettoken_url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret
        //print(gettoken_url)
        client := &http.Client{}
        req, _ := client.Get(gettoken_url)
        defer req.Body.Close()
        body, _ := ioutil.ReadAll(req.Body)
        //fmt.Printf("\n%q",string(body))
        var json_str JSON
        json.Unmarshal([]byte(body), &json_str)
        //fmt.Printf("\n%q",json_str.Access_token)
        return json_str.Access_token
    }
    
    func Send_Message(access_token,msg string) {
        send_url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token
        //print(send_url)
        client := &http.Client{}
        req, _ := http.NewRequest("POST", send_url, bytes.NewBuffer([]byte(msg)))
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("charset","UTF-8")
        resp, err := client.Do(req)
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()
        //fmt.Println("response Status:", resp.Status)
        //body, _ := ioutil.ReadAll(resp.Body)
        //fmt.Println("response Body:", string(body))
    }
    
    func messages(touser string,toparty string,agentid int,content string) string{
        msg := MESSAGES{
            Touser: touser,
            Toparty: toparty,
            Msgtype: "text",
            Agentid: agentid,
            Safe: 0,
            Text: struct {
                //Subject string `json:"subject"`
                Content string `json:"content"`
            }{Content: content},
        }
        sed_msg, _ := json.Marshal(msg)
        //fmt.Printf("%s",string(sed_msg))
        return string(sed_msg)
    }
    
    func main() {
        touser := "BigBoss"  //企業號中的用戶帳號,在zabbix用戶Media中配置,如果配置不正常,將按部門發送。
        toparty := "2"       //企業號中的部門id。
        agentid:= 1000002    //企業號中的應用id。
        corpid := "xxxxxxxxxxxxxxxxx"  //企業號的標識
        corpsecret := "exxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  ///企業號中的應用的Secret
        accessToken := Get_AccessToken(corpid,corpsecret)
        content := os.Args[1]
        //  fmt.Println(content)
        // 序列化成json之後,\n會被轉義,也就是變成了\\n,使用str替換,替換掉轉義
        msg := strings.Replace(messages(touser,toparty,agentid,content),"\\\\","\\",-1)
    
        //  fmt.Println(strings.Replace(msg,"\\\\","\\",-1))
        Send_Message(accessToken,msg)
    
    }
    

    Go調用企業微信API發送自定義信息