workwechat.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package util
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "strings"
  8. jsoniter "github.com/json-iterator/go"
  9. )
  10. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  11. type JSON struct {
  12. Access_token string `json:"access_token"`
  13. }
  14. type MESSAGES struct {
  15. Touser string `json:"touser"`
  16. Toparty string `json:"toparty"`
  17. Msgtype string `json:"msgtype"`
  18. Agentid int `json:"agentid"`
  19. Text struct {
  20. //Subject string `json:"subject"`
  21. Content string `json:"content"`
  22. } `json:"text"`
  23. Safe int `json:"safe"`
  24. }
  25. func Get_AccessToken(corpid, corpsecret string) string {
  26. gettoken_url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret
  27. //print(gettoken_url)
  28. client := &http.Client{}
  29. req, e := client.Get(gettoken_url)
  30. if e != nil {
  31. panic(e)
  32. }
  33. defer req.Body.Close()
  34. body, _ := ioutil.ReadAll(req.Body)
  35. //fmt.Printf("\n%q",string(body))
  36. var json_str JSON
  37. json.Unmarshal([]byte(body), &json_str)
  38. //fmt.Printf("\n%q",json_str.Access_token)
  39. return json_str.Access_token
  40. }
  41. func Send_Message(access_token, msg string) {
  42. send_url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token
  43. //print(send_url)
  44. client := &http.Client{}
  45. req, _ := http.NewRequest("POST", send_url, bytes.NewBuffer([]byte(msg)))
  46. req.Header.Set("Content-Type", "application/json")
  47. req.Header.Set("charset", "UTF-8")
  48. resp, err := client.Do(req)
  49. if err != nil {
  50. panic(err)
  51. }
  52. defer resp.Body.Close()
  53. //fmt.Println("response Status:", resp.Status)
  54. //body, _ := ioutil.ReadAll(resp.Body)
  55. //fmt.Println("response Body:", string(body))
  56. }
  57. func messages(touser string, toparty string, agentid int, content string) string {
  58. msg := MESSAGES{
  59. Touser: touser,
  60. Toparty: toparty,
  61. Msgtype: "text",
  62. Agentid: agentid,
  63. Safe: 0,
  64. Text: struct {
  65. //Subject string `json:"subject"`
  66. Content string `json:"content"`
  67. }{Content: content},
  68. }
  69. sed_msg, _ := json.Marshal(msg)
  70. //fmt.Printf("%s",string(sed_msg))
  71. return string(sed_msg)
  72. }
  73. func main() {
  74. touser := "BigBoss" //企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
  75. toparty := "2" //企业号中的部门id。
  76. agentid := 1000002 //企业号中的应用id。
  77. corpid := "xxxxxxxxxxxxxxxxx" //企业号的标识
  78. corpsecret := "exxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ///企业号中的应用的Secret
  79. accessToken := Get_AccessToken(corpid, corpsecret)
  80. content := os.Args[1]
  81. // fmt.Println(content)
  82. // 序列化成json之后,\n会被转义,也就是变成了\\n,使用str替换,替换掉转义
  83. msg := strings.Replace(messages(touser, toparty, agentid, content), "\\\\", "\\", -1)
  84. // fmt.Println(strings.Replace(msg,"\\\\","\\",-1))
  85. Send_Message(accessToken, msg)
  86. }