c1896be3c964dfb65408b9989724cede58238f55.svn-base 2.7 KB

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