⼩程序客服智能回复⽰例代码(PHP)业务逻辑⽂件编写
use think\Action; //⾃⼰封装的curl⽅法,详情看附录
define("TOKEN", "你设置的token");
class Customer extends Controller
{
//校验服务器地址URL
public function checkServer(){
if (isset($_GET['echostr'])) {
$this->valid();
}else{
$this->responseMsg();
}
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}else{
echo $echoStr.'+++'.TOKEN;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
//此处推荐使⽤file_get_contents('php://input')获取后台post过来的数据
$postStr = file_get_contents('php://input');
if (!empty($postStr) && is_string($postStr)){
$postArr = json_decode($postStr,true);
if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'text'){
//⽤户给客服发送⽂本消息
if($postArr['Content'] == 7){
//接收到指定的⽂本消息,触发事件
$fromUsername = $postArr['FromUserName']; //发送者openid
$media_id = '上传到服务器的图⽚id,看第三部分'; //输⼊想要回复的图⽚消息的media_id
$this->requestIMAGE($fromUsername,$media_id);
}
}
else if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){
//⽤户给客服发送图⽚消息,按需求设置
}
else if($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){
//⽤户进⼊客服事件
$fromUsername = $postArr['FromUserName']; //发送者openid
$content = '你好,欢迎来到***客服,有什么能帮助您的么';
$this->requestTXT($fromUsername,$content);
}
else{
exit('error');
}
}else{
echo "empty";
exit;
}
}
//⽂本回复
public function requestTXT($fromUsername,$content){
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"text",
"text"=>array("content"=>$content)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE);
$this->requestAPI($json);
}
//图⽚回复
public function requestIMAGE($fromUsername,$media_id){
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"image",
"image"=>array("media_id"=>$media_id)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE);
$this->requestAPI($json);
}
public function requestAPI($json){
$access_token = $this->get_accessToken();
$action = new Action(); //⾃⼰封装的curl⽅法,详情看附录
$url = "api.weixin.qq/cgi-bin/message/custom/send?access_token=".$access_token;
$output = $action->curl_post($url,$json);
if($output == 0){
echo 'success';
exit;
}
}
//调⽤api,获取access_token,有效期7200s
public function get_accessToken(){
$url = 'api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的秘钥'; //替换成⾃⼰的⼩程序id和secret $res = file_get_contents($url);
$data = json_decode($res,true);
$token = $data['access_token'];
return $token;
}
}
上传回复图⽚到服务器
这个是临时的素材接⼝,只能存在3天,⽬前⼩程序不⽀持永久素材上传,只有⽀持
public function uploadWxMedia(){
$token = $this->get_accessToken();
$type = "image";
$filepath = Env::get('root_path').'public\\assets\\imageName.png'; //⽂件在服务器的绝对路径,按⾃⼰存放位置修改
$data = array("media"=>new \CURLFile($filepath)); //php5.6以上必须⽤这种⽅法上传⽂件
$url = "api.weixin.qq/cgi-bin/media/upload?access_token=".$token."&type=".$type;
$action = new Action(); //封装的curl⽅法,看附录
$result = $action->curl_post($url,$data);
print_r($result);
}
//调⽤api,获取access_token,有效期7200s
public function get_accessToken(){
$url = 'api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的秘钥'; //替换成⾃⼰的⼩程序id和secret $res = file_get_contents($url);
$data = json_decode($res,true);
$token = $data['access_token'];
return $token;
}
访问 uploadWxMedia() ⽅法就会把设置好的图⽚上传,会返回⼀个json数据:
{"type":"image","media_id":"LTbNsi***************JqG","created_at":1558062553}
代码转换其中的 media_id 就是⽤来填写进第⼆步的回复图⽚中的值
附录
封装的curl⽅法
namespace think;
class Action
{
//get⽅式请求接⼝
public function get_json($url)
{
$data = file_get_contents($url);
//转换成数组
$data = json_decode($data,true);
//输出
return $data;
}
//post⽅式请求接⼝
public function curl_post($url,$data,$headers = null)
{
//$data 是⼀个 array() 数组;未编码
$curl = curl_init(); // 启动⼀个CURL会话
if(substr($url,0,5)=='https'){
// 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//只有在CURL低于7.28.1时CURLOPT_SSL_VERIFYHOST才⽀持使⽤1表⽰true,⾼于这个版本就需要使⽤2表⽰了(true也不⾏)。curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
if($headers != null){
//post请求中携带header参数
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
//返回api的json对象
$response = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//返回json对象
return $response;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。