关于go:go-python-开发对接企微自建应用发消息-实例

12次阅读

共计 2498 个字符,预计需要花费 7 分钟才能阅读完成。

doc

https://developer.work.weixin…

app

go


package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

const (
    CorpId = "xxxxxxxxx"
    CorpSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-k"
    AgentId int = 1xxxxxxx
)

type T struct {
    Errcode     int    `json:"errcode"`
    Errmsg      string `json:"errmsg"`
    AccessToken string `json:"access_token"`
    ExpiresIn   int    `json:"expires_in"`
}

type T2 struct {Touser  interface{} `json:"touser"`
    Msgtype string      `json:"msgtype"`
    Agentid int         `json:"agentid"`
    Text    struct {Content interface{} `json:"content"`
    } `json:"markdown"`
    Safe int `json:"safe"`
}

type T3 struct {
    Errcode int    `json:"errcode"`
    Errmsg  string `json:"errmsg"`
    Msgid   string `json:"msgid"`
}


func GetToken(CorpId string, CorpSecret string) (*T) {url := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", CorpId, CorpSecret)
    client := &http.Client{}
    req, err := http.NewRequest("GET", url, nil)

    resp, err := client.Do(req)
    if err != nil {log.Fatal(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {log.Fatal(err)
    }
    fmt.Println("######", string(body))
    var assetList T
    err = json.Unmarshal(body, &assetList)
    return &assetList
}

func SendMessage(token string, comment string, userName string) *T3 {
    var j T2

    j.Agentid = AgentId
    j.Text.Content = comment
    j.Touser = userName
    j.Msgtype = "markdown"
    j.Safe = 0

    data, err := json.Marshal(j)
    if err != nil {fmt.Println("err was %v", err)
    }
    fmt.Println(string(data))

    reader := bytes.NewReader(data)
    uri := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
    client := &http.Client{}
    req, err := http.NewRequest("POST", uri, reader)
    req.Header.Add("Content-Type", "application/json")
    req.Header.Add("Accept", "application/json")
    resp, err := client.Do(req)
    if err != nil {log.Fatal(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {log.Fatal(err)
    }
    fmt.Println("####send msg##", string(body))
    var assetList T3
    err = json.Unmarshal(body, &assetList)
    return &assetList
}


func main()  {token1 := GetToken(CorpId, CorpSecret)
    //fmt.Println(token1.AccessToken)
    SendMessage(token1.AccessToken, "### hallo, ich bin TixnxxxXiang \n <font color=\\\"info\\\">Gutgen Tag</font> \n * Lexbzenhn", "lixxxxg@xiaoxxxxshu.com")
}


python

def send_wechat_markdown(username, content):
    url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxxxxx&corpsecret=xxxxxxk"
    re = requests.get(url)
    token = json.loads(re.content).get("access_token")
    print(token)

    url2 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
    data = {
        "touser": username,
        "msgtype": "markdown",
        "agentid": xxxxxx,
        "markdown": {"content": content},
        "safe": 0
    }
    data2 = json.dumps(data)

    r = requests.post(url2, data2)
    print("###", r.text, "###")
    r.close()
正文完
 0