|
- package main
-
- import (
- "bytes"
- "fmt"
- "io/ioutil"
- "net/http"
- )
-
- //menu is a json definition
- var menu = []byte(`
- {
- "button":[
- {
- "name":"Member",
- "sub_button":[
- {
- "type":"click",
- "name":"My Profile",
- "key": "MEMBER_MYPROFILE"
- },
- {
- "type":"click",
- "name":"Credits",
- "key": "MEMBER_CREDITS"
- },
-
- {
- "type":"click",
- "name":"Search",
- "key":"MEMBER_SEARCH"
- },
- {
- "type":"click",
- "name":"Broadcast",
- "key":"MEMBER_BROADCAST"
- }
-
- ]
- },
- {
- "name":"Events",
- "sub_button":[
- {
- "type":"click",
- "name":"Start One",
- "key": "EVENT_STARTONE"
- },
- {
- "type":"click",
- "name":"Contribute Photo",
- "key": "EVENT_CONTRIBUTEPHOTO"
- },
- {
- "type":"click",
- "name":"Check in (on arrival)",
- "key": "EVENT_CHECKIN"
- },
-
- {
- "type":"click",
- "name":"Lucky Draw",
- "key": "EVENT_LUCKYDRAW"
- },
-
- {
- "type":"click",
- "name":"Upcoming",
- "key":"EVENTS_UPCOMING"
- }]
- },
- {
- "name":"Career Development",
- "sub_button":[
- {
- "type":"click",
- "name":"Job Opportunities",
- "key": "CAREERDEV_JOBS"
- },
- {
- "type":"click",
- "name":"Local Industry",
- "key": "CARDEV_INDUSTRY"
- },
-
- {
- "type":"click",
- "name":"Internatioal Opportunity",
- "key":"CARDEV_TRANSPORT"
- }]
- }
- ]
- }
- `)
-
- //CreateCustomMenu bulid a menu for wechat windows
- // 3 main manu max (< 4 mandarin words)
- // 5 sub items max (< 7 mandarin words)
- //success return nil
- //error return err
- func CreateCustomMenu(m []byte) error {
- atoken, err := GetAccessToken()
- if err != nil {
- return err
- }
- url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", atoken)
- if len(m) == 0 {
- m = menu //using default menu
- }
- return jsonPostRequest(url, m)
-
- }
-
- //CreateDefaultMenu build a menu for wechat window
- //
- func CreateDefaultMenu() error {
- var b []byte
- return CreateCustomMenu(b)
- }
-
- //jsonPostRequest perform http post request with json body
- // url is the url to post to
- // jsonBody is the payload in http post request
- func jsonPostRequest(url string, jsonBody []byte) error {
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
- if err != nil {
- return err
- }
- req.Header.Set("X-Custom-Header", "Patrick Sun")
- req.Header.Set("Content-Type", "application/json")
-
- client := &http.Client{}
- resp, err := client.Do(req)
-
- if err != nil {
- return err
- }
- defer resp.Body.Close()
-
- fmt.Println("response Status:", resp.Status)
- fmt.Println("response Headers:", resp.Header)
- body, _ := ioutil.ReadAll(resp.Body)
- fmt.Println("response Body:", string(body))
-
- return nil
- }
-
- func GetMenu() {
-
- }
|