CustomServiceApi.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:CustomServiceAPI.cs
  17. 文件功能描述:多客服接口
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150306
  22. 修改描述:增加多客服接口
  23. 修改标识:Senparc - 20160520
  24. 修改描述:增加邀请绑定客服帐号接口
  25. 修改标识:Senparc - 20150312
  26. 修改描述:开放代理请求超时时间
  27. 修改标识:Senparc - 20160718
  28. 修改描述:增加其接口的异步方法
  29. 修改标识:Senparc - 20170609
  30. 修改描述:v14.4.12 修复 CustomServiceApi.GetMsgList() 方法时间戳问题
  31. 修改标识:Senparc - 20170707
  32. 修改描述:v14.5.1 完善异步方法async/await
  33. 修改标识:Senparc - 20170522
  34. 修改描述:v16.6.2 修改 DateTime 为 DateTimeOffset
  35. ----------------------------------------------------------------*/
  36. /*
  37. 多客服接口聊天记录接口,官方API:http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E5%AE%A2%E6%9C%8D%E8%81%8A%E5%A4%A9%E8%AE%B0%E5%BD%95
  38. */
  39. using System;
  40. using System.Collections.Generic;
  41. using System.Threading.Tasks;
  42. using Senparc.CO2NET.Extensions;
  43. using Senparc.CO2NET.Helpers;
  44. using Senparc.NeuChar;
  45. using Senparc.Weixin.Entities;
  46. using Senparc.Weixin.Helpers;
  47. using Senparc.CO2NET.HttpUtility;
  48. using Senparc.Weixin.MP.AdvancedAPIs.CustomService;
  49. using Senparc.Weixin.MP.CommonAPIs;
  50. using Senparc.Weixin.CommonAPIs;
  51. namespace Senparc.Weixin.MP.AdvancedAPIs
  52. {
  53. /// <summary>
  54. /// 多客服接口
  55. /// </summary>
  56. public static class CustomServiceApi
  57. {
  58. #region 同步方法
  59. /// <summary>
  60. /// 获取用户聊天记录
  61. /// </summary>
  62. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  63. /// <param name="startTime">查询开始时间,会自动转为UNIX时间戳</param>
  64. /// <param name="endTime">查询结束时间,会自动转为UNIX时间戳,每次查询不能跨日查询</param>
  65. /// <param name="pageSize">每页大小,每页最多拉取1000条</param>
  66. /// <param name="pageIndex">查询第几页,从1开始</param>
  67. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  68. /// <returns></returns>
  69. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetRecord", true)]
  70. public static GetRecordResult GetRecord(string accessTokenOrAppId, DateTime startTime, DateTime endTime, int pageSize = 10, int pageIndex = 1, int timeOut = Config.TIME_OUT)
  71. {
  72. return ApiHandlerWapper.TryCommonApi(accessToken =>
  73. {
  74. var urlFormat = Config.ApiMpHost + "/customservice/msgrecord/getrecord?access_token={0}";
  75. //规范页码
  76. if (pageSize <= 0)
  77. {
  78. pageSize = 1;
  79. }
  80. else if (pageSize > 50)
  81. {
  82. pageSize = 50;
  83. }
  84. //组装发送消息
  85. var data = new
  86. {
  87. starttime = DateTimeHelper.GetUnixDateTime(startTime),
  88. endtime = DateTimeHelper.GetUnixDateTime(endTime),
  89. pagesize = pageSize,
  90. pageindex = pageIndex
  91. };
  92. return CommonJsonSend.Send<GetRecordResult>(accessToken, urlFormat, data, timeOut: timeOut);
  93. }, accessTokenOrAppId);
  94. }
  95. /// <summary>
  96. /// 获取客服基本信息
  97. /// </summary>
  98. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  99. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  100. /// <returns></returns>
  101. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetCustomBasicInfo", true)]
  102. public static CustomInfoJson GetCustomBasicInfo(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  103. {
  104. return ApiHandlerWapper.TryCommonApi(accessToken =>
  105. {
  106. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/customservice/getkflist?access_token={0}", accessToken.AsUrlData());
  107. return CommonJsonSend.Send<CustomInfoJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  108. //return GetCustomInfoResult<CustomInfoJson>(urlFormat);
  109. }, accessTokenOrAppId);
  110. }
  111. /// <summary>
  112. /// 获取在线客服接待信息
  113. /// </summary>
  114. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  115. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  116. /// <returns></returns>
  117. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetCustomOnlineInfo", true)]
  118. public static CustomOnlineJson GetCustomOnlineInfo(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  119. {
  120. return ApiHandlerWapper.TryCommonApi(accessToken =>
  121. {
  122. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/customservice/getonlinekflist?access_token={0}", accessToken.AsUrlData());
  123. return CommonJsonSend.Send<CustomOnlineJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  124. //return GetCustomInfoResult<CustomOnlineJson>(urlFormat);
  125. }, accessTokenOrAppId);
  126. }
  127. //private static T GetCustomInfoResult<T>(string urlFormat)
  128. //{
  129. // var jsonString = HttpUtility.RequestUtility.HttpGet(urlFormat, Encoding.UTF8);
  130. // JavaScriptSerializer js = new JavaScriptSerializer();
  131. // return js.Deserialize<T>(jsonString);
  132. //}
  133. /// <summary>
  134. /// 添加客服账号
  135. /// </summary>
  136. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  137. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。如果没有公众号微信号,请前往微信公众平台设置。</param>
  138. /// <param name="nickName">客服昵称,最长6个汉字或12个英文字符</param>
  139. /// <param name="passWord">客服账号登录密码,格式为密码明文的32位加密MD5值</param>
  140. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  141. /// <returns></returns>
  142. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.AddCustom", true)]
  143. public static WxJsonResult AddCustom(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = Config.TIME_OUT)
  144. {
  145. return ApiHandlerWapper.TryCommonApi(accessToken =>
  146. {
  147. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/add?access_token={0}", accessToken.AsUrlData());
  148. var data = new
  149. {
  150. kf_account = kfAccount,
  151. nickname = nickName,
  152. password = passWord
  153. };
  154. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  155. }, accessTokenOrAppId);
  156. }
  157. /// <summary>
  158. /// 邀请绑定客服帐号
  159. /// </summary>
  160. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  161. /// <param name="kfAccount">完整客服帐号,格式为:帐号前缀@公众号微信号</param>
  162. /// <param name="inviteWx">接收绑定邀请的客服微信号</param>
  163. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  164. /// <returns></returns>
  165. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.InviteWorker", true)]
  166. public static WxJsonResult InviteWorker(string accessTokenOrAppId, string kfAccount, string inviteWx, int timeOut = Config.TIME_OUT)
  167. {
  168. return ApiHandlerWapper.TryCommonApi(accessToken =>
  169. {
  170. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/inviteworker?access_token={0}", accessToken.AsUrlData());
  171. var data = new
  172. {
  173. kf_account = kfAccount,
  174. invite_wx = inviteWx
  175. };
  176. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  177. }, accessTokenOrAppId);
  178. }
  179. /// <summary>
  180. /// 修改客服账号
  181. /// </summary>
  182. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  183. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。如果没有公众号微信号,请前往微信公众平台设置。</param>
  184. /// <param name="nickName">客服昵称,最长6个汉字或12个英文字符</param>
  185. /// <param name="passWord">客服账号登录密码,格式为密码明文的32位加密MD5值</param>
  186. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  187. /// <returns></returns>
  188. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.UpdateCustom", true)]
  189. public static WxJsonResult UpdateCustom(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = Config.TIME_OUT)
  190. {
  191. return ApiHandlerWapper.TryCommonApi(accessToken =>
  192. {
  193. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/update?access_token={0}", accessToken.AsUrlData());
  194. var data = new
  195. {
  196. kf_account = kfAccount,
  197. nickname = nickName,
  198. password = passWord
  199. };
  200. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  201. }, accessTokenOrAppId);
  202. }
  203. /// <summary>
  204. /// 上传客服头像
  205. /// </summary>
  206. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  207. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  208. /// <param name="file">form-data中媒体文件标识,有filename、filelength、content-type等信息</param>
  209. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  210. /// <returns></returns>
  211. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.UploadCustomHeadimg", true)]
  212. public static WxJsonResult UploadCustomHeadimg(string accessTokenOrAppId, string kfAccount, string file, int timeOut = Config.TIME_OUT)
  213. {
  214. return ApiHandlerWapper.TryCommonApi(accessToken =>
  215. {
  216. var url = string.Format(Config.ApiMpHost + "/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  217. var fileDictionary = new Dictionary<string, string>();
  218. fileDictionary["media"] = file;
  219. return Post.PostFileGetJson<WxJsonResult>(url, null, fileDictionary, null, timeOut: timeOut);
  220. }, accessTokenOrAppId);
  221. }
  222. /// <summary>
  223. /// 删除客服账号
  224. /// </summary>
  225. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  226. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  227. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  228. /// <returns></returns>
  229. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.DeleteCustom", true)]
  230. public static WxJsonResult DeleteCustom(string accessTokenOrAppId, string kfAccount, int timeOut = Config.TIME_OUT)
  231. {
  232. return ApiHandlerWapper.TryCommonApi(accessToken =>
  233. {
  234. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/del?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  235. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  236. }, accessTokenOrAppId);
  237. }
  238. /// <summary>
  239. /// 创建会话
  240. /// </summary>
  241. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  242. /// <param name="openId">客户openid</param>
  243. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  244. /// <param name="text">附加信息,文本会展示在客服人员的多客服客户端(非必须)</param>
  245. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  246. /// <returns></returns>
  247. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.CreateSession", true)]
  248. public static WxJsonResult CreateSession(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = Config.TIME_OUT)
  249. {
  250. return ApiHandlerWapper.TryCommonApi(accessToken =>
  251. {
  252. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/create?access_token={0}", accessToken.AsUrlData());
  253. var data = new
  254. {
  255. openid = openId,
  256. kf_account = kfAccount,
  257. text = text
  258. };
  259. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  260. }, accessTokenOrAppId);
  261. }
  262. /// <summary>
  263. /// 关闭会话
  264. /// </summary>
  265. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  266. /// <param name="openId">客户openid</param>
  267. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  268. /// <param name="text">附加信息,文本会展示在客服人员的多客服客户端(非必须)</param>
  269. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  270. /// <returns></returns>
  271. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.CloseSession", true)]
  272. public static WxJsonResult CloseSession(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = Config.TIME_OUT)
  273. {
  274. return ApiHandlerWapper.TryCommonApi(accessToken =>
  275. {
  276. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/close?access_token={0}", accessToken.AsUrlData());
  277. var data = new
  278. {
  279. openid = openId,
  280. kf_account = kfAccount,
  281. text = text
  282. };
  283. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  284. }, accessTokenOrAppId);
  285. }
  286. /// <summary>
  287. /// 获取客户的会话状态
  288. /// </summary>
  289. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  290. /// <param name="openId">客户openid</param>
  291. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  292. /// <returns></returns>
  293. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetSessionState", true)]
  294. public static GetSessionStateResultJson GetSessionState(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
  295. {
  296. return ApiHandlerWapper.TryCommonApi(accessToken =>
  297. {
  298. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getsession?access_token={0}&openid={1}", accessToken.AsUrlData(), openId.AsUrlData());
  299. return CommonJsonSend.Send<GetSessionStateResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  300. }, accessTokenOrAppId);
  301. }
  302. /// <summary>
  303. /// 获取客服的会话列表
  304. /// </summary>
  305. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  306. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。</param>
  307. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  308. /// <returns></returns>
  309. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetSessionList", true)]
  310. public static GetSessionListResultJson GetSessionList(string accessTokenOrAppId, string kfAccount, int timeOut = Config.TIME_OUT)
  311. {
  312. return ApiHandlerWapper.TryCommonApi(accessToken =>
  313. {
  314. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getsessionlist?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  315. return CommonJsonSend.Send<GetSessionListResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  316. }, accessTokenOrAppId);
  317. }
  318. /// <summary>
  319. /// 获取未接入会话列表
  320. /// </summary>
  321. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  322. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  323. /// <returns></returns>
  324. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetWaitCase", true)]
  325. public static GetWaitCaseResultJson GetWaitCase(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  326. {
  327. return ApiHandlerWapper.TryCommonApi(accessToken =>
  328. {
  329. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getwaitcase?access_token={0}", accessToken.AsUrlData());
  330. return CommonJsonSend.Send<GetWaitCaseResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  331. }, accessTokenOrAppId);
  332. }
  333. /// <summary>
  334. /// 获取聊天记录
  335. /// </summary>
  336. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  337. /// <param name="startTime">起始时间,unix时间戳</param>
  338. /// <param name="endTime">结束时间,unix时间戳,每次查询时段不能超过24小时</param>
  339. /// <param name="msgId">消息id顺序从小到大,从1开始</param>
  340. /// <param name="number">每次获取条数,最多10000条</param>
  341. /// <param name="timeOut"></param>
  342. /// <returns></returns>
  343. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetMsgList", true)]
  344. public static GetMsgListResultJson GetMsgList(string accessTokenOrAppId, DateTime startTime, DateTime endTime, long msgId, int number, int timeOut = Config.TIME_OUT)
  345. {
  346. return ApiHandlerWapper.TryCommonApi(accessToken =>
  347. {
  348. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/msgrecord/getmsglist?access_token={0}", accessToken.AsUrlData());
  349. var data = new
  350. {
  351. starttime = DateTimeHelper.GetUnixDateTime(startTime),
  352. endtime = DateTimeHelper.GetUnixDateTime(endTime),
  353. msgid = msgId,
  354. number = number
  355. };
  356. return CommonJsonSend.Send<GetMsgListResultJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  357. }, accessTokenOrAppId);
  358. }
  359. #endregion
  360. #if !NET35 && !NET40
  361. #region 异步方法
  362. /// <summary>
  363. /// 【异步方法】获取用户聊天记录
  364. /// </summary>
  365. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  366. /// <param name="startTime">查询开始时间,会自动转为UNIX时间戳</param>
  367. /// <param name="endTime">查询结束时间,会自动转为UNIX时间戳,每次查询不能跨日查询</param>
  368. /// <param name="pageSize">每页大小,每页最多拉取1000条</param>
  369. /// <param name="pageIndex">查询第几页,从1开始</param>
  370. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  371. /// <returns></returns>
  372. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetRecordAsync", true)]
  373. public static async Task<GetRecordResult> GetRecordAsync(string accessTokenOrAppId, DateTime startTime, DateTime endTime, int pageSize = 10, int pageIndex = 1, int timeOut = Config.TIME_OUT)
  374. {
  375. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  376. {
  377. var urlFormat = Config.ApiMpHost + "/customservice/msgrecord/getrecord?access_token={0}";
  378. //规范页码
  379. if (pageSize <= 0)
  380. {
  381. pageSize = 1;
  382. }
  383. else if (pageSize > 50)
  384. {
  385. pageSize = 50;
  386. }
  387. //组装发送消息
  388. var data = new
  389. {
  390. starttime = DateTimeHelper.GetUnixDateTime(startTime),
  391. endtime = DateTimeHelper.GetUnixDateTime(endTime),
  392. pagesize = pageSize,
  393. pageindex = pageIndex
  394. };
  395. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetRecordResult>(accessToken, urlFormat, data, timeOut: timeOut);
  396. }, accessTokenOrAppId);
  397. }
  398. /// <summary>
  399. ///【异步方法】 获取客服基本信息
  400. /// </summary>
  401. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  402. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  403. /// <returns></returns>
  404. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetCustomBasicInfoAsync", true)]
  405. public static async Task<CustomInfoJson> GetCustomBasicInfoAsync(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  406. {
  407. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  408. {
  409. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/customservice/getkflist?access_token={0}", accessToken.AsUrlData());
  410. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<CustomInfoJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  411. //return GetCustomInfoResult<CustomInfoJson>(urlFormat);
  412. }, accessTokenOrAppId);
  413. }
  414. /// <summary>
  415. /// 【异步方法】获取在线客服接待信息
  416. /// </summary>
  417. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  418. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  419. /// <returns></returns>
  420. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetCustomOnlineInfoAsync", true)]
  421. public static async Task<CustomOnlineJson> GetCustomOnlineInfoAsync(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  422. {
  423. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  424. {
  425. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/customservice/getonlinekflist?access_token={0}", accessToken.AsUrlData());
  426. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<CustomOnlineJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  427. //return GetCustomInfoResult<CustomOnlineJson>(urlFormat);
  428. }, accessTokenOrAppId);
  429. }
  430. //private static T GetCustomInfoResult<T>(string urlFormat)
  431. //{
  432. // var jsonString = HttpUtility.RequestUtility.HttpGet(urlFormat, Encoding.UTF8);
  433. // JavaScriptSerializer js = new JavaScriptSerializer();
  434. // return js.Deserialize<T>(jsonString);
  435. //}
  436. /// <summary>
  437. /// 【异步方法】添加客服账号
  438. /// </summary>
  439. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  440. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。如果没有公众号微信号,请前往微信公众平台设置。</param>
  441. /// <param name="nickName">客服昵称,最长6个汉字或12个英文字符</param>
  442. /// <param name="passWord">客服账号登录密码,格式为密码明文的32位加密MD5值</param>
  443. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  444. /// <returns></returns>
  445. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.AddCustomAsync", true)]
  446. public static async Task<WxJsonResult> AddCustomAsync(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = Config.TIME_OUT)
  447. {
  448. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  449. {
  450. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/add?access_token={0}", accessToken.AsUrlData());
  451. var data = new
  452. {
  453. kf_account = kfAccount,
  454. nickname = nickName,
  455. password = passWord
  456. };
  457. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  458. }, accessTokenOrAppId);
  459. }
  460. /// <summary>
  461. /// 【异步方法】邀请绑定客服帐号
  462. /// </summary>
  463. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  464. /// <param name="kfAccount">完整客服帐号,格式为:帐号前缀@公众号微信号</param>
  465. /// <param name="inviteWx">接收绑定邀请的客服微信号</param>
  466. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  467. /// <returns></returns>
  468. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.InviteWorkerAsync", true)]
  469. public static async Task<WxJsonResult> InviteWorkerAsync(string accessTokenOrAppId, string kfAccount, string inviteWx, int timeOut = Config.TIME_OUT)
  470. {
  471. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  472. {
  473. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/inviteworker?access_token={0}", accessToken.AsUrlData());
  474. var data = new
  475. {
  476. kf_account = kfAccount,
  477. invite_wx = inviteWx
  478. };
  479. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  480. }, accessTokenOrAppId);
  481. }
  482. /// <summary>
  483. /// 【异步方法】设置客服信息
  484. /// </summary>
  485. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  486. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。如果没有公众号微信号,请前往微信公众平台设置。</param>
  487. /// <param name="nickName">客服昵称,最长6个汉字或12个英文字符</param>
  488. /// <param name="passWord">客服账号登录密码,格式为密码明文的32位加密MD5值</param>
  489. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  490. /// <returns></returns>
  491. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.UpdateCustomAsync", true)]
  492. public static async Task<WxJsonResult> UpdateCustomAsync(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = Config.TIME_OUT)
  493. {
  494. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  495. {
  496. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/update?access_token={0}", accessToken.AsUrlData());
  497. var data = new
  498. {
  499. kf_account = kfAccount,
  500. nickname = nickName,
  501. password = passWord
  502. };
  503. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  504. }, accessTokenOrAppId);
  505. }
  506. /// <summary>
  507. /// 【异步方法】上传客服头像
  508. /// </summary>
  509. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  510. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  511. /// <param name="file">form-data中媒体文件标识,有filename、filelength、content-type等信息</param>
  512. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  513. /// <returns></returns>
  514. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.UploadCustomHeadimgAsync", true)]
  515. public static async Task<WxJsonResult> UploadCustomHeadimgAsync(string accessTokenOrAppId, string kfAccount, string file, int timeOut = Config.TIME_OUT)
  516. {
  517. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  518. {
  519. var url = string.Format(Config.ApiMpHost + "/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  520. var fileDictionary = new Dictionary<string, string>();
  521. fileDictionary["media"] = file;
  522. return await Post.PostFileGetJsonAsync<WxJsonResult>(url, null, fileDictionary, null, timeOut: timeOut);
  523. }, accessTokenOrAppId);
  524. }
  525. /// <summary>
  526. ///【异步方法】 删除客服账号
  527. /// </summary>
  528. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  529. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  530. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  531. /// <returns></returns>
  532. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.DeleteCustomAsync", true)]
  533. public static async Task<WxJsonResult> DeleteCustomAsync(string accessTokenOrAppId, string kfAccount, int timeOut = Config.TIME_OUT)
  534. {
  535. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  536. {
  537. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfaccount/del?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  538. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  539. }, accessTokenOrAppId);
  540. }
  541. /// <summary>
  542. ///【异步方法】创建会话
  543. /// </summary>
  544. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  545. /// <param name="openId">客户openid</param>
  546. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  547. /// <param name="text">附加信息,文本会展示在客服人员的多客服客户端(非必须)</param>
  548. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  549. /// <returns></returns>
  550. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.CreateSessionAsync", true)]
  551. public static async Task<WxJsonResult> CreateSessionAsync(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = Config.TIME_OUT)
  552. {
  553. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  554. {
  555. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/create?access_token={0}", accessToken.AsUrlData());
  556. var data = new
  557. {
  558. openid = openId,
  559. kf_account = kfAccount,
  560. text = text
  561. };
  562. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  563. }, accessTokenOrAppId);
  564. }
  565. /// <summary>
  566. /// 【异步方法】关闭会话
  567. /// </summary>
  568. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  569. /// <param name="openId">客户openid</param>
  570. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号</param>
  571. /// <param name="text">附加信息,文本会展示在客服人员的多客服客户端(非必须)</param>
  572. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  573. /// <returns></returns>
  574. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.CloseSessionAsync", true)]
  575. public static async Task<WxJsonResult> CloseSessionAsync(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = Config.TIME_OUT)
  576. {
  577. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  578. {
  579. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/close?access_token={0}", accessToken.AsUrlData());
  580. var data = new
  581. {
  582. openid = openId,
  583. kf_account = kfAccount,
  584. text = text
  585. };
  586. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  587. }, accessTokenOrAppId);
  588. }
  589. /// <summary>
  590. /// 【异步方法】获取客户的会话状态
  591. /// </summary>
  592. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  593. /// <param name="openId">客户openid</param>
  594. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  595. /// <returns></returns>
  596. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetSessionStateAsync", true)]
  597. public static async Task<GetSessionStateResultJson> GetSessionStateAsync(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
  598. {
  599. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  600. {
  601. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getsession?access_token={0}&openid={1}", accessToken.AsUrlData(), openId.AsUrlData());
  602. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetSessionStateResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  603. }, accessTokenOrAppId);
  604. }
  605. /// <summary>
  606. /// 【异步方法】获取客服的会话列表
  607. /// </summary>
  608. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  609. /// <param name="kfAccount">完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。</param>
  610. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  611. /// <returns></returns>
  612. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetSessionListAsync", true)]
  613. public static async Task<GetSessionListResultJson> GetSessionListAsync(string accessTokenOrAppId, string kfAccount, int timeOut = Config.TIME_OUT)
  614. {
  615. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  616. {
  617. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getsessionlist?access_token={0}&kf_account={1}", accessToken.AsUrlData(), kfAccount.AsUrlData());
  618. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetSessionListResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  619. }, accessTokenOrAppId);
  620. }
  621. /// <summary>
  622. /// 【异步方法】获取未接入会话列表
  623. /// </summary>
  624. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  625. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  626. /// <returns></returns>
  627. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetWaitCaseAsync", true)]
  628. public static async Task<GetWaitCaseResultJson> GetWaitCaseAsync(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  629. {
  630. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  631. {
  632. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/kfsession/getwaitcase?access_token={0}", accessToken.AsUrlData());
  633. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetWaitCaseResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  634. }, accessTokenOrAppId);
  635. }
  636. /// <summary>
  637. /// 【异步方法】获取聊天记录
  638. /// </summary>
  639. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  640. /// <param name="startTime">起始时间,unix时间戳</param>
  641. /// <param name="endTime">结束时间,unix时间戳,每次查询时段不能超过24小时</param>
  642. /// <param name="msgId">消息id顺序从小到大,从1开始</param>
  643. /// <param name="number">每次获取条数,最多10000条</param>
  644. /// <param name="timeOut"></param>
  645. /// <returns></returns>
  646. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CustomServiceApi.GetMsgListAsync", true)]
  647. public static async Task<GetMsgListResultJson> GetMsgListAsync(string accessTokenOrAppId, DateTime startTime, DateTime endTime, long msgId, int number, int timeOut = Config.TIME_OUT)
  648. {
  649. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  650. {
  651. var urlFormat = string.Format(Config.ApiMpHost + "/customservice/msgrecord/getmsglist?access_token={0}", accessToken.AsUrlData());
  652. var data = new
  653. {
  654. starttime = DateTimeHelper.GetUnixDateTime(startTime),
  655. endtime = DateTimeHelper.GetUnixDateTime(endTime),
  656. msgid = msgId,
  657. number = number
  658. };
  659. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetMsgListResultJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
  660. }, accessTokenOrAppId);
  661. }
  662. #endregion
  663. #endif
  664. }
  665. }