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