12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package util
- 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 {
-
- 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
-
- client := &http.Client{}
- req, _ := client.Get(gettoken_url)
- defer req.Body.Close()
- body, _ := ioutil.ReadAll(req.Body)
-
- var json_str JSON
- json.Unmarshal([]byte(body), &json_str)
-
- 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
-
- 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()
-
-
-
- }
- 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 {
-
- Content string `json:"content"`
- }{Content: content},
- }
- sed_msg, _ := json.Marshal(msg)
-
- return string(sed_msg)
- }
- func main1() {
- touser := "BigBoss"
- toparty := "2"
- agentid:= 1000002
- corpid := "xxxxxxxxxxxxxxxxx"
- corpsecret := "exxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- accessToken := Get_AccessToken(corpid,corpsecret)
- content := os.Args[1]
-
-
- msg := strings.Replace(messages(touser,toparty,agentid,content),"\\\\","\\",-1)
-
- Send_Message(accessToken,msg)
- }
|