/// <summary> /// 发送语音消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="mediaId"></param> /// <returns></returns> public static bool SendVoice(string companyId, string accessToken, string openId, string mediaId) { var data = new { touser = openId, msgtype = "voice", voice = new { media_id = mediaId } }; ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送语音消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
public static bool SendTemplateMessage <T>(string companyId, string accessToken, string openId, string templateId, string topcolor, T data) { var pdata = new Templete { template_id = templateId, topcolor = topcolor, touser = openId, data = data }; ResError result = WxHttp.Post(WxUrl.TemplateSend.ToFormat(accessToken), pdata); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.TemplateSend.ToFormat(newAccessToken), pdata); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送模板消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
/// <summary> /// 删除群发 /// 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片。 /// </summary> /// <param name="accessToken"></param> /// <param name="msgId">发送出去的消息ID</param> /// <returns></returns> public static bool SendAllDelete(string companyId, string accessToken, long msgId) { var data = new { msgid = msgId }; ResError result = WxHttp.Post(WxUrl.SendAllDelete.ToFormat(accessToken), data); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.SendAllDelete.ToFormat(newAccessToken), data); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "删除群发发生错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
/// <summary> /// 移动用户分组 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="toGroupId"></param> /// <returns></returns> public static bool UpdateUserGroup(string companyId, string accessToken, string openId, int toGroupId) { var data = new { openid = openId, to_groupid = toGroupId }; ResError result = WxHttp.Post(WxUrl.UpdateUserGroup.ToFormat(accessToken), data); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.UpdateUserGroup.ToFormat(newAccessToken), data); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "移动用户分组发生错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
/// <summary> /// 发送音乐消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="title">音乐标题(非必须)</param> /// <param name="description">音乐描述(非必须)</param> /// <param name="musicUrl">音乐链接</param> /// <param name="hqMusicUrl">高品质音乐链接,wifi环境优先使用该链接播放音乐</param> /// <param name="thumbMediaId">视频缩略图的媒体ID</param> /// <returns></returns> public static bool SendMusic(string companyId, string accessToken, string openId, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaId) { var data = new { touser = openId, msgtype = "music", music = new { title = title, description = description, musicurl = musicUrl, hqmusicurl = hqMusicUrl, thumb_media_id = thumbMediaId } }; ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送音乐消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
public static bool DeleteMenu(string companyId, string accessToken) { ResError result = WxHttp.Get(WxUrl.DeleteMenu.ToFormat(accessToken)); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Get(WxUrl.DeleteMenu.ToFormat(newAccessToken)); return(newResult.errcode == ResCode.请求成功); } return(false); }
/// <summary> /// 创建菜单 /// </summary> /// <param name="accessToken"></param> /// <param name="mainButton"></param> /// <returns></returns> public static bool CreateMenu(string companyId, string accessToken, MainButton mainButton) { ResError result = WxHttp.Post(WxUrl.CreateMenu.ToFormat(accessToken), mainButton); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.CreateMenu.ToFormat(newAccessToken), mainButton); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "微信创建菜单错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }
/// <summary> /// 发送图文消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="articles"></param> /// <returns></returns> public static bool SendNews(string companyId, string accessToken, string openId, List <Article> articles) { var data = new { touser = openId, msgtype = "news", news = new { articles = articles.Select(z => new { title = z.Title, description = z.Description, url = z.Url, picurl = z.PicUrl//图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80 }).ToList() } }; ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data); if (result.errcode == ResCode.请求成功) { return(true); } if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效) { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); var newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true); ResError newResult = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data); if (newResult.errcode == ResCode.请求成功) { return(true); } } TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送图文消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg); return(false); }