微信公众平台C#开发系列(八):查询自定义菜单
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
{
/// <summary>
/// 查询当前的自定义菜单结果
/// </summary>
public class QueryingCustomMenusResponse : ErrorMessage
{
/// <summary>
/// 菜单是否开启,0代表未开启,1代表开启
/// </summary>
public int is_menu_open { get; set; }
/// <summary>
/// 菜单信息
/// </summary>
public Selfmenu_Info selfmenu_info { get; set; }
}
public class Selfmenu_Info
{
/// <summary>
/// 菜单按钮
/// </summary>
public Button[] button { get; set; }
}
public class Button
{
/// <summary>
/// 菜单的类型,公众平台官网上能够设置的菜单类型
///有view(跳转网页)、text(返回文本,下同)、img、photo、video、voice。
///使用API设置的则有8种,详见《自定义菜单创建接口》
/// </summary>
public string type { get; set; }
/// <summary>
/// 菜单名称
/// </summary>
public string name { get; set; }
/// <summary>
///
/// </summary>
public string key { get; set; }
/// <summary>
/// 子菜单列表
/// </summary>
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
{
/// <summary>
/// 自定义菜单查询接口
/// </summary>
public class QueryingCustomMenus : WeiXinRequest<QueryingCustomMenusResponse>, IWeiXin<QueryingCustomMenusResponse>
{
String api;
public QueryingCustomMenus(string access_token)
{
this.api = $"https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={access_token}";
}
/// <summary>
/// 调用接口获取结果
/// </summary>
/// <returns></returns>
public QueryingCustomMenusResponse GetResponse()
{
return this.Get(api);
}
}
}
调用代码例子
string access_token = token.access_token;
IWeiXin<QueryingCustomMenusResponse> api = new QueryingCustomMenus(access_token);
QueryingCustomMenusResponse response = api.GetResponse();
调用成功返回的结果