一聚教程网:一个值得你收藏的教程网站

热门教程

微信公众平台自定义菜单开发示例

时间:2022-06-24 23:07:57 编辑:袖梨 来源:一聚教程网

 腾讯微信3月19日正式公布公众平台自定义菜单功能。目前该功能仅提供给企业和机构,且内测资格需要申请。对于这一新增功能,目前仅有少数账号已经开通。至此,微信的商业化价值被进一步挖掘出来。

 

微信公众平台推出自定义菜单功能

 

   开发者可利用该功能为公众账号的会话界面底部增加自定义菜单。用户点击菜单中的选项,可以调出相应的回复信息或网页链接。

菜单创建
接口说明
通过POST一个特定结构体,实现在微信客户端创建自定义菜单。
请求说明
http请求方式:POST

https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

结构体其实就是一个json格式的字符串,相信很多朋友就卡在这里了,怎么往这个接口地址 post 一个结构体呢,实现代码如下:

 代码如下 复制代码

public static void main(String[] args) throws Exception {
    URL url = new URL("https://api.weixin.qq.com/cgi-bin
                                 /menu/create?access_token=TOKEN");
        String responeJsonStr = "{"+
                                     ""button":["+
                                     "{"name":"酒店预订","+
                                     ""sub_button":["+
                             "{"+
                                ""type":"click","+
                                ""name":"附近酒店","+
                                ""key":"jintoneinn488878-1""+
                            "},"+
                            "{"+
                                ""type":"click","+
                                ""name":"查找酒店","+
                                ""key":"jintoneinn488878-2""+
                            "}"+
                        "]"+
                    "}"
                "]"+
            "}";
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.connect();
        conn.getOutputStream().write(responeJsonStr.getBytes("utf-8"));
        InputStream is = conn.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        System.out.println(buffer.toString()); 
    }


菜单查询
接口说明

查询当前使用的自定义菜单结构。

请求说明

http请求方式:GET

https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
返回说明

对应创建接口,正确的Json返回结果:

 代码如下 复制代码

{"menu":{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC","sub_button":[]},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER","sub_button":[]},{"name":"菜单","sub_button":[{"type":"click","name":"hello word","key":"V1001_HELLO_WORLD","sub_button":[]},{"type":"click","name":"赞一下我们","key":"V1001_GOOD","sub_button":[]}]}]}}


菜单删除
接口说明

取消当前使用的自定义菜单。

请求说明

http请求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
返回说明

对应创建接口,正确的Json返回结果:

 代码如下 复制代码
{"errcode":0,"errmsg":"ok"}

 

热门栏目