思路
1.点击应用的菜单-在线客服-触发事件
$result1 = $this->receiveEvent($postObj);
2.分配客服 - 使用缓存memcache进行存储分配客服的信息(项目部署在sae,所以在sae上面开启memcache即可)
$mmc = memcache_init(); //初始化缓存
memcache_set($mmc, "service", '{"from":"'.$from.'","to":"'.$to.'"}'); //分配的信息
3.发送信息接入客服和用户
$this->send_custom_msg($from, "text", "正在为您接入客服,请稍候..");
4.断开连接,结束在线客服模式
memcache_delete($mmc, "service");
单客服
该多客服功能参考修改了单客服功能,全面代码请参考
微信企业号客服功能开发PHP.
下文中的代码则是更改了的部分代码
多客服
客服用户对话连接
private function receiveEvent($object)
{
$content = "";
switch($object->Event){
case "click":
switch($object->EventKey)
{
case "qt_kf":
$mmc = memcache_init();
//客服单击,结束对话
if( in_array($object->FromUserName, Constant::$Staff4)){ //只有客服点击,才可结束会话
$staff = Constant::$Staff4; //客服名单
$countt = sizeof($staff); //客服总数量
$i = 1;
$ii = 0;
while($countt >= $i){
$service = memcache_get($mmc,"service".$i);
$relation = json_decode($service, true);
$ii = $i;
if($service && in_array(strval($object->FromUserName), array( $relation['to']))){
$from = $relation['to'];
$to = $relation['from'];
$this->send_custom_msg($from, "text", "你已结束本次对话!",1,"service".$ii);
$this->send_custom_msg($to, "text", "感谢您的咨询。期待下次再会!",1,"service".$ii);
memcache_delete($mmc, "service".$ii); //删除memcache
$i = $countt;
}
$i = $i + 1;
}
}
else{ //用户单击,开始对话
$service = memcache_get($mmc,"service");
$relation = json_decode($service, true);
if($relation['from'] == null)
{
$from =strval($object->FromUserName);
$to = Constant::$Staff1;
memcache_set($mmc, "service", '{"from":"'.$from.'","to":"'.$to.'"}');
$this->send_custom_msg($from, "text", "正在为您接入客服,请稍候..");
$this->send_custom_msg($to, "text", "有用户请求接入,请响应!");
}else { //分配客服
$staff = Constant::$Staff4; //客服名单
$countt = sizeof($staff); //客服总数量
$flag = 1; // 0为有空闲客服,1为无空闲客服
$i = 1; //循环判断
while($i <= $countt){
$ii = $i - 1;
$service = memcache_get($mmc,"service".$i);
$relation = json_decode($service, true);
if(!$service){
$from =strval($object->FromUserName);
$to = $staff[$ii];
memcache_set($mmc, "service".$i, '{"from":"'.$from.'","to":"'.$to.'"}',0,300);
$this->send_custom_msg($from, "text", "正在为您接入客服,请稍候..",1,"service".$i);
$this->send_custom_msg($to, "text", "有用户请求接入,请响应!",1,"service".$i);
$i = $countt;
$flag = 0;
}else{
if(in_array(strval($object->FromUserName), array( $relation['from'])))
{
$i = $countt;
}
}
$i = $i + 1;
}
if($flag == 1){
$from =strval($object->FromUserName);
$this->send_custom_msg($from, "text", "在线客服占线,请稍后再试。",1,"all");
}
}
}
return;
default:
$content = "菜单默认回复";
break;
}
break;
}
if(is_array($content)){
$result = $this->transmitNews($object,$content);
}else{
$result = $this->transmitText($object,$content);
}
return $result;
}
接收文本信息
/**
用户和客服之间的文本交互,若是在线客服功能已连接,则通过此方法,进行信息传递
*/
private function receiveText($object) //文本消息
{
$keyword = trim($object->Content);
$mmc = memcache_init(); //初始化memcache
$staff = Constant::$Staff4;
$countt = sizeof($staff);
$i = 1;
$ii = 0;
while($countt >= $i){
$service = memcache_get($mmc, "service".$i);
$relation = json_decode($service, true);
$ii = $i;
if($service && in_array(strval($object->FromUserName), array($relation['to'], $relation['from']))){
$i = $countt;
}
$i = $i + 1;
}
if($service && in_array(strval($object->FromUserName), array($relation['to'], $relation['from']))){ //当前有人工客服对话
$to = ($relation['from'] == strval($object->FromUserName))?$relation['to']:$relation['from'];
$this->send_custom_msg($to, "text", $keyword,0,"service".$ii);
return;
}
return $result;
}
发送文本信息
public function send_custom_msg($from, $type, $content,$stype,$servicetype)
{
$agentid = Constant::$Agentid;
$access_token = $this->getAccessToken(); //获取access_token
//发送信息的url
$sendmsg_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token";
//信息格式
$data ='{ "touser": "'.$from.'",
"toparty": "",
"totag": "",
"msgtype": "text",
"agentid": $agentid,
"text": { "content": "'.$content.'" },
"safe": "'.$stype.'" }';
if($servicetype != "all"){
$mmc = memcache_init();
$service = memcache_get($mmc,$servicetype);
$relation = json_decode($service, true);
$from = $relation['from'];//strval($object->FromUserName);
$to = $relation['to'];
memcache_set($mmc, $servicetype, '{"from":"'.$from.'","to":"'.$to.'"}',0,300);
}
return $this->mn_post($sendmsg_url,$data);
}