/// <summary> /// 根据OpenId获取分组Id。 /// </summary> /// <param name="openId">用户唯一标识符。</param> /// <returns>分组Id。</returns> public ulong GetGroupByOpenId(string openId) { var url = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=" + _accountModel.GetAccessToken(); var data = WeiXinHttpHelper.Post(url, new { openid = openId }); var content = Encoding.UTF8.GetString(data); return(JObject.Parse(content).Value <ulong>("groupid")); }
/// <summary> /// 删除门店。 /// </summary> /// <param name="storeId">门店Id。</param> /// <remarks>删除已经成功创建的门店。请商户慎重调用该接口,门店信息被删除后,可能会影响其他与门店相关的业务使用,如卡券等。同样,该门店信息也不会在微信的商户详情页显示,不会再推荐入附近功能。</remarks> public void Delete(ulong storeId) { var url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { poi_id = storeId }); }
/// <summary> /// 添加多图文素材。 /// </summary> /// <param name="articles">文章模型。</param> /// <returns>图文素材Id。</returns> public string AddNews(NewsArticleModel[] articles) { var url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=" + _accountModel.GetAccessToken(); var data = WeiXinHttpHelper.Post(url, new { articles }); var content = Encoding.UTF8.GetString(data); ResultHelper.TryThrowError(content); return(JObject.Parse(content).Value <string>("media_id")); }
/// <summary> /// 设置自定义菜单。 /// </summary> /// <param name="menus">自定义菜单数组。</param> /// <exception cref="ArgumentNullException"><paramref name="menus"/> 为null。</exception> /// <exception cref="ArgumentException"><paramref name="menus"/> 长度超过3。</exception> public void Set(CustomMenuButtonBase[] menus) { if (menus.NotNull("menus").Length > 3) { throw new ArgumentException("顶级菜单项不能超过3个。", "menus"); } var url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { button = menus.Select(GetMenuItem) }); }
/// <summary> /// 创建会话。 /// </summary> /// <param name="openId">客户openid</param> /// <param name="account">完整客服账号,格式为:账号前缀@公众号微信号</param> /// <param name="text">附加信息,文本会展示在客服人员的多客服客户端</param> /// <remarks>开发者可以使用本接口,为多客服的客服工号创建会话,将某个客户直接指定给客服工号接待,需要注意此接口不会受客服自动接入数以及自动接入开关限制。只能为在线的客服(PC客户端在线,或者已绑定多客服助手)创建会话。</remarks> public void Create(string openId, string account, string text = null) { var url = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { kf_account = account, openid = openId, text }); }
/// <summary> /// 添加客服账号(每个公众号最多添加10个客服账号)。 /// </summary> /// <param name="account">完整客服账号,格式为:账号前缀@公众号微信号</param> /// <param name="password">客服账号登录密码,格式为密码明文的32位加密MD5值。该密码仅用于在公众平台官网的多客服功能中使用,若不使用多客服功能,则不必设置密码</param> /// <param name="nickName">客服昵称,最长6个汉字或12个英文字符</param> public void AddAccount(string account, string password, string nickName) { account.NotEmptyOrWhiteSpace("account"); password.NotEmptyOrWhiteSpace("password"); var url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { kf_account = account, nickname = nickName, password }); }
/// <summary> /// 更新卡券Code。 /// </summary> /// <param name="code">需变更的Code码。</param> /// <param name="newCode">变更后的有效Code码。</param> /// <param name="cardId">卡券ID。自定义Code码卡券为必填。</param> public void UpdateCode(string code, string newCode, string cardId = null) { var url = "https://api.weixin.qq.com/card/code/update?access_token=" + _accountModel.GetAccessToken(); object postData; if (string.IsNullOrWhiteSpace(cardId)) { postData = new { code, new_code = newCode }; } else { postData = new { card_id = cardId, code, new_code = newCode }; } WeiXinHttpHelper.Post(url, postData); }
/// <summary> /// 设置客服账号的头像。 /// </summary> /// <param name="account">完整客服账号,格式为:账号前缀@公众号微信号</param> /// <param name="pictureData">客服人员的头像,头像图片文件必须是jpg格式,推荐使用640*640大小的图片以达到最佳效果</param> public void SetAccountHeadPicture(string account, byte[] pictureData) { if (!FileHelper.IsJpg(pictureData)) { throw new NotSupportedException("头像图片文件必须是jpg格式。"); } var url = string.Format("http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", _accountModel.GetAccessToken(), account); var createBytes = new CreateBytes(); var list = new List <byte[]> { createBytes.CreateFieldData("media", FileHelper.GetRandomFileName(pictureData), FileHelper.GetContentType(pictureData), pictureData), }; var data = createBytes.JoinBytes(list); WeiXinHttpHelper.Post(url, data, createBytes.ContentType); }
/// <summary> /// 设置自定义菜单。 /// </summary> /// <param name="menus">自定义菜单数组。</param> /// <param name="matchRule">匹配规则。</param> /// <exception cref="ArgumentNullException"><paramref name="menus"/> 为null。</exception> /// <exception cref="ArgumentException"><paramref name="menus"/> 长度超过3。</exception> /// <returns>如果是个性化菜单会返回菜单Id。</returns> public int?Set(CustomMenuButtonBase[] menus, CustomMeunMatchRule matchRule) { if (menus.NotNull("menus").Length > 3) { throw new ArgumentException("顶级菜单项不能超过3个。", nameof(menus)); } if (matchRule == null) { var url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { button = menus.Select(GetMenuItem) }); return(null); } else { var url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + _accountModel.GetAccessToken(); var result = WeiXinHttpHelper.PostString(url, new { button = menus.Select(GetMenuItem), matchrule = matchRule }); return(JObject.Parse(result).Value <int>("menuid")); } }
/// <summary> /// 设置用户备注。 /// </summary> /// <param name="openId">用户标识</param> /// <param name="remark">新的备注名,长度必须小于30字符</param> public void SetRemark(string openId, string remark) { var url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { openid = openId, remark }); }
/// <summary> /// 设置自定义菜单 /// </summary> /// <param name="jsonData"></param> public void Set(string jsonData) { var url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, jsonData); }
/// <summary> /// 删除标签 /// </summary> /// <param name="tagId">删除标签的编号</param> public void Delete(int tagId) { string url = $"https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={_accountModel.GetAccessToken()}"; WeiXinHttpHelper.Post(url, new { tag = new { id = tagId } }); }
/// <summary> /// 设置行业。 /// </summary> /// <param name="mainIndustryId">主行业Id。</param> /// <param name="deputyIndustryid">副行业Id。</param> /// <remarks>行业Id参考:http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html#toc。</remarks> public void SetIndustry(int mainIndustryId, int deputyIndustryid) { var postUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(postUrl, new { industry_id1 = mainIndustryId, industry_id2 = deputyIndustryid }); }
/// <summary> /// 批量移动用户分组。 /// </summary> /// <param name="openIds">用户唯一标识符openid的列表(size不能超过50)</param> /// <param name="toGroupId">新分组的Id。</param> public void MoveUsersGroup(string[] openIds, ulong toGroupId) { var url = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { openid_list = openIds, to_groupid = toGroupId }); }
/// <summary> /// 编辑标签 /// </summary> /// <param name="tagInfo">编辑的标签</param> public void Update(TagInfo tagInfo) { string url = $"https://api.weixin.qq.com/cgi-bin/tags/update?access_token={_accountModel.GetAccessToken()}"; WeiXinHttpHelper.Post(url, new { tag = new { id = tagInfo.Id, name = tagInfo.Name } }); }
private byte[] GetMaterial(string mediaId) { var url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + _accountModel.GetAccessToken(); return(WeiXinHttpHelper.Post(url, new { media_id = mediaId })); }
/// <summary> /// 批量为用户取消标签 /// </summary> /// <param name="tagId">标签编号</param> /// <param name="openIds">粉丝openid列表</param> public void BatchUntagging(int tagId, IEnumerable <string> openIds) { string url = $"https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={_accountModel.GetAccessToken()}"; WeiXinHttpHelper.Post(url, new { tagid = tagId, openid_list = openIds }); }
/// <summary> /// 删除一个永久素材。 /// </summary> /// <param name="mediaId">素材Id。</param> public void Delete(string mediaId) { var url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { media_id = mediaId }); }
/// <summary> /// 修改分组名称。 /// </summary> /// <param name="groupId">分组Id。</param> /// <param name="name">分组名称(30字以内)</param> public void ModifyGroupName(ulong groupId, string name) { var url = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { group = new { id = groupId, name } }); }
/// <summary> /// 发送消息。 /// </summary> /// <param name="message">客服消息。</param> public void Send(CustomServiceMessage message) { var url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, message.GetJson()); }
/// <summary> /// 删除分组(删除分组后,所有该分组内的用户自动进入默认分组)。 /// </summary> /// <param name="groupId">分组Id。</param> public void Delete(ulong groupId) { var url = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=" + _accountModel.GetAccessToken(); WeiXinHttpHelper.Post(url, new { group = new { id = groupId } }); }
/// <summary> /// 发送群组消息。 /// </summary> /// <param name="filter">群组的筛选信息。</param> /// <param name="message">群组消息。</param> public void SendByGroup(GroupFilter filter, GroupMessage message) { var url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + _accountModel.GetAccessToken(); object filterObj; if (filter.GroupId.HasValue) { filterObj = new { is_to_all = false, group_id = filter.GroupId.Value }; } else { filterObj = new { is_to_all = true }; } string msgtype; switch (message.Type) { case GroupMessageType.News: msgtype = "mpnews"; break; case GroupMessageType.Card: msgtype = "wxcard"; break; case GroupMessageType.Image: msgtype = "image"; break; case GroupMessageType.Music: msgtype = "music"; break; case GroupMessageType.Text: msgtype = "text"; break; case GroupMessageType.Video: msgtype = "mpvideo"; var videoMessage = (GroupMessageVideo)message; var uploadVideoUrl = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=" + _accountModel.GetAccessToken(); var result = WeiXinHttpHelper.PostString(uploadVideoUrl, new { media_id = videoMessage.MediaId, title = videoMessage.Title, description = videoMessage.Description }); videoMessage.MediaId = JObject.Parse(result)["media_id"].Value <string>(); break; case GroupMessageType.Voice: msgtype = "voice"; break; default: throw new NotSupportedException("不支持的消息类型:" + message.Type); } var postJson = JsonConvert.SerializeObject(new { filter = filterObj, msgtype, }); var postObj = JObject.Parse(postJson); postObj[msgtype] = JObject.Parse(JsonConvert.SerializeObject(message)); WeiXinHttpHelper.Post(url, postObj.ToString()); }