自动回复这块是比较麻烦的
一、后端回复管理
后端没什么说的,根据微信公众平台的自动回复建表,添加数据即可
需要强调的是 规则、关键词、回复内容需要建三个表
规则表中关键词和回复内容拼接id成字符串 存入表中
二、前端回复逻辑
$this->app->server->push(function($message){
switch ($message['MsgType']) {
case 'event':
//若通过扫描二维码关注
if (isset($message['EventKey'])){
$class = "\\app\\common\\library\\wechat\\message\\" . ucfirst($message['MsgType']);
class_exists($class) && new $class($message);
}
//关注事件回复
$content = db('wx_reply_receive')->where('reply_type=1')->find();
switch ($content['content_type']){
case 1:
$contentStr = $content['content'];
$contentStr = $this->replace_str($contentStr);
$resultStr = new Text($contentStr);
return $resultStr;
break;
case 2:
$contentStr = $content['content_id'];
$resultStr = new Image($contentStr);
return $resultStr;
break;
case 3:
$contentStr = $content['content_id'];
$resultStr = new Voice($contentStr);
return $resultStr;
break;
case 4:
$contentStr = $content['content_id'];
$resultStr = new Video($contentStr);
return $resultStr;
break;
default:
break;
}
break;
//关键词回复
case 'text':
$keyword = $message['Content'];
if(!empty( $keyword )){
$where['keyword'] = $keyword;
$where['status'] = 1;
$content = db('wx_reply')
->where($where)
->whereBetweenTimeField('start_time','end_time')
->select();
if (empty($content)){
$len = mb_strlen($keyword);
if ($len<=15){
$words = $this->split_str($keyword);
$map[] = array('status','=',1);
$map[] = array('keyword','in',$words);
$bcont = db('wx_reply')
->where($map)
->whereBetweenTimeField('start_time','end_time')
->select();
if (!empty($bcont)){
foreach ($bcont as $value){
if ($value['matching_mode']==0){
if ($value['reply_mode']==0){ //回复方式 全部回复
switch ($value['reply_type']){
case 1:
$contentStr = $value['content'];
$contentStr = $this->replace_str($contentStr);
$resultStr = new Text($contentStr);
return $resultStr;
break;
case 2:
$contentStr = $value['media_id'];
$resultStr = new Image($contentStr);
return $resultStr;
break;
case 3:
$contentStr = $value['media_id'];
$resultStr = new Voice($contentStr);
return $resultStr;
break;
case 4:
$contentStr = $value['media_id'];
$resultStr = new Video($contentStr);
return $resultStr;
break;
default:
break;
}
}else{
switch ($value['reply_type']){
case 1:
$contentStr = $value['content'];
$contentStr = $this->replace_str($contentStr);
$resultStr = new Text($contentStr);
return $resultStr;
break;
case 2:
$contentStr = $value['media_id'];
$resultStr = new Image($contentStr);
return $resultStr;
break;
case 3:
$contentStr = $value['media_id'];
$resultStr = new Voice($contentStr);
return $resultStr;
break;
case 4:
$contentStr = $value['media_id'];
$resultStr = new Video($contentStr);
return $resultStr;
break;
default:
break;
}
break;
}
}
}
}else{
$receive = db('wx_reply_receive')->where('reply_type=0')->find();
switch ($receive['content_type']){
case 1:
$contentStr = $receive['content'];
$contentStr = $this->replace_str($contentStr);
$resultStr = new Text($contentStr);
return $resultStr;
break;
case 2:
$contentStr = $receive['content_id'];
$resultStr = new Image($contentStr);
return $resultStr;
break;
case 3:
$contentStr = $receive['content_id'];
$resultStr = new Voice($contentStr);
return $resultStr;
break;
case 4:
$contentStr = $receive['content_id'];
$resultStr = new Video($contentStr);
return $resultStr;
break;
default:
break;
}
}
}
}
}
break;
default:
return '收到其它消息';
break;
}
});
$this->app->server->serve()->send();
关键词分割
//分解关键词
public function split_str($str){
$len = strlen($str);
// $data = str_split($str);
for($i = 1; $i<=$len;$i++){
for($j = 0; $j < $len; $j++) {
$data[] = mb_substr($str,$j,$i);
}
}
$data = array_unique($data);
$data = array_filter($data);
return $data;
}
处理html标签,包括
、//微信端显示处理
public function replace_str($str){
$str = str_replace('','',$str);
$str = str_replace('
',"\n",$str);
$str = str_replace(' '," ",$str);
$str = str_replace('
',"\n",$str);
$str = str_replace('&','&',$str);
return $str;
}
大体步骤就是这样,业务逻辑根据需求更改即可