知识库模块
'.$keyword.$uniacid;
$json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
$keys = json_decode($json,true);
$json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
$numkeys = json_decode($json,true);
if(is_numeric($keyword)){
$send=$keys[$numkeys[$keyword]];
}else {
$send=$keys[$keyword]; //$keys['关于我们'];
}
if ( $send<>'' ) { //判断是否是知识库设置字段
if ($send['link']['title']!=''){
$send['link']['title']=str_replace('id',$userinfo['id'],$send['link']['title']);
$send['link']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['link']['title']);
}
if ($send['miniprogrampage']['title']!=''){
$send['miniprogrampage']['title']=str_replace('id',$userinfo['id'],$send['miniprogrampage']['title']);
$send['miniprogrampage']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['miniprogrampage']['title']);
}
if ($send['text']['content']!=''){
$send['text']['content']=str_replace('[id]',$userinfo['id'],$send['text']['content']);
$send['text']['content']=str_replace('[name]',trim($userinfo['nickname']),$send['text']['content']);
$send['text']['content']=changeurl($send['text']['content']);
$send['text']['content'].=$addstr;
}
$send['touser']=$fromuserid;
$data=json_encode($send,JSON_UNESCAPED_UNICODE);
$aaa = http_post_data($url, $data);
$zt=json_decode($aaa, true)['errcode'];
if ($zt!=0){//下发未成功
$data='{ "touser":"'.$fromuserid.'", "msgtype":"text",
"text": { "content":" 系统忙 错误代码: ' . $zt . ' " } }';
$aaa = http_post_data($url, $data);
}
return true;
}else{
return false;//不是知识.
}
}
function sendmsg($openid,$msgtype,$keyword,$postObj,$url){
//消息处理模块.
switch ($msgtype)
{
case 'text':
$data1='{ "touser":"'.$openid.'", "msgtype":"text",
"text": { "content":" '.$keyword.' " } }';
break;
case 'image':
$MediaId = $postObj->MediaId;
$data1=' { "touser":"'.$openid.'", "msgtype":"image",
"image": { "media_id":"'.$MediaId.'" }}';
break;
case 'voice':
$MediaId = $postObj->MediaId;
$Recognition = $postObj->Recognition;
$data1=' { "touser":"'.$openid.'", "msgtype":"voice",
"voice": { "media_id":"'.$MediaId.'" }}';
break;
case 'miniprogrampage':
$data1='';
break;
default:
$data1='';
}
$aaa = http_post_data($url, $data1);
//echo '发送消息:'.$data1.$aaa;
// logger('@@@@@@@@'.$data1.$aaa);
return $aaa;
}
function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
//echo $return_content."
";
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// return array($return_code, $return_content);
return $return_content;
}
function zhaokefumingling($fromuserid,$uniacid,$keyword,$url,$kefuarr,$website){
$kefulist = json_decode(file_get_contents("json/".$uniacid."_kefulist.json"),true);
//$kefuname=substr($keyword,7);
$Kefuopenid=array_search($keyword,$kefulist,true);//找到客服id
if (substr($kefuarr[$Kefuopenid],0,1)=='o')//看客服状态 客服状态over
{
sendmsg($fromuserid,'text','客服忙,请稍后.','',$url);//下发排队情况给用户.
return false;
}else{//客服接入//
if($Kefuopenid==''){
sendmsg($fromuserid,'text','客服休息了.请稍后再试.','',$url);//下发排队情况给用户.
return false;
}else{//客服在,对接开始. 如果是排队发的 则删除排队.如果是没排队发的.则不排队.
$kefuarr[$Kefuopenid]=$fromuserid;
//建立对接 同时通知双方. 发两次消息.
exec("rm -f ".'json/'.$uniacid.'_kefu.json');
$res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
//增加获取用户信息功能. 没排队 直接进入 需要查客户信息.
$json = file_get_contents("json/".$uniacid."_paidui.json");
$paiduiarr = json_decode($json,true);
if(array_key_exists($fromuserid,$paiduiarr)){//如果在排队则..
$uid=$paiduiarr[$fromuserid]['id'];
$nickname=$paiduiarr[$fromuserid]['nickname'];
unset($paiduiarr[$fromuserid]);//删除它
$res=file_put_contents("json/".$uniacid."_paidui.json", json_encode($paiduiarr));
}else{//没排队 .就是上来就发的. 新用户就注册 老用户就获取.
$users=getuser($uniacid,$fromuserid,$nickname,$website);
$uid=$users['id'];
$nickname=$users['nickname'];
}
sendmsg($Kefuopenid,'text','['.$uid.']'.$nickname.':'.$keyword.' 你好','',$url);//通知客服
// $kefuname = $kefulist[$Kefuopenid];
sendmsg($fromuserid,'text',$keyword.':你好,有什么可以帮到你的?','',$url);//通知客服
return true;
}
}
}
function atsuoyouren($uniacid,$website,$keyword,$myopenid,$url,$toUsername){
//@所有人
$huncun= json_decode(alluser($uniacid,$website),true);
$gjz=substr($keyword,10,strlen(trim($keyword))-10);
if (substr ($keyword, -1)=='#'){
$gjz=str_replace('#','|',$gjz);
}
$baogao=' {
"title": "代发列表已经准备好!点此群发",
"description": "本次准备发送: '.$huncun['count'].' 请1分钟内点击,过期失效.",
"url": "'.$website.'addons/qidong_app/qunfa.php?uniacid='.$uniacid.'&keyword='.$gjz.'&tousername='.$toUsername.'&t='.time().'",
"thumb_url": "'.$website.'/addons/qidong_app/icon.jpg"
}';
$data1='{ "touser":"'.$myopenid.'", "msgtype":"link", "link": '.$baogao.' }';
$aaa = http_post_data($url, $data1); //
}
function zxkefu($uniacid,$kefuarr,$sqstr1){//读取在线客服列表//下次升级点击接通??
$json = file_get_contents("json/".$uniacid."_kefulist.json");//客服详情页
$kefulist = json_decode($json,true);
$kefuopenid= array_keys($kefulist);//kfuopenid
$kefuall='';
$i=0;
foreach ($kefuopenid as $kefuopenid){ //群发--
//$kfopenid=$arr;
if(substr($kefuarr[$kefuopenid],0,1)=="o"){
$zt='忙碌';
}else{
$zt=$kefuarr[$kefuopenid];
}
if ($zt<>'隐身'){
$i++;
//$sqstr=$website."addons/qidong_app/intenet.php?zfopenid=".$fromUsername."&toUsername=".$toUsername."&appid=".$arr['Gappid']."&kfname=".$kfname;
//$sqstr="\n点此快速接通客服";
//$sqstr1=$website."addons/qidong_app/intenet.php?zfopenid=".$fromUsername."&toUsername=".$toUsername."&appid=".$arr['Gappid']."&kfname=".$kfname;
if(($sqstr1=='') or ($zt=='忙碌')){
$kefuall=$kefuall." ".$kefulist[$kefuopenid]."[".$zt."]";
}else{
$sqstr="\n".$kefulist[$kefuopenid]."";
$kefuall=$kefuall." ".$sqstr."[".$zt."]";
}
if(($i % 2)==0){ $kefuall.="\n";}
// $kefuall=$kefuall.' '.$kefulist[$kefuopenid].'['.$zt.']';
}
}
//logger($kefuall);
return $kefuall;
}
function atmouren($keyword,$uniacid,$website,$fromuserid,$url,$kefuname){
$stri=0;
$stri= stripos($keyword,':');
if ($stri==0){$stri= stripos($keyword,' ') ;}
if ($stri==0){$stri= stripos($keyword,':') ;}
$nid=substr($keyword,1,$stri-1); //@12:=>12
$say=trim(substr($keyword,$stri+1));//发送的内容:
if($say==''){$say=$kefuname.'@你 ,客服上线了,邀请你进入会话.';}
//判断say最后字符是# 则是关键词消息 991#
$liuuser= hiuser($uniacid,$nid,$website);
$result = substr ($say, -1); //当是# 关键字图文时 .
if ($result=='#'){
$json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
$keys = json_decode($json,true);
$json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
$numkeys = json_decode($json,true);
$gjz=substr($say,0,strlen($say)-1);
if(is_numeric($gjz)){
$send=$keys[$numkeys[$gjz]];
} else {
$send=$keys[$gjz]; //$keys['关于我们'];
}
$send['touser']=$liuuser['openid'];
$nickname=$liuuser['nickname'];
if($nickname==''){$nickname='会员'.$liuuser['id'];}
$send['link']['title']=str_replace('[id]',$liuuser['id'],$send['link']['title']);
$send['link']['title']=str_replace('[name]', trim($nickname),$send['link']['title']);
$data=json_encode($send,JSON_UNESCAPED_UNICODE);
$bbb = http_post_data($url, $data);
logger('单发:'.$gjz.'-'.$data);
}else{
sendmsg($liuuser['openid'],'text','['.$kefuname.'@你]:'.$say,'',$url);
}
if (json_decode($bbb, true)['errcode']==0)
{ $zt='送达';
//对方正在输入...哈哈
$data8='{ "touser":"'.trim($liuuser['openid']).'", "command":"Typing"}';
$url8='https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token='.$Atoken;
$bbb = http_post_data($url8, $data8);
//logger($bbb);
//结束输入判断.
}else{
$zt='失败,可能已经离线'.json_decode($bbb, true)['errcode']; };
sendmsg($fromuserid,'text','[@'.$nid.'报告]:'.$zt,'',$url);
//发送消息
}
function glymingling($uniacid,$openid,$kehuopenid,$keyword,$jsy,$kefuarr,$url,$toUsername,$website){
//处理管理员命令 @某人命令?
//echo '检查命令!';$
if (strpos(',清零,下一个,隐身,看留言,上班,排队,@所有人,',$keyword)>0){
//此处增加管理员命令列表处理.
if($keyword=='看留言'){
$json = file_get_contents('json/'.$toUsername.'_lishi.json');
sendmsg($openid,'text','本次获取的内容,已清空留言板:\n'.$json,'',$url);
exec("rm -f ".'json/'.$toUsername.'_lishi.json');
break;
}
if($keyword=='排队'){
$json = file_get_contents("json/".$uniacid."_paidui.json");
$karr = json_decode($json,true);
$pdrs=count($karr);
sendmsg($openid,'text','排队人数:'.$pdrs,'',$url);//发排队人数.后面再做详情.??
}
if($keyword=='隐身'){
$kefuarr[$openid]='隐身';
$res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
sendmsg($openid,'text','设置隐身成功. 可被点名进入聊天.不能自动进入聊天.','',$url);
}
if($keyword=='清零'){
exec("rm -f ".'json/'.$toUsername.'_paidui.json');
exec("rm -f ".'json/'.$toUsername.'_token.json');
exec("rm -f ".'log.xml');
if (!file_exists('json/'.$toUsername.'_token.json')){
$t="清零成功!";
}else{
$t="清零失败,请检查目录权限是否设置为777!";
}
sendmsg($openid,'text',$t,'',$url);
}
if($keyword=='上班'){
$kefuarr[$openid]='空闲';
$res=file_put_contents("json/".$uniacid."_kefu.json", json_encode($kefuarr));
sendmsg($openid,'text','设置上班成功. 可自动接入来访客户.','',$url);
}
if($keyword=='下一个'){
//跟现在的说再见 ?再见语?
$json = file_get_contents("json/".$uniacid."_kefulist.json");//客服详情页
$kefulist = json_decode($json,true);
$kefuname = $kefulist[$openid];
if (substr($kehuopenid,0,1)=='o'){
//回复图文消息.
jieshuyu($jsy,$uniacid,$kehuopenid,$website,$url);
// sendmsg($kehuopenid,'text',$jsy,'',$url);//说再见.
// $a=edituser($uniacid,$delid['openid'],'',$website);
//$lastliao='';//最后聊天客服.
edituser($uniacid,$kehuopenid,'',$website,$avartar,$zhuangtai,$kefuname);
}
//找到下一个客户?
$json = file_get_contents("json/".$uniacid."_paidui.json");
$paiduiarr = json_decode($json,true);
$nextkh=array_shift($paiduiarr);//取出并从排队中删除第一个.
if (substr($nextkh['openid'],0,1)=='o'){
//print_r($nextkh);
//echo '