Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

166 lines
3.7KB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. //menu is a json definition
  9. var menu = []byte(`
  10. {
  11. "button":[
  12. {
  13. "name":"会员信息",
  14. "sub_button":[
  15. {
  16. "type":"click",
  17. "name":"我的会员资料",
  18. "key": "MEMBER_MYPROFILE"
  19. },
  20. {
  21. "type":"click",
  22. "name":"积分/余额",
  23. "key": "MEMBER_CREDITS"
  24. },
  25. {
  26. "type":"click",
  27. "name":"搜索其他会员信息",
  28. "key":"MEMBER_SEARCH"
  29. },
  30. {
  31. "type":"click",
  32. "name":"匿名投诉",
  33. "key":"MEMBER_FEEDBACK"
  34. },
  35. {
  36. "type": "view",
  37. "name":"校友会主页",
  38. "url":"https://www.hitxy.org.au/"
  39. }
  40. ]
  41. },
  42. {
  43. "name":"组织活动",
  44. "sub_button":[
  45. {
  46. "type":"click",
  47. "name":"我来发起一次活动",
  48. "key": "EVENT_STARTONE"
  49. },
  50. {
  51. "type":"click",
  52. "name":"观看当前活动的直播",
  53. "key": "EVENT_LIVE"
  54. },
  55. {
  56. "type":"scancode_waitmsg",
  57. "name":"签到当前活动",
  58. "key": "EVENT_CHECKIN"
  59. },
  60. {
  61. "type":"click",
  62. "name":"参与幸运抽奖",
  63. "key": "EVENT_LUCKYDRAW"
  64. },
  65. {
  66. "type":"click",
  67. "name":"即将举办的活动",
  68. "key":"EVENTS_UPCOMING"
  69. }]
  70. }
  71. ]
  72. }
  73. `)
  74. // ,
  75. // {
  76. // "name":"职业发展",
  77. // "sub_button":[
  78. // {
  79. // "type":"click",
  80. // "name":"澳洲工作机会",
  81. // "key": "CAREERDEV_JOBS"
  82. // },
  83. // {
  84. // "type":"click",
  85. // "name":"融入澳洲社区",
  86. // "key": "CAREERDEV_INDUSTRY"
  87. // },
  88. // {
  89. // "type":"click",
  90. // "name":"OutReach Program",
  91. // "key": "CAREERDEV_OUTREACH"
  92. // },
  93. // {
  94. // "type":"click",
  95. // "name":"母校/国内纽带",
  96. // "key": "CAREERDEV_HIT"
  97. // },
  98. // {
  99. // "type":"click",
  100. // "name":"校友互助信息",
  101. // "key":"CAREERDEV_HELP"
  102. // }]
  103. // }
  104. //CreateCustomMenu bulid a menu for wechat windows
  105. // 3 main manu max (< 4 mandarin words)
  106. // 5 sub items max (< 7 mandarin words)
  107. //success return nil
  108. //error return err
  109. func CreateCustomMenu(m []byte) error {
  110. atoken, err := GetAccessToken()
  111. if err != nil {
  112. return err
  113. }
  114. url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", atoken)
  115. if len(m) == 0 {
  116. m = menu //using default menu
  117. }
  118. return jsonPostRequest(url, m)
  119. }
  120. //CreateDefaultMenu build a menu for wechat window
  121. //
  122. func CreateDefaultMenu() error {
  123. var b []byte
  124. return CreateCustomMenu(b)
  125. }
  126. //jsonPostRequest perform http post request with json body
  127. // url is the url to post to
  128. // jsonBody is the payload in http post request
  129. func jsonPostRequest(url string, jsonBody []byte) error {
  130. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
  131. if err != nil {
  132. return err
  133. }
  134. req.Header.Set("X-Custom-Header", "Patrick Sun")
  135. req.Header.Set("Content-Type", "application/json")
  136. client := &http.Client{}
  137. resp, err := client.Do(req)
  138. if err != nil {
  139. return err
  140. }
  141. defer resp.Body.Close()
  142. fmt.Println("response Status:", resp.Status)
  143. fmt.Println("response Headers:", resp.Header)
  144. body, _ := ioutil.ReadAll(resp.Body)
  145. fmt.Println("response Body:", string(body))
  146. return nil
  147. }
  148. func GetMenu() {
  149. }