kefu_c_bak.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. <?php
  2. //echo checkSignature;
  3. /* 本代码已经申请著作权保护
  4. 盗版必究
  5. 法律顾问:北京东环律所事务所
  6. ?用插件的方式./...
  7. o 客服直接输入关键字 //直接生成客服关键字列表. ok 判断关键字进入. ok
  8. o 列出在线客服 回复 或者 //下一版 --点击 可接通.??
  9. o 增加获取用户信息功能 //授权 //下一班
  10. o 常用命令功能:清零 排队 上班 下标
  11. 再见打招呼 回复1 修改下.
  12. o 继续上次的对话. ok
  13. 感谢您的来访,请您对本次服务进行评价:
  14. 非常满意 满意 一般 不满意 非常不满意
  15. 给再见加上一个 链接功能 或者 按钮 ...
  16. ok 群发列表问题 修正 是缺少参数
  17. ok 下一个 更新时间.
  18. 转接客服
  19. 客服按钮
  20. ///////////
  21. 管理员可见--????
  22. 第一步 创建表 管理员可增加
  23. 第二步 用户端可申请 提交 .显示自己的客服席位数量 .
  24. 第三步 管理员负责修改席位 我负责开通权限
  25. 可增加 权限开通 密钥 .50个密钥 输入1个 .用一个.
  26. CopyRight 2017 www.17cheng.cn All Rights Reserved
  27. */
  28. header('Content-type:text/json');
  29. $stime=microtime(true);
  30. ini_set("display_errors", "off");
  31. define("TOKEN", "xcxkefu");
  32. $wechatObj = new wechatCallbackapiTest();
  33. if (isset($_GET['echostr'])) { //判断是不是首次验证
  34. $wechatObj->valid();
  35. }else{
  36. $wechatObj->responseMsg();
  37. }
  38. class wechatCallbackapiTest
  39. {
  40. public function valid()
  41. {
  42. $echoStr = $_GET["echostr"];
  43. if($this->checkSignature()){
  44. echo $echoStr;
  45. exit;
  46. }
  47. }
  48. private function checkSignature()
  49. {
  50. $signature = $_GET["signature"];
  51. $timestamp = $_GET["timestamp"];
  52. $nonce = $_GET["nonce"];
  53. $token = TOKEN;
  54. $tmpArr = array($token, $timestamp, $nonce);
  55. sort($tmpArr);
  56. $tmpStr = implode( $tmpArr );
  57. $tmpStr = sha1( $tmpStr );
  58. if( $tmpStr == $signature ){
  59. return true;
  60. }else{
  61. return false;
  62. }
  63. }
  64. public function responseMsg()
  65. {
  66. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取数据
  67. // logger(time().$postStr);
  68. $xcx=false;//true 是小程序
  69. if (!empty($postStr)){
  70. //$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  71. if($xcx){
  72. $postObj =json_decode($postStr);}
  73. else{
  74. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  75. }
  76. $fromUsername = trim($postObj->FromUserName); //openid
  77. $toUsername = trim($postObj->ToUserName);
  78. $tousernamebak = $toUsername;
  79. $keyword = trim($postObj->Content);
  80. $sessionFrom = trim($postObj->SessionFrom);
  81. $MsgType = $postObj->MsgType;//消息类型 text event
  82. $Event = $postObj->Event;
  83. if ($Event == "CLICK") {
  84. $EventKey = $postObj->EventKey;
  85. $MsgType='text';
  86. $keyword=trim($EventKey);//修复公众号问题.
  87. logger('event:'.$Event.$MsgType.'key:'.$keyword);
  88. }
  89. $json = file_get_contents('json/'.$toUsername.'.json');
  90. $arr = json_decode($json,true);
  91. $uniacid = $arr['uniacid'];
  92. //echo $uniacid;
  93. $website = $arr['website'];
  94. define("APPID", $arr['APPID']);
  95. define("APPSECRET", $arr['APPSECRET']);
  96. $fromuserid = $fromUsername;//
  97. $myopenid = trim($arr['openids']);//管理员id
  98. $Atoken = getToken(APPID,APPSECRET,$toUsername);
  99. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Atoken;
  100. if ($keyword=='openid'){
  101. sendmsg($fromuserid,$MsgType,$fromuserid,'',$url);//给用户发消息.
  102. exit;
  103. }
  104. //echo $Atoken ;
  105. $json = file_get_contents("json/".$uniacid."_kefu.json");
  106. $kefuarr = json_decode($json,true); //客服列表
  107. ////////////////////////////////////////////////////////动作提示//////////////////////////////
  108. if ($MsgType == "event") {
  109. $event = $postObj->Event;
  110. if(array_key_exists($fromuserid,$kefuarr)){ //判断是不是客服.
  111. logger($MsgType.'是客服.');
  112. $paiduinumber=count(file('json/'.$uniacid.'_paidui.json'));
  113. $liuyannumber=count(file('json/'.$toUsername.'_lishi.json'));
  114. $kehuopenid= $kefuarr[$fromuserid];
  115. //当前聊天的是?
  116. if (substr($kehuopenid,0,1)=='o'){
  117. $users=getuser($uniacid,$kehuopenid,$nickname,$website);
  118. $zhuangtai='当前聊天:['.$users['id'].']'.$users['nickname'];
  119. }else{
  120. $zhuangtai='休息中';
  121. if ($kehuopenid=='空闲') { $zhuangtai='当前空闲,有客户会立即进入聊天.';}
  122. if ($kehuopenid=='隐身') { $zhuangtai='当前隐身,客户可主动指定聊天.';}
  123. }
  124. $tongzhi='排队人数:'. $paiduinumber.'\n'.'留言条数:\n'. $liuyannumber.' \n'.$zhuangtai;
  125. $data1='{ "touser":"'.$fromuserid.'", "msgtype":"text",
  126. "text": { "content":"[客服上线工作提醒]\n '.$tongzhi.' " } }';
  127. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  128. exit;
  129. }
  130. //在线客服代码
  131. //继续上一次的对话
  132. $Kefuopenid=array_search($fromuserid,$kefuarr,true);
  133. if($Kefuopenid<>''){
  134. sendmsg($fromuserid,'text','客服还在,直接回复继续上次的对话,或者回复再见 结束对话','',$url);//给用户发消息.
  135. exit;
  136. }
  137. if($xcx){
  138. $userinfo = getuser($uniacid,$fromUsername,$nickname,$website);
  139. } else{
  140. $user = getwxinfo($Atoken,$fromUsername);//微信公众号代码
  141. $userinfo = getuser($uniacid,$fromUsername,$user['nickname'],$website);//微信公众号代码
  142. }
  143. //
  144. // 下一次增加在线客服提示 这里不提示先 .等下一版本.
  145. if ($arr['Gappid']<>''){ //网页授权
  146. // if((substr($userinfo['nickname'],0,6)=='会员')){
  147. // $sqstr1=$website."addons/qidong_app/intenet.php?zfopenid=".$fromUsername."&toUsername=".$toUsername."&appid=".$arr['Gappid'];
  148. // $sqstr="\n<a href='".$sqstr1."'>点链接快速接通客服</a>";
  149. // }
  150. }
  151. //logger('是客户:.'.$fromUsername);
  152. // if($event=='unsubscribe'){ $eve='取消关注'; exit; };//公众号专用
  153. //logger($fromuserid.'-'.$arr['默认'].$uniacid.$url);
  154. $zxkefu="\n在线客服:回复名字接通\n".zxkefu($uniacid,$kefuarr,$sqstr1);
  155. if($userinfo['lastliao']<>null){$zxkefu.="\n上次接待客服是:".$userinfo['lastliao'];}
  156. $zxkefu.=$sqstr;//[屏蔽一下 下一版发布.]
  157. zhishiku($fromuserid,trim($arr['默认']),$uniacid,$url,$zxkefu,$userinfo) ;
  158. $caidan=trim($arr['Gjsonid']);//进入通知:
  159. if ($caidan<>''){
  160. $Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
  161. // logger("通知下发:".$caidan_arr['APPID'].'-状态:'.$caidan_arr['openids']);
  162. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
  163. $data1='{ "touser":"'.$arr['Gopenids'].'", "msgtype":"text",
  164. "text": { "content":"[小程序'.$uniacid.'进入提醒]\n '.$userinfo['nickname'].' " } }';
  165. $aaa = http_post_data($url1, $data1); // //通知到公共号
  166. // logger($data1.$aaa);
  167. }
  168. exit;
  169. }
  170. ////////////////////////////////////////////////结束动作///////////////////////////////
  171. //print_r($kefuarr);
  172. //$arr= array_keys($arr1);//openid
  173. //处理客服消息模块开始
  174. if(array_key_exists($fromuserid,$kefuarr)){//客服消息处理
  175. if (substr($keyword,0,10)=='@所有人'){//@所有人
  176. atsuoyouren($uniacid,$website,$keyword,$fromuserid,$url,$toUsername);
  177. exit;
  178. }
  179. //处理@----某人命令
  180. if(substr($keyword,0,1)=='@'){//@某人
  181. $kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
  182. $kefuname = $kefulist[$fromuserid];
  183. atmouren($keyword,$uniacid,$website,$fromuserid,$url,$kefuname);
  184. exit;
  185. }
  186. if($keyword=='p'){$keyword='下一个'; }
  187. if($keyword=='m'){$keyword='排队'; }
  188. if($keyword=='0'){$keyword='清零'; }
  189. //客服来的消息处理
  190. $kehuopenid= $kefuarr[$fromuserid];//
  191. // echo '找到客户:'.$kehuopenid;
  192. //判断是不是有客户
  193. if (substr($kehuopenid,0,1)=='o'){
  194. //有客户 ,直接发消息过去: 判断消息是不是@
  195. if (glymingling($uniacid,$fromuserid,$kehuopenid,$keyword,$arr['结束语'],$kefuarr,$url,$toUsername)){//是管理员命令
  196. // echo '命令!';
  197. }else{
  198. //查找客服信息.
  199. $kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
  200. $kefuname = $kefulist[$fromuserid];
  201. // echo '给:'.$kefuarr[$fromuserid].'发内容'.$keyword;
  202. $bbb=sendmsg($kehuopenid,$MsgType,$kefuname.':'.$keyword,$postObj,$url);//给用户发消息.
  203. //增加判断.消息失败提示.
  204. if (json_decode($bbb, true)['errcode']<>0){
  205. $data2='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  206. "text": { "content":" 客服离开... 消息已存入信箱.'.json_decode($bbb, true)['errcode'].'" } }'; //当没有约定好的回复时,这么回复。
  207. sendmsg($fromUsername,'text','客户可能已经离开了.','',$url);//给用户发消息.
  208. // savejson('json/'.$toUsername.'_lishi.json', '@'.$paidui['id'].'_'.$content.''.$keyword );
  209. }
  210. //sendmsg($openid,$msgtype,$keyword,$postObj,$url)
  211. }
  212. }else{
  213. //没客户接待时,可能发的命令:下一个.
  214. if (glymingling($uniacid,$fromuserid,$kehuopenid,$keyword,$arr['结束语'],$kefuarr,$url,$toUsername,$website)){
  215. //echo '是命令!';zaij
  216. }else{
  217. // echo '给:'.$kefuarr[$fromuserid].'发内容'.$keyword;
  218. // 空闲时 可以玩玩素材库 .
  219. if (!zhishiku($fromuserid,$keyword,$uniacid,$url,'',$userinfo)){
  220. sendmsg($fromuserid,'text','暂无接待客户,可发送命令下一个接通.','',$url,$toUsername,$website);//给用户发消息.
  221. }
  222. }
  223. //客服状态. kong mang 判断是不是要上班?或者其他命令 比如 下一个 等.
  224. }
  225. }else{
  226. // echo "not found kefu--";//不是客服 则看谁空闲? 规则 先找老客服 .
  227. //反查是否在队列
  228. //先看客户是不是...选了指定的客服. 客服小李 正在咨询的时候 是不能调出的.除非.
  229. $Kefuopenid=array_search($fromuserid,$kefuarr,true);
  230. if($Kefuopenid<>'')
  231. {
  232. // echo "这是用户,正在咨询的客服是:";
  233. // 用户发 再见 主动离线 . 更新客服列表 . 告诉客服 告诉客户
  234. if (substr($keyword,0,6)=='再见'){
  235. // sendmsg($fromuserid,'text',$arr['结束语'],'',$url);//说再见.
  236. jieshuyu($arr['结束语'],$uniacid,$fromuserid,$website,$url);
  237. $json = file_get_contents("json/".$uniacid."_kefulist.json");//客服详情页
  238. $kefulist = json_decode($json,true);
  239. $kefuname = $kefulist[$Kefuopenid];
  240. edituser($uniacid,$fromuserid,'',$website,$avartar,$zhuangtai,$kefuname);
  241. $json = file_get_contents("json/".$uniacid."_paidui.json");
  242. $paiduinum = count(json_decode($json,true));
  243. sendmsg($Kefuopenid,'text','当前客户已经离开,发下一个接通.当前有'.$paiduinum.'人排队.','',$url);//说再见.
  244. $kefuarr[$Kefuopenid]='空闲';
  245. //建立对接 同时通知双方. 发两次消息.
  246. exec("rm -f ".'json/'.$uniacid.'_kefu.json');
  247. $res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
  248. }else{
  249. $users=getuser($uniacid,$fromuserid,$nickname,$website);
  250. $bbb=sendmsg($Kefuopenid,$MsgType,'['.$users['id'].']'.$users['nickname'].':'.$keyword,$postObj,$url);
  251. if (json_decode($bbb, true)['errcode']<>0){
  252. sendmsg($fromUsername,'text','客服忙..已经存入留言板...\n 回复 再见 结束会话.','',$url);//给用户发消息.
  253. // savejson('json/'.$toUsername.'_lishi.json', '@'.$users['id'].'_'.$users['nickname'].''.$keyword );
  254. file_put_contents('json/'.$toUsername.'_lishi.json', '@'.$users['id'].'_'.$users['nickname'].''.$keyword .PHP_EOL, FILE_APPEND);
  255. }
  256. }
  257. }else{//没在咨询,检查是否有空闲客服
  258. if ( strpos($arr['kefustr'],$keyword)>0){//客户发张三
  259. $kongkefu='';
  260. $zhiding=1;
  261. }else{
  262. $kongkefu=array_search('空闲',$kefuarr,true);
  263. }
  264. if($kongkefu<>''){
  265. //echo '当前有空的客服:';
  266. //echo $kongkefu;
  267. $kefuarr[$kongkefu]=$fromuserid;
  268. //建立对接 同时通知双方. 发两次消息.
  269. exec("rm -f ".'json/'.$uniacid.'_kefu.json');
  270. $res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
  271. //增加获取用户信息功能. 没排队 直接进入 需要查客户信息.
  272. $users=getuser($uniacid,$fromuserid,$nickname,$website);
  273. sendmsg($kongkefu,'text',$users['id'].$users['nickname'].':'.$keyword,'',$url);//通知客服
  274. $kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
  275. $kefuname = $kefulist[$kongkefu];
  276. sendmsg($fromuserid,'text',$kefuname.':你好,有什么可以帮到你的?','',$url);//通知客服
  277. $tongzhiadmin=$kefuname.' 接入了新客户.['.$users['id'].']'.$users['nickname'];
  278. }else
  279. { //echo '当前客服都忙:/或者客户指定了客服.';
  280. //检查关键词回复 如果有则回复关键词
  281. if($MsgType=='text'){
  282. if (!zhishiku($fromuserid,$keyword,$uniacid,$url,'',$userinfo)){
  283. //当客服发送 指定客服的指令时 .如发 客服1
  284. if ($zhiding==1){//客户主动选择客服.
  285. $zd=zhaokefumingling($fromuserid,$uniacid,$keyword,$url,$kefuarr,$website);
  286. if($zd){ $tongzhiadmin=$keyword.' 被客户点名. ';}
  287. }else{
  288. $paidui=paidui($uniacid,$fromuserid,$website);//排队语自己组织.
  289. sendmsg($fromuserid,'text',$paidui,'',$url);//下发排队情况给用户.
  290. if($paidui<>'排队中,请耐心等待.'){
  291. $tongzhiadmin='有用户来:'.$keyword.'排队:'.$paidui;
  292. }
  293. }
  294. }
  295. }else{//用户发送其他信息怎么办? 通知他排队.
  296. $paidui=paidui($uniacid,$fromuserid,$website);//排队语自己组织.
  297. sendmsg($fromuserid,'text',$paidui,'',$url);//下发排队情况给用户.
  298. sendmsg($myopenid,$MsgType,'其他类型文件',$postObj,$url);//其他文件 直接通知管理员..
  299. }
  300. // 是否在排队? 用数组键值查询, 没有.插入排队表 .有 返回排队顺序?
  301. }
  302. } //下发给总管理员通知;
  303. if ($tongzhiadmin<>''){
  304. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  305. "text": { "content":"[管理员提醒:]\n'.$tongzhiadmin.'" } }';
  306. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  307. }
  308. }
  309. //处理客服消息模块结束.
  310. }else{///////////////////////////////////////////////////////////////
  311. echo "";
  312. exit;
  313. }
  314. }
  315. }
  316. function zhishiku($fromuserid,$keyword,$uniacid,$url,$addstr,$userinfo){
  317. //知识库处理模块
  318. //echo '<br>知识库模块<br>'.$keyword.$uniacid;
  319. $json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
  320. $keys = json_decode($json,true);
  321. $json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
  322. $numkeys = json_decode($json,true);
  323. if(is_numeric($keyword)){
  324. $send=$keys[$numkeys[$keyword]];
  325. }else {
  326. $send=$keys[$keyword]; //$keys['关于我们'];
  327. }
  328. if ( $send<>'' ) { //判断是否是知识库设置字段
  329. if ($send['link']['title']!=''){
  330. $send['link']['title']=str_replace('id',$userinfo['id'],$send['link']['title']);
  331. $send['link']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['link']['title']);
  332. }
  333. if ($send['miniprogrampage']['title']!=''){
  334. $send['miniprogrampage']['title']=str_replace('id',$userinfo['id'],$send['miniprogrampage']['title']);
  335. $send['miniprogrampage']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['miniprogrampage']['title']);
  336. }
  337. if ($send['text']['content']!=''){
  338. $send['text']['content']=str_replace('[id]',$userinfo['id'],$send['text']['content']);
  339. $send['text']['content']=str_replace('[name]',trim($userinfo['nickname']),$send['text']['content']);
  340. $send['text']['content']=changeurl($send['text']['content']);
  341. $send['text']['content'].=$addstr;
  342. }
  343. $send['touser']=$fromuserid;
  344. $data=json_encode($send,JSON_UNESCAPED_UNICODE);
  345. $aaa = http_post_data($url, $data);
  346. $zt=json_decode($aaa, true)['errcode'];
  347. if ($zt!=0){//下发未成功
  348. $data='{ "touser":"'.$fromuserid.'", "msgtype":"text",
  349. "text": { "content":" 系统忙 错误代码: ' . $zt . ' " } }';
  350. $aaa = http_post_data($url, $data);
  351. }
  352. return true;
  353. }else{
  354. return false;//不是知识.
  355. }
  356. }
  357. function sendmsg($openid,$msgtype,$keyword,$postObj,$url){
  358. //消息处理模块.
  359. switch ($msgtype)
  360. {
  361. case 'text':
  362. $data1='{ "touser":"'.$openid.'", "msgtype":"text",
  363. "text": { "content":" '.$keyword.' " } }';
  364. break;
  365. case 'image':
  366. $MediaId = $postObj->MediaId;
  367. $data1=' { "touser":"'.$openid.'", "msgtype":"image",
  368. "image": { "media_id":"'.$MediaId.'" }}';
  369. break;
  370. case 'voice':
  371. $MediaId = $postObj->MediaId;
  372. $Recognition = $postObj->Recognition;
  373. $data1=' { "touser":"'.$openid.'", "msgtype":"voice",
  374. "voice": { "media_id":"'.$MediaId.'" }}';
  375. break;
  376. case 'miniprogrampage':
  377. $data1='';
  378. break;
  379. default:
  380. $data1='';
  381. }
  382. $aaa = http_post_data($url, $data1);
  383. //echo '发送消息:'.$data1.$aaa;
  384. // logger('@@@@@@@@'.$data1.$aaa);
  385. return $aaa;
  386. }
  387. function http_post_data($url, $data_string) {
  388. $ch = curl_init();
  389. curl_setopt($ch, CURLOPT_POST, 1);
  390. curl_setopt($ch, CURLOPT_URL, $url);
  391. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  392. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  393. 'Content-Type: application/json; charset=utf-8',
  394. 'Content-Length: ' . strlen($data_string))
  395. );
  396. ob_start();
  397. curl_exec($ch);
  398. $return_content = ob_get_contents();
  399. //echo $return_content."<br>";
  400. ob_end_clean();
  401. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  402. // return array($return_code, $return_content);
  403. return $return_content;
  404. }
  405. function zhaokefumingling($fromuserid,$uniacid,$keyword,$url,$kefuarr,$website){
  406. $kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
  407. //$kefuname=substr($keyword,7);
  408. $Kefuopenid=array_search($keyword,$kefulist,true);//找到客服id
  409. if (substr($kefuarr[$Kefuopenid],0,1)=='o')//看客服状态 客服状态over
  410. {
  411. sendmsg($fromuserid,'text','客服忙,请稍后.','',$url);//下发排队情况给用户.
  412. return false;
  413. }else{//客服接入//
  414. if($Kefuopenid==''){
  415. sendmsg($fromuserid,'text','客服休息了.请稍后再试.','',$url);//下发排队情况给用户.
  416. return false;
  417. }else{//客服在,对接开始. 如果是排队发的 则删除排队.如果是没排队发的.则不排队.
  418. $kefuarr[$Kefuopenid]=$fromuserid;
  419. //建立对接 同时通知双方. 发两次消息.
  420. exec("rm -f ".'json/'.$uniacid.'_kefu.json');
  421. $res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
  422. //增加获取用户信息功能. 没排队 直接进入 需要查客户信息.
  423. $json = file_get_contents("json/".$uniacid."_paidui.json");
  424. $paiduiarr = json_decode($json,true);
  425. if(array_key_exists($fromuserid,$paiduiarr)){//如果在排队则..
  426. $uid=$paiduiarr[$fromuserid]['id'];
  427. $nickname=$paiduiarr[$fromuserid]['nickname'];
  428. unset($paiduiarr[$fromuserid]);//删除它
  429. $res=file_put_contents("json/".$uniacid."_paidui.json", json_encode($paiduiarr));
  430. }else{//没排队 .就是上来就发的. 新用户就注册 老用户就获取.
  431. $users=getuser($uniacid,$fromuserid,$nickname,$website);
  432. $uid=$users['id'];
  433. $nickname=$users['nickname'];
  434. }
  435. sendmsg($Kefuopenid,'text','['.$uid.']'.$nickname.':'.$keyword.' 你好','',$url);//通知客服
  436. // $kefuname = $kefulist[$Kefuopenid];
  437. sendmsg($fromuserid,'text',$keyword.':你好,有什么可以帮到你的?','',$url);//通知客服
  438. return true;
  439. }
  440. }
  441. }
  442. function atsuoyouren($uniacid,$website,$keyword,$myopenid,$url,$toUsername){
  443. //@所有人
  444. $huncun= json_decode(alluser($uniacid,$website),true);
  445. $gjz=substr($keyword,10,strlen(trim($keyword))-10);
  446. if (substr ($keyword, -1)=='#'){
  447. $gjz=str_replace('#','|',$gjz);
  448. }
  449. $baogao=' {
  450. "title": "代发列表已经准备好!点此群发",
  451. "description": "本次准备发送: '.$huncun['count'].' 请1分钟内点击,过期失效.",
  452. "url": "'.$website.'addons/qidong_app/qunfa.php?uniacid='.$uniacid.'&keyword='.$gjz.'&tousername='.$toUsername.'&t='.time().'",
  453. "thumb_url": "'.$website.'/addons/qidong_app/icon.jpg"
  454. }';
  455. $data1='{ "touser":"'.$myopenid.'", "msgtype":"link", "link": '.$baogao.' }';
  456. $aaa = http_post_data($url, $data1); //
  457. }
  458. function zxkefu($uniacid,$kefuarr,$sqstr1){//读取在线客服列表//下次升级点击接通??
  459. $json = file_get_contents("json/".$uniacid."_kefulist.json");//客服详情页
  460. $kefulist = json_decode($json,true);
  461. $kefuopenid= array_keys($kefulist);//kfuopenid
  462. $kefuall='';
  463. $i=0;
  464. foreach ($kefuopenid as $kefuopenid){ //群发--
  465. //$kfopenid=$arr;
  466. if(substr($kefuarr[$kefuopenid],0,1)=="o"){
  467. $zt='忙碌';
  468. }else{
  469. $zt=$kefuarr[$kefuopenid];
  470. }
  471. if ($zt<>'隐身'){
  472. $i++;
  473. //$sqstr=$website."addons/qidong_app/intenet.php?zfopenid=".$fromUsername."&toUsername=".$toUsername."&appid=".$arr['Gappid']."&kfname=".$kfname;
  474. //$sqstr="\n<a href='".$sqstr."'>点此快速接通客服</a>";
  475. //$sqstr1=$website."addons/qidong_app/intenet.php?zfopenid=".$fromUsername."&toUsername=".$toUsername."&appid=".$arr['Gappid']."&kfname=".$kfname;
  476. if(($sqstr1=='') or ($zt=='忙碌')){
  477. $kefuall=$kefuall." ".$kefulist[$kefuopenid]."[".$zt."]";
  478. }else{
  479. $sqstr="\n<a href='".$sqstr1."&kfname=".$kefulist[$kefuopenid]."&kfopenid=".$kefuopenid."'>".$kefulist[$kefuopenid]."</a>";
  480. $kefuall=$kefuall." ".$sqstr."[".$zt."]";
  481. }
  482. if(($i % 2)==0){ $kefuall.="\n";}
  483. // $kefuall=$kefuall.' <a href="#">'.$kefulist[$kefuopenid].'</a>['.$zt.']';
  484. }
  485. }
  486. //logger($kefuall);
  487. return $kefuall;
  488. }
  489. function atmouren($keyword,$uniacid,$website,$fromuserid,$url,$kefuname){
  490. $stri=0;
  491. $stri= stripos($keyword,':');
  492. if ($stri==0){$stri= stripos($keyword,' ') ;}
  493. if ($stri==0){$stri= stripos($keyword,':') ;}
  494. $nid=substr($keyword,1,$stri-1); //@12:=>12
  495. $say=trim(substr($keyword,$stri+1));//发送的内容:
  496. if($say==''){$say=$kefuname.'@你 ,客服上线了,邀请你进入会话.';}
  497. //判断say最后字符是# 则是关键词消息 991#
  498. $liuuser= hiuser($uniacid,$nid,$website);
  499. $result = substr ($say, -1); //当是# 关键字图文时 .
  500. if ($result=='#'){
  501. $json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
  502. $keys = json_decode($json,true);
  503. $json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
  504. $numkeys = json_decode($json,true);
  505. $gjz=substr($say,0,strlen($say)-1);
  506. if(is_numeric($gjz)){
  507. $send=$keys[$numkeys[$gjz]];
  508. } else {
  509. $send=$keys[$gjz]; //$keys['关于我们'];
  510. }
  511. $send['touser']=$liuuser['openid'];
  512. $nickname=$liuuser['nickname'];
  513. if($nickname==''){$nickname='会员'.$liuuser['id'];}
  514. $send['link']['title']=str_replace('[id]',$liuuser['id'],$send['link']['title']);
  515. $send['link']['title']=str_replace('[name]', trim($nickname),$send['link']['title']);
  516. $data=json_encode($send,JSON_UNESCAPED_UNICODE);
  517. $bbb = http_post_data($url, $data);
  518. logger('单发:'.$gjz.'-'.$data);
  519. }else{
  520. sendmsg($liuuser['openid'],'text','['.$kefuname.'@你]:'.$say,'',$url);
  521. }
  522. if (json_decode($bbb, true)['errcode']==0)
  523. { $zt='送达';
  524. //对方正在输入...哈哈
  525. $data8='{ "touser":"'.trim($liuuser['openid']).'", "command":"Typing"}';
  526. $url8='https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token='.$Atoken;
  527. $bbb = http_post_data($url8, $data8);
  528. //logger($bbb);
  529. //结束输入判断.
  530. }else{
  531. $zt='失败,可能已经离线'.json_decode($bbb, true)['errcode']; };
  532. sendmsg($fromuserid,'text','[@'.$nid.'报告]:'.$zt,'',$url);
  533. //发送消息
  534. }
  535. function glymingling($uniacid,$openid,$kehuopenid,$keyword,$jsy,$kefuarr,$url,$toUsername,$website){
  536. //处理管理员命令 @某人命令?
  537. //echo '检查命令!';$
  538. if (strpos(',清零,下一个,隐身,看留言,上班,排队,@所有人,',$keyword)>0){
  539. //此处增加管理员命令列表处理.
  540. if($keyword=='看留言'){
  541. $json = file_get_contents('json/'.$toUsername.'_lishi.json');
  542. sendmsg($openid,'text','本次获取的内容,已清空留言板:\n'.$json,'',$url);
  543. exec("rm -f ".'json/'.$toUsername.'_lishi.json');
  544. break;
  545. }
  546. if($keyword=='排队'){
  547. $json = file_get_contents("json/".$uniacid."_paidui.json");
  548. $karr = json_decode($json,true);
  549. $pdrs=count($karr);
  550. sendmsg($openid,'text','排队人数:'.$pdrs,'',$url);//发排队人数.后面再做详情.??
  551. }
  552. if($keyword=='隐身'){
  553. $kefuarr[$openid]='隐身';
  554. $res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
  555. sendmsg($openid,'text','设置隐身成功. 可被点名进入聊天.不能自动进入聊天.','',$url);
  556. }
  557. if($keyword=='清零'){
  558. exec("rm -f ".'json/'.$toUsername.'_paidui.json');
  559. exec("rm -f ".'json/'.$toUsername.'_token.json');
  560. exec("rm -f ".'log.xml');
  561. if (!file_exists('json/'.$toUsername.'_paidui.json')){
  562. $t="清零成功!";
  563. }else{
  564. $t="清零失败,请检查目录权限是否设置为777!";
  565. }
  566. sendmsg($openid,'text',$t,'',$url);
  567. }
  568. if($keyword=='上班'){
  569. $kefuarr[$openid]='空闲';
  570. $res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
  571. sendmsg($openid,'text','设置上班成功. 可自动接入来访客户.','',$url);
  572. }
  573. if($keyword=='下一个'){
  574. //跟现在的说再见 ?再见语?
  575. $json = file_get_contents("json/".$uniacid."_kefulist.json");//客服详情页
  576. $kefulist = json_decode($json,true);
  577. $kefuname = $kefulist[$openid];
  578. if (substr($kehuopenid,0,1)=='o'){
  579. //回复图文消息.
  580. jieshuyu($jsy,$uniacid,$kehuopenid,$website,$url);
  581. // sendmsg($kehuopenid,'text',$jsy,'',$url);//说再见.
  582. // $a=edituser($uniacid,$delid['openid'],'',$website);
  583. //$lastliao='';//最后聊天客服.
  584. edituser($uniacid,$kehuopenid,'',$website,$avartar,$zhuangtai,$kefuname);
  585. }
  586. //找到下一个客户?
  587. $json = file_get_contents("json/".$uniacid."_paidui.json");
  588. $paiduiarr = json_decode($json,true);
  589. $nextkh=array_shift($paiduiarr);//取出并从排队中删除第一个.
  590. if (substr($nextkh['openid'],0,1)=='o'){
  591. //print_r($nextkh);
  592. //echo '<hr>';
  593. //print_r('nextkhoid:'.$nextkh['openid']);
  594. sendmsg($nextkh['openid'],'text',$kefuname.':你好,久等了.','',$url);//新用户你好.
  595. $userjson=json_encode($paiduiarr);
  596. $res=file_put_contents("json/".$uniacid."_paidui.json",$userjson);//更新排队数据.
  597. $kefuarr[$openid]=$nextkh['openid'];//保存新的用户进来.;
  598. sendmsg($openid,'text','['.$nextkh['id'].']'.$nextkh['nickname'].'已经进入聊天.','',$url);//
  599. $res=file_put_contents("json/".$uniacid."_kefu.json",json_encode($kefuarr));//更新客服数据.
  600. $kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
  601. $kefuname = $kefulist[$fromuserid];
  602. //sendmsg($nextkh['openid'],'text',$kefuname.':你好久等了.','',$url);//
  603. }else{//已无排队用户.修正客服表 保存.告诉客服.
  604. $kefuarr[$openid]='空闲';
  605. $res=file_put_contents("json/".$uniacid."_kefu.json",json_encode($kefuarr));//更新客服数据.
  606. sendmsg($openid,'text','已无排队用户.你的状态目前是空闲.\n常用命令: 上班 隐身 排队.','',$url);//新用户你好.
  607. }
  608. }
  609. return true;
  610. }else
  611. {
  612. return false;
  613. }
  614. }
  615. function paidui($uniacid,$openid,$website){
  616. $json = file_get_contents("json/".$uniacid."_paidui.json");
  617. $kefuarr = json_decode($json,true);
  618. if(array_key_exists($openid,$kefuarr)){
  619. return '排队中,请耐心等待.';//排队中...
  620. }else{
  621. $user=getuser($uniacid,$openid,$nickname,$website);//获取用户信息
  622. $userinfo=array('id'=>$user['id'],
  623. 'nickname'=>$user['nickname'],
  624. 'beizhu'=>$user['beizhu'],
  625. 'openid'=>$openid);
  626. $kefuarr[$openid]=$userinfo;
  627. $userjson=json_encode($kefuarr);
  628. $res=file_put_contents("json/".$uniacid."_paidui.json",$userjson);
  629. $qianmian=count($kefuarr)-1;
  630. return '已接入客服中心,前面有'.$qianmian.'人';//排队数量
  631. }
  632. }
  633. function getuser($ucid,$openid,$nickname,$website)
  634. {
  635. ///先判断缓存文件是否存在
  636. $json = file_get_contents("json/".$ucid."_user.json");
  637. $userinfos = json_decode($json,true);
  638. // $userid=$userinfos[$openid]['id'];
  639. // echo '找到缓存表:'.$userid;
  640. if (!array_key_exists($openid,$userinfos)){
  641. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=getuser&m=qidong_app&openid=".$openid."&nickname=".$nickname;
  642. // echo $token_access_url;
  643. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  644. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  645. $nickname= $result['data']['wxapp'][0]['nickname'];
  646. $beizhu= $result['data']['wxapp'][0]['zhuangtai'];
  647. $lastliao= $result['data']['wxapp'][0]['lastliao'];
  648. if (trim($nickname)==null) {
  649. $nickname='会员'.$result['data']['wxapp'][0]['id'];
  650. $isid=1;
  651. }
  652. $userid=$result['data']['wxapp'][0]['id'];
  653. $data = array(
  654. 'nickname' => $nickname,
  655. 'id' => $userid,
  656. 'beizhu' => $beizhu,
  657. 'lastliao'=> $lastliao
  658. );
  659. return $data ;
  660. }else
  661. {
  662. // echo '找到缓存 直接返回;';
  663. return $userinfos[$openid];
  664. }
  665. //缓存 先去判断文件是否存在 .
  666. }
  667. //去查询 用户表 或者 客服表 找到详细信息.
  668. // 输出 : 身份[客服 或 用户 ] 状态 [ 咨询中 | 排队中 ]
  669. //规则 聊天中 不能使用1 2 3 不去查询 . 都发给管理员 .
  670. function changeurl($a){
  671. $bi= strpos($a,'[[');
  672. if ($bi==0){
  673. return $a;
  674. }else{
  675. $ei= strpos($a,']]');
  676. $qb=substr($a,0,$bi);
  677. $eb=substr($a,$ei+2);
  678. $b=substr($a,$bi+2,$ei-$bi-2);
  679. $ci=strpos($b,'|');
  680. $add=substr($b,0,$ci);
  681. $herf=substr($b,$ci+1,strlen($b)-$ci);
  682. }
  683. return $qb."<a href='".$herf."'>".$add."</a>".$eb;
  684. }
  685. function getToken($appid,$appsecret,$jsonid){
  686. $file = file_get_contents("json/{$jsonid}_token.json",true);
  687. $result = json_decode($file,true);
  688. if (time() > $result['expires']){
  689. $data = array();
  690. $data['access_token'] = getNewToken($appid,$appsecret);
  691. $data['expires']=time()+7200;
  692. $jsonStr = json_encode($data);
  693. $fp = fopen("json/{$jsonid}_token.json", "w");
  694. fwrite($fp, $jsonStr);
  695. fclose($fp);
  696. return $data['access_token'];
  697. }else{
  698. return $result['access_token'];
  699. }
  700. }
  701. function getNewToken($appid,$appsecret){
  702. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  703. $access_token_Arr = https_request($url);
  704. return $access_token_Arr['access_token'];
  705. }
  706. function https_request ($url){
  707. $ch = curl_init();
  708. curl_setopt($ch, CURLOPT_URL, $url);
  709. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  710. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  711. $out = curl_exec($ch);
  712. curl_close($ch);
  713. return json_decode($out,true);
  714. }
  715. function logger($log_content)
  716. {
  717. if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
  718. sae_set_display_errors(false);
  719. sae_debug($log_content);
  720. sae_set_display_errors(true);
  721. }else{ //LOCAL
  722. $max_size = 500000;
  723. $log_filename = "log1.xml";
  724. if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
  725. file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);
  726. }
  727. }
  728. function getwxinfo($token,$openid){
  729. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
  730. // logger($url);
  731. $access_token_Arr = https_request($url);
  732. return $access_token_Arr;
  733. }
  734. function hiuser($ucid,$id,$website){
  735. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=getopenid&m=qidong_app&id=".$id;
  736. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  737. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  738. $data=$result['data'][0] ;
  739. return $data ;
  740. }
  741. function alluser($ucid,$website){
  742. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=Getallopenid&m=qidong_app";
  743. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  744. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  745. $data=$result['data'] ;
  746. return json_encode($data) ;
  747. }
  748. function edituser($ucid,$openid,$nickname,$website,$avartar,$zhuangtai,$lastliao) //改名取号
  749. {
  750. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=edituser&m=qidong_app&openid=".$openid."&nickname=".$nickname."&avartar=".$avartar."&lastliao=".$lastliao; //zhuangtai
  751. // echo $token_access_url;
  752. // exit;
  753. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  754. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  755. $data=$result['data'];
  756. if ($data==1){$data='改名成功!';}
  757. else{$data='改名失败!';}
  758. return $data ;
  759. }
  760. function jieshuyu($bye,$uniacid,$openid,$website,$url){
  761. if(is_numeric($bye)){
  762. $json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
  763. $keys = json_decode($json,true);
  764. $json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
  765. $numkeys = json_decode($json,true);
  766. $delid=getuser($uniacid,$openid,'',$website);
  767. $send=$keys[$numkeys[$bye]];
  768. if ($send['link']['title']!=''){
  769. $send['link']['title']=str_replace('id',$delid['id'],$send['link']['title']);
  770. $send['link']['title']=str_replace('[name]', trim($delid['nickname']),$send['link']['title']);
  771. }
  772. if ($send['miniprogrampage']['title']!=''){
  773. $send['miniprogrampage']['title']=str_replace('id',$delid['id'],$send['miniprogrampage']['title']);
  774. $send['miniprogrampage']['title']=str_replace('[name]', trim($delid['nickname']),$send['miniprogrampage']['title']);
  775. }
  776. if ($send['text']['content']!=''){
  777. $send['text']['content']=str_replace('[id]',$delid['id'],$send['text']['content']);
  778. $send['text']['content']=str_replace('[name]',trim($delid['nickname']),$send['text']['content']);
  779. // logger('要替换的文本:'.$send['text']['content']);
  780. $send['text']['content']=changeurl($send['text']['content']);
  781. }
  782. $send['touser']=trim($openid);
  783. $data0=json_encode($send,JSON_UNESCAPED_UNICODE);
  784. }
  785. else {
  786. $sends=$bye; //$keys['关于我们'];
  787. $jieshuyu= str_replace('[name]',$delid['nickname'],$sends);
  788. $jieshuyu=str_replace("{br}","\n",$jieshuyu);
  789. $jieshuyu=changeurl($jieshuyu);
  790. $data0='{ "touser":"'.trim($openid).'", "msgtype":"text",
  791. "text": { "content":" '.$jieshuyu.' " } }';
  792. }
  793. $aaa = http_post_data($url, $data0);
  794. logger($data0);
  795. }
  796. ?>