微信小程序python人工智能回复_微信小程序客服智能回复示例代码(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 = "https://api.weixin.qq.com/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 = 'https://api.weixin.qq.com/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 = "https://api.weixin.qq.com/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 = 'https://api.weixin.qq.com/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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在这个互联网时代,客服可以说必不可少,每个电商网站都应该有一个强大的智能客服对话系统,以满足用户沟通的需求。智能客服对话系统,不仅需要人工的沟通,同时结合人工智能实现智能对话,减少人工客服的成本,势在必行。基于SpringBoot+Python的多语言前后端智能多人聊天系统课程,将以基础知识为根基,带大家完成一个强大的智能客服系统,该系统将包含以下功能:智能对话机器人、单聊、群聊、消息撤回、上线、下线通知、用户动态信息实时提示等。即时通讯和人工智能,在未来的发展趋势,必然需要大批人才,掌握这两个技术势在必行。项目是一个真实可用的项目,商业价值不言而喻。也可以基于课程的基础上进一步完善和优化,所以价值是很高的。本课程包含的技术: 开发工具为:IDEA、WebStorm、PyCharmTensorflowRNNLSTMAnacondaSpringBoot SpringCloudWebsocketSTOMPDjangoVue+Nodejs+jQuery等 课程亮点: 1.与企业接轨、真实工业界产品2.从基础到案例,逐层深入,学完即用3.市场主流的前后端分离架构和人工智能应用结合开发4.多语言结合开发,满足多元化的需求5.涵盖TensorFlow1.x+TensorFlow2.x版本6.智能机器人实战7.即时通讯实战8.多Python环境切换9.微服务SpringBoot10.集成SpringCloud实现统一整合方案 11.全程代码实操,提供全部代码和资料 12.提供答疑和提供企业技术方案咨询 课程目录:第一章、Anaconda以及TensorFlow环境和使用0、智能多人聊天系统课程说明1、智能多人聊天系统之Anaconda讲解2、智能多人聊天系统之Anaconda安装和使用3、智能多人聊天系统之Anaconda之conda命令使用4、智能多人聊天系统之TensorFlow讲解5、智能多人聊天系统之TensorFlow安装和使用6、TensorFlow常量、变量和占位符实战讲解17、TensorFlow常量、变量和占位符实战讲解28、TensorFlow原理补充讲解9、TensorFlow四则运算实战讲10、TensorFlow矩阵操作以及运算实战讲解111、TensorFlow矩阵操作以及运算实战讲解212、TensorFlow均匀分布和正态分布数据实战讲解13、智能多人聊天系统之Numpy实战讲解14、智能多人聊天系统之matplotlib实战讲解15、TensorFlow深度学习DNN讲解16、TensorFlow常用Python扩展包讲解17、TensorFlow常用回归算法以及正则化讲解18、TensorFlow损失函数定义和使用实战讲解19、TensorFlow优化器讲解以及综合案例实战讲解20、智能多人聊天系统之RNN讲解21、智能多人聊天系统之RNN种类讲解22、智能多人聊天系统之RNN代码实战23、智能多人聊天系统之LSTM讲解24、智能多人聊天系统之attention机制讲解25、智能多人聊天系统之Django环境构建及初体验26、智能多人聊天系统之Django开发27、Python章节环境侯建和项目搭建28、Python TensorFlow读取训练数据代码编写29、Python TensorFlow形成语料编码30、Python TensorFlow保存字典文件31、Python TensorFlow构建词向量32、Python TensorFlow构建lstm模型以及attention wrapper33、Python TensorFlow训练代码编写34、Python整体代码讲解35、Python运用模型代码讲解36、SpringBoot讲解以及构建web应用37、Spring Cloud注册中心构建38、智能多人聊天系统之前端Vue项目构建39、SpringBoot+Websocket群聊40、SpringBoot+Websocket昵称群聊41、SpringBoot+Websocket群聊+单聊实战42、SpringBoot+Stomp单聊143、SpringBoot+Stomp单聊244、SpringBoot+Stomp单聊+群聊45、Django Web整合TF代码讲解及Postman调试46、智能客服系统单聊群聊等项目功能代码讲解147、智能客服系统单聊群聊等项目功能代码讲解248、智能客服系统集成机器人对话代码开发讲解49、智能机器人TensorFlow2版本升级实战之训练模型代码讲解50、智能机器人TensorFlow2版本升级实战之预测代码讲解 51、智能机器人TensorFlow2版本升级实战补充讲解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值