c#调用接口获取当前微信公众号设置的菜单内容
获取公众号当前使用的自定义菜单的配置信息,如果公众号是通过API调用设置的菜单,则返回菜单的开发配置,而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。
接口文档地址:
https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Querying_Custom_Menus.html#
接着上一篇:微信公众平台C#开发系列(七):创建自定义菜单
部分使用类方法详情 请参考系列四 ,本文不再重复赘述
根据文档我们先定义一个QueryingCustomMenusResponse类继承 ErrorMessage 用于解析接口结果
ErrorMessage 公共类参考 : 微信公众平台C#开发系列(四):获取access token凭证
namespace YuanTK.WeiXin.Wrapper { ////// 查询当前的自定义菜单结果 /// public class QueryingCustomMenusResponse : ErrorMessage { /// /// 菜单是否开启,0代表未开启,1代表开启 /// public int is_menu_open { get; set; } /// /// 菜单信息 /// public Selfmenu_Info selfmenu_info { get; set; } } public class Selfmenu_Info { /// /// 菜单按钮 /// public Button[] button { get; set; } } public class Button { /// /// 菜单的类型,公众平台官网上能够设置的菜单类型 ///有view(跳转网页)、text(返回文本,下同)、img、photo、video、voice。 ///使用API设置的则有8种,详见《自定义菜单创建接口》 /// public string type { get; set; } /// /// 菜单名称 /// public string name { get; set; } /// /// /// public string key { get; set; } /// /// 子菜单列表 /// public Sub_Button sub_button { get; set; } } public class Sub_Button { public ChildButton[] list { get; set; } } public class ChildButton { public string type { get; set; } public string name { get; set; } public string url { get; set; } public string key { get; set; } } }
定义一个QueryingCustomMenus方法类继承WeiXinRequest 和接口IWeiXin
namespace YuanTK.WeiXin { ////// 自定义菜单查询接口 /// public class QueryingCustomMenus : WeiXinRequest , IWeiXin { String api; public QueryingCustomMenus(string access_token) { this.api = $"https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={access_token}"; } /// /// 调用接口获取结果 /// /// public QueryingCustomMenusResponse GetResponse() { return this.Get(api); } } }
调用代码例子
string access_token = token.access_token;
IWeiXin api = new QueryingCustomMenus(access_token);
QueryingCustomMenusResponse response = api.GetResponse();
调用成功返回的结果