摘要:小程序接入客服消息消息推送配置填寫等。特別要注意的是數(shù)據(jù)格式的選擇為了后續(xù)其他信息能夠轉(zhuǎn)發(fā)回微信自有網(wǎng)頁版客服消息平臺(tái),建議選擇格式。
小程序接入客服消息 1、消息推送配置
填寫URL、Token等。
特別要注意的是:數(shù)據(jù)格式的選擇;
為了后續(xù)其他信息能夠轉(zhuǎn)發(fā)回微信自有網(wǎng)頁版客服消息平臺(tái),建議選擇xml格式。
json格式現(xiàn)在還有bug(2018.3.5),
bug主要原因返回的頭部不符合http標(biāo)準(zhǔn)。
返回json格式header頭應(yīng)該是
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數(shù)據(jù)轉(zhuǎn)數(shù)組 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秒,這里用到的文件緩存 //注意:文件權(quán)限問題 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發(fā)送json數(shù)據(jù) 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("發(fā)送消息失敗:".curl_error($ch)); } curl_close($ch); } }; $wechatObj = new wechatAPI(); //注意:第一步驗(yàn)證時(shí)打開,驗(yàn)證完成之后就可以注釋了 // $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"];//文本內(nèi)容 // "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("文章標(biāo)題"); $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") {//接收?qǐng)D片 }else if($MsgType === "event") {//進(jìn)入客服窗口事件 $Event = $accept_info["Event"]; $SessionFrom = $accept_info["SessionFrom"]; if($Event == "user_enter_tempsession") { $data["touser"] = $FromUserName; $data["msgtype"] = "text"; $data["text"]["content"] = urlencode("您好很高興為您服務(wù)");//urlencode 解決中文亂碼問題 $wechatObj->send($data);exit; } } echo ""; } } ".$CreateTime."
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/28330.html
摘要:自從京東開始為第三方賣家提供入駐平臺(tái)服務(wù)后,咚咚也就隨之誕生了。之后開始接手了京東咚咚,并持續(xù)完善這個(gè)產(chǎn)品,進(jìn)行了三次技術(shù)架構(gòu)演進(jìn)。涅槃至今年京東的組織架構(gòu)發(fā)生了很大變化,從一個(gè)公司變成了一個(gè)集團(tuán),下設(shè)多個(gè)子公司。 showImg(https://segmentfault.com/img/remote/1460000009083941); 咚咚是什么?咚咚之于京東相當(dāng)于旺旺之于淘寶,它...
摘要:在杭州云棲大會(huì)上,阿里云正式發(fā)布云小蜜一款智能會(huì)話客服機(jī)器人。阿里巴巴推出云小蜜是開放智能服務(wù)能力賦能客服行業(yè)生態(tài)的舉措。阿里巴巴于年底正式推出無線端多領(lǐng)域私人助理阿里小蜜,一款人工智能購(gòu)物助理虛擬機(jī)器人。在2017杭州云棲大會(huì)上,阿里云正式發(fā)布云小蜜 —— 一款智能會(huì)話客服機(jī)器人。據(jù)介紹,智能客服機(jī)器人云小蜜具備36個(gè)預(yù)置的細(xì)分領(lǐng)域知識(shí)包,支持中文英文會(huì)話,可以7*24小時(shí)在線工作。目前,...
摘要:此次教程將再次帶領(lǐng)大家體驗(yàn)消息推送,實(shí)現(xiàn)另一個(gè)微信消息推送的觸發(fā)器,關(guān)于自動(dòng)回復(fù)小程序客服消息的。我們需要通過控制臺(tái)引擎觸發(fā)器添加創(chuàng)建一個(gè)觸發(fā)器,這個(gè)觸發(fā)器將幫助我們?cè)谠O(shè)置好的條件被觸發(fā)的情況下運(yùn)行云函數(shù)來實(shí)現(xiàn)用戶消息的自動(dòng)回復(fù)。 在上次推送完「卡券核銷消息推送」的教程后,我們決定再多出點(diǎn)教程。 此次教程將再次帶領(lǐng)大家體驗(yàn)「消息推送」,實(shí)現(xiàn)另一個(gè)「微信消息推送」的觸發(fā)器,關(guān)于自動(dòng)回復(fù)小...
閱讀 2715·2019-08-30 15:55
閱讀 1834·2019-08-30 15:53
閱讀 2694·2019-08-29 18:38
閱讀 954·2019-08-26 13:49
閱讀 527·2019-08-23 15:42
閱讀 3194·2019-08-22 16:33
閱讀 1031·2019-08-21 17:59
閱讀 1107·2019-08-21 17:11