摘要:小程序接入客服消息消息推送配置填写等。特别要注意的是数据格式的选择为了后续其他信息能够转发回微信自有网页版客服消息平台,建议选择格式。
小程序接入客服消息 1、消息推送配置
填写URL、Token等。
</>复制代码
特别要注意的是:数据格式的选择;
为了后续其他信息能够转发回微信自有网页版客服消息平台,建议选择xml格式。
json格式现在还有bug(2018.3.5),
bug主要原因返回的头部不符合http标准。
返回json格式header头应该是
</>复制代码
Content-Type:application/json,
而微信的选json还是
</>复制代码
Content-Type:text/xml,
2、代码如下,直接改改就能用(这里用的php)
</>复制代码
checkSignature()) {
echo $echoStr;
exit;
}
}
public 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 send($data){
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->getAccessToken();
$data = urldecode(json_encode($data));
$this->curl_post($url,$data);
}
//xml数据转数组
public function xml2Array($contents = NULL, $encoding = "UTF-8", $get_attributes = 1, $priority = "tag"){
if (!$contents)
{
return array();
}
if (!function_exists("xml_parser_create"))
{
return array ();
}
$parser = xml_parser_create("");
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $encoding);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
return array();
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == "tag")
$result = trim($value);
else
$result["value"] = trim($value);
}
if (isset ($attributes) && $get_attributes) {
foreach ($attributes as $attr => $val)
{
if ($priority == "tag")
$attributes_data[$attr] = $val;
else
$result["attr"][$attr] = $val; //Set all the attributes in a array called "attr"
}
}
if ($type == "open")
{
$parent[$level -1] = & $current;
if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . "_attr"] = $attributes_data;
$repeated_tag_index[$tag . "_" . $level] = 1;
if (isset($tag) && $tag && isset($current[$tag])) {
$current = & $current[$tag];
}
}
else
{
if (isset ($current[$tag][0]))
{
$current[$tag][$repeated_tag_index[$tag . "_" . $level]] = $result;
$repeated_tag_index[$tag . "_" . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . "_" . $level] = 2;
if (isset ($current[$tag . "_attr"]))
{
$current[$tag]["0_attr"] = $current[$tag . "_attr"];
unset ($current[$tag . "_attr"]);
}
}
$last_item_index = $repeated_tag_index[$tag . "_" . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
$current[$tag] = $result;
$repeated_tag_index[$tag . "_" . $level] = 1;
if ($priority == "tag" && $attributes_data) {
$current[$tag . "_attr"] = $attributes_data;
}
}
else
{
if (isset ($current[$tag][0]) && is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag . "_" . $level]] = $result;
if ($priority == "tag" && $get_attributes && $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . "_" . $level] . "_attr"] = $attributes_data;
}
$repeated_tag_index[$tag . "_" . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . "_" . $level] = 1;
if ($priority == "tag" && $get_attributes) {
if (isset ($current[$tag . "_attr"]) && is_array($current[$tag]))
{
$current[$tag]["0_attr"] = $current[$tag . "_attr"];
unset ($current[$tag . "_attr"]);
}
if ($attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . "_" . $level] . "_attr"] = $attributes_data;
}
}
$repeated_tag_index[$tag . "_" . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == "close")
{
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
//获取accesstoken
public function getAccessToken() {
$tokenFile = "access_token.txt";
$data = json_decode(file_get_contents($tokenFile,FILE_USE_INCLUDE_PATH));
//accesstoken有效期是7200秒,这里用到的文件缓存
//注意:文件权限问题
if (!$data->expire_time || $data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::APP_ID."&secret=".self::APP_SECRET;
$res = json_decode(file_get_contents($url));
if($res) {
$arr = array();
$access_token = $res->access_token;
$arr["expire_time"] = time() + 7000;
$arr["access_token"] = $access_token;
$fp = fopen($tokenFile, "w");
fwrite($fp, json_encode($arr));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
//post发送json数据
public function curl_post($url,$post_data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$res = curl_exec($ch);
if(!$res){
throw new Exception("发送消息失败:".curl_error($ch));
}
curl_close($ch);
}
};
$wechatObj = new wechatAPI();
//注意:第一步验证时打开,验证完成之后就可以注释了
// $wechatObj->isValid();
if($wechatObj->checkSignature() === true){
$xmlstring = file_get_contents("php://input");
$accept_info = $wechatObj->xml2Array($xmlstring)["xml"];
if($accept_info){
$ToUserName = $accept_info["ToUserName"];
$FromUserName = $accept_info["FromUserName"];
$CreateTime = $accept_info["CreateTime"];
$MsgType = $accept_info["MsgType"];
//$MsgId = $accept_info["MsgId"];
// $Encrypt = $accept_info["Encrypt"];
$data = array();
if($MsgType == "text"){//接收文本
$Content = $accept_info["Content"];//文本内容
// "touser": "OPENID",
// "msgtype": "link",
// "link": {
// "title": "Happy Day",
// "description": "Is Really A Happy Day",
// "url": "URL",
// "thumb_url": "THUMB_URL"
// }
if($Content === "图文") {
$data["touser"] = $FromUserName;
$data["msgtype"] = "link";
$data["link"]["title"] = urlencode("文章标题");
$data["link"]["description"] = urlencode("好文章要分享");
$data["link"]["url"] = "https://segmentfault.com";
$data["link"]["thumb_url"] = "https://static.segmentfault.com/v-5a7c12fe/global/img/logo-b.svg";
$wechatObj->send($data);exit;
}
//else if 可以做好多事
}else if($MsgType === "image") {//接收图片
}else if($MsgType === "event") {//进入客服窗口事件
$Event = $accept_info["Event"];
$SessionFrom = $accept_info["SessionFrom"];
if($Event == "user_enter_tempsession") {
$data["touser"] = $FromUserName;
$data["msgtype"] = "text";
$data["text"]["content"] = urlencode("您好很高兴为您服务");//urlencode 解决中文乱码问题
$wechatObj->send($data);exit;
}
}
echo "".$CreateTime." ";
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/28330.html
摘要:在杭州云栖大会上,阿里云正式发布云小蜜一款智能会话客服机器人。阿里巴巴推出云小蜜是开放智能服务能力赋能客服行业生态的举措。阿里巴巴于年底正式推出无线端多领域私人助理阿里小蜜,一款人工智能购物助理虚拟机器人。在2017杭州云栖大会上,阿里云正式发布云小蜜 —— 一款智能会话客服机器人。据介绍,智能客服机器人云小蜜具备36个预置的细分领域知识包,支持中文英文会话,可以7*24小时在线工作。目前,...
摘要:此次教程将再次带领大家体验消息推送,实现另一个微信消息推送的触发器,关于自动回复小程序客服消息的。我们需要通过控制台引擎触发器添加创建一个触发器,这个触发器将帮助我们在设置好的条件被触发的情况下运行云函数来实现用户消息的自动回复。 在上次推送完「卡券核销消息推送」的教程后,我们决定再多出点教程。 此次教程将再次带领大家体验「消息推送」,实现另一个「微信消息推送」的触发器,关于自动回复小...
阅读 2790·2019-08-30 15:55
阅读 1884·2019-08-30 15:53
阅读 2744·2019-08-29 18:38
阅读 991·2019-08-26 13:49
阅读 560·2019-08-23 15:42
阅读 3295·2019-08-22 16:33
阅读 1074·2019-08-21 17:59
阅读 1158·2019-08-21 17:11