12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- #region Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- either express or implied. See the License for the specific language governing permissions
- and limitations under the License.
- Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
- ----------------------------------------------------------------*/
- #endregion Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright (C) 2019 Senparc
-
- 文件名:CustomAPI.cs
- 文件功能描述:客服接口
-
-
- 创建标识:Senparc - 20150211
-
- 修改标识:Senparc - 20150303
- 修改描述:整理接口
-
- 修改标识:Senparc - 20150312
- 修改描述:开放代理请求超时时间
-
- 修改标识:Senparc - 20160718
- 修改描述:增加其接口的异步方法
-
- 修改标识:Senparc - 20160722
- 修改描述:将其SendText方法增加了kfAccount的参数
-
- 修改标识:Senparc - 20160802
- 修改描述:将其Send方法增加了kfAccount的参数
-
- 创建标识:Senparc - 20160808
- 创建描述:增加SendCard
- 创建标识:Senparc - 20161224
- 创建描述:SendVideo方法添加thumb_media_id参数 感谢 @hello2008zj
- 修改标识:Senparc - 20170707
- 修改描述:v14.5.1 完善异步方法async/await
- 修改标识:Senparc - 20180928
- 修改描述:增加GetTypingStatus
- 修改标识:Senparc - 20190129
- 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
- ----------------------------------------------------------------*/
- /*
- API地址:http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
- 新地址(2019年3月):https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- */
- using Senparc.CO2NET.Extensions;
- using Senparc.CO2NET.Helpers.Serializers;
- using Senparc.NeuChar;
- using Senparc.NeuChar.Entities;
- using Senparc.Weixin.CommonAPIs;
- using Senparc.Weixin.Entities;
- using Senparc.Weixin.MP.CommonAPIs;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Senparc.Weixin.MP.AdvancedAPIs
- {
- /// <summary>
- /// 客服接口
- /// </summary>
- public static class CustomApi
- {
- /// <summary>
- /// 客服消息统一请求地址格式
- /// </summary>
- public static readonly string UrlFormat = Config.ApiMpHost + "/cgi-bin/message/custom/send?access_token={0}";
- #region 同步方法
- /// <summary>
- /// 发送文本信息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="content"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendText", true)]
- public static WxJsonResult SendText(string accessTokenOrAppId, string openId, string content,
- int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "text",
- text = new
- {
- content = content
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "text",
- text = new
- {
- content = content
- },
- customservice = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送图片消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendImage", true)]
- public static WxJsonResult SendImage(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "image",
- image = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "image",
- image = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送语音消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendVoice", true)]
- public static WxJsonResult SendVoice(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "voice",
- voice = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "voice",
- voice = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送视频消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="title"></param>
- /// <param name="description"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <param name="thumb_media_id"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendVideo", true)]
- public static WxJsonResult SendVideo(string accessTokenOrAppId, string openId, string mediaId, string title, string description, int timeOut = Config.TIME_OUT, string kfAccount = "", string thumb_media_id = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "video",
- video = new
- {
- media_id = mediaId,
- thumb_media_id = thumb_media_id,
- title = title,
- description = description
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "video",
- video = new
- {
- media_id = mediaId,
- thumb_media_id = thumb_media_id,
- title = title,
- description = description
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送音乐消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</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>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendMusic", true)]
- public static WxJsonResult SendMusic(string accessTokenOrAppId, string openId, string title, string description,
- string musicUrl, string hqMusicUrl, string thumbMediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "music",
- music = new
- {
- title = title,
- description = description,
- musicurl = musicUrl,
- hqmusicurl = hqMusicUrl,
- thumb_media_id = thumbMediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "music",
- music = new
- {
- title = title,
- description = description,
- musicurl = musicUrl,
- hqmusicurl = hqMusicUrl,
- thumb_media_id = thumbMediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送图文消息(点击跳转到外链)
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="articles"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendNews", true)]
- public static WxJsonResult SendNews(string accessTokenOrAppId, string openId, List<Article> articles, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- 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()
- }
- };
- }
- else
- {
- 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()
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送图文消息(点击跳转到图文消息页面)
- /// 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut"></param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendMpNews", true)]
- public static WxJsonResult SendMpNews(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "mpnews",
- mpnews = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "mpnews",
- mpnews = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送卡券
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="cardId"></param>
- /// <param name="cardExt"></param>
- /// <param name="timeOut"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendCard", true)]
- public static WxJsonResult SendCard(string accessTokenOrAppId, string openId, string cardId, CardExt cardExt, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var data = new
- {
- touser = openId,
- msgtype = "wxcard",
- wxcard = new
- {
- card_id = cardId,
- card_ext = cardExt
- }
- };
- JsonSetting jsonSetting = new JsonSetting()
- {
- TypesToIgnoreNull = new List<System.Type>() { typeof(CardExt) }
- };
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut, jsonSetting: jsonSetting);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 客服输入状态
- /// </summary>
- /// <param name="accessTokenOrAppId"></param>
- /// <param name="cardId"></param>
- /// <param name="typingStatus"></param>
- /// <param name="timeOut"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.GetTypingStatus", true)]
- public static WxJsonResult GetTypingStatus(string accessTokenOrAppId, string cardId, string typingStatus, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/message/custom/typing?access_token={0}", accessToken.AsUrlData());
- var data = new
- {
- touser = cardId,
- command = typingStatus
- };
- return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 发送客户菜单消息
- /// </summary>
- /// <param name="accessTokenOrAppId"></param>
- /// <param name="openId">接受人员OPenid</param>
- /// <param name="head">标题</param>
- /// <param name="menuList">内容</param>
- /// <param name="tail">结尾内容</param>
- /// <param name="timeOut">超时时间</param>
- /// <returns></returns>
- public static WxJsonResult SendMenu(string accessTokenOrAppId, string openId,
- string head, List<SendMenuContent> menuList, string tail,
- int timeOut = Config.TIME_OUT)
- {
- var data = new
- {
- touser = openId,
- msgtype = "msgmenu",
- msgmenu = new
- {
- head_content = head,
- list = menuList,
- tail_content = tail
- }
- };
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- #endregion
- #if !NET35 && !NET40
- #region 异步方法
- /// <summary>
- /// 【异步方法】发送文本信息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="content"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendTextAsync", true)]
- public static async Task<WxJsonResult> SendTextAsync(string accessTokenOrAppId, string openId, string content, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (string.IsNullOrEmpty(kfAccount))
- {
- data = new
- {
- touser = openId,
- msgtype = "text",
- text = new
- {
- content = content
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "text",
- text = new
- {
- content = content
- },
- customservice = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送图片消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendImageAsync", true)]
- public static async Task<WxJsonResult> SendImageAsync(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "image",
- image = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "image",
- image = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送语音消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendVoiceAsync", true)]
- public static async Task<WxJsonResult> SendVoiceAsync(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "voice",
- voice = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "voice",
- voice = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送视频消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="title"></param>
- /// <param name="description"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <param name="thumb_media_id"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendVideoAsync", true)]
- public static async Task<WxJsonResult> SendVideoAsync(string accessTokenOrAppId, string openId, string mediaId, string title, string description, int timeOut = Config.TIME_OUT, string kfAccount = "", string thumb_media_id = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "video",
- video = new
- {
- media_id = mediaId,
- thumb_media_id = thumb_media_id,
- title = title,
- description = description
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "video",
- video = new
- {
- media_id = mediaId,
- thumb_media_id = thumb_media_id,
- title = title,
- description = description
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送音乐消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</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>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendMusicAsync", true)]
- public static async Task<WxJsonResult> SendMusicAsync(string accessTokenOrAppId, string openId, string title, string description,
- string musicUrl, string hqMusicUrl, string thumbMediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "music",
- music = new
- {
- title = title,
- description = description,
- musicurl = musicUrl,
- hqmusicurl = hqMusicUrl,
- thumb_media_id = thumbMediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "music",
- music = new
- {
- title = title,
- description = description,
- musicurl = musicUrl,
- hqmusicurl = hqMusicUrl,
- thumb_media_id = thumbMediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送图文消息
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="articles"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendNewsAsync", true)]
- public static async Task<WxJsonResult> SendNewsAsync(string accessTokenOrAppId, string openId, List<Article> articles, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- 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()
- }
- };
- }
- else
- {
- 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()
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送图文消息(点击跳转到图文消息页面)
- /// 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="mediaId"></param>
- /// <param name="timeOut"></param>
- /// <param name="kfAccount">客服</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendMpNewsAsync", true)]
- public static async Task<WxJsonResult> SendMpNewsAsync(string accessTokenOrAppId, string openId, string mediaId, int timeOut = Config.TIME_OUT, string kfAccount = "")
- {
- object data = null;
- if (kfAccount.IsNullOrWhiteSpace())
- {
- data = new
- {
- touser = openId,
- msgtype = "mpnews",
- mpnews = new
- {
- media_id = mediaId
- }
- };
- }
- else
- {
- data = new
- {
- touser = openId,
- msgtype = "mpnews",
- mpnews = new
- {
- media_id = mediaId
- },
- CustomService = new
- {
- kf_account = kfAccount
- }
- };
- }
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送卡券
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="cardId"></param>
- /// <param name="cardExt"></param>
- /// <param name="timeOut"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.SendCardAsync", true)]
- public static async Task<WxJsonResult> SendCardAsync(string accessTokenOrAppId, string openId, string cardId, CardExt cardExt, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var data = new
- {
- touser = openId,
- msgtype = "wxcard",
- wxcard = new
- {
- card_id = cardId,
- card_ext = cardExt
- }
- };
- JsonSetting jsonSetting = new JsonSetting()
- {
- TypesToIgnoreNull = new List<System.Type>() { typeof(CardExt) }
- };
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut, jsonSetting: jsonSetting);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】客服输入状态
- /// </summary>
- /// <param name="accessTokenOrAppId"></param>
- /// <param name="cardId"></param>
- /// <param name="typingStatus"></param>
- /// <param name="timeOut"></param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomApi.TypingAsync", true)]
- public static async Task<WxJsonResult> GetTypingStatusAsync(string accessTokenOrAppId, string cardId, string typingStatus, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/message/custom/typing?access_token={0}", accessToken.AsUrlData());
- var data = new
- {
- card_id = cardId,
- command = typingStatus
- };
- return await Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】发送客户菜单消息
- /// </summary>
- /// <param name="accessTokenOrAppId"></param>
- /// <param name="accessTokenOrAppId"></param>
- /// <param name="openId">接受人员OPenid</param>
- /// <param name="head">标题</param>
- /// <param name="menuList">内容</param>
- /// <param name="tail">结尾内容</param>
- /// <param name="timeOut">超时时间</param>
- /// <returns></returns>
- public static async Task<WxJsonResult> SendMenuAsync(string accessTokenOrAppId, string openId,
- string head, List<SendMenuContent> menuList, string tail,
- int timeOut = Config.TIME_OUT)
- {
- var data = new
- {
- touser = openId,
- msgtype = "msgmenu",
- msgmenu = new
- {
- head_content = head,
- list = menuList,
- tail_content = tail
- }
- };
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- #endregion
- #endif
- /////
- ///// 发送卡券 查看card_ext字段详情及签名规则,特别注意客服消息接口投放卡券仅支持非自定义Code码的卡券。
- /////
- }
- }
|