资讯专栏INFORMATION COLUMN

小程序接入客服消息,客服消息转发到网页版客服工具。

Lycheeee / 1397人阅读

摘要:小程序接入客服消息消息推送配置填写等。特别要注意的是数据格式的选择为了后续其他信息能够转发回微信自有网页版客服消息平台,建议选择格式。

小程序接入客服消息 1、消息推送配置

填写URL、Token等。

</>复制代码

  1. 特别要注意的是:数据格式的选择;

为了后续其他信息能够转发回微信自有网页版客服消息平台,建议选择xml格式。
json格式现在还有bug(2018.3.5),

bug主要原因返回的头部不符合http标准。
返回json格式header头应该是

</>复制代码

  1. Content-Type:application/json,

而微信的选json还是

</>复制代码

  1. Content-Type:text/xml,
2、代码如下,直接改改就能用(这里用的php)

</>复制代码

  1. checkSignature()) {
  2. echo $echoStr;
  3. exit;
  4. }
  5. }
  6. public function checkSignature(){
  7. $signature = $_GET["signature"];
  8. $timestamp = $_GET["timestamp"];
  9. $nonce = $_GET["nonce"];
  10. $token = TOKEN;
  11. $tmpArr = array($token, $timestamp, $nonce);
  12. sort($tmpArr, SORT_STRING);
  13. $tmpStr = implode( $tmpArr );
  14. $tmpStr = sha1( $tmpStr );
  15. if( $tmpStr == $signature ){
  16. return true;
  17. }else{
  18. return false;
  19. }
  20. }
  21. public function send($data){
  22. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->getAccessToken();
  23. $data = urldecode(json_encode($data));
  24. $this->curl_post($url,$data);
  25. }
  26. //xml数据转数组
  27. public function xml2Array($contents = NULL, $encoding = "UTF-8", $get_attributes = 1, $priority = "tag"){
  28. if (!$contents)
  29. {
  30. return array();
  31. }
  32. if (!function_exists("xml_parser_create"))
  33. {
  34. return array ();
  35. }
  36. $parser = xml_parser_create("");
  37. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $encoding);
  38. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  39. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  40. xml_parse_into_struct($parser, trim($contents), $xml_values);
  41. xml_parser_free($parser);
  42. if (!$xml_values)
  43. return array();
  44. $xml_array = array ();
  45. $parents = array ();
  46. $opened_tags = array ();
  47. $arr = array ();
  48. $current = & $xml_array;
  49. $repeated_tag_index = array ();
  50. foreach ($xml_values as $data)
  51. {
  52. unset ($attributes, $value);
  53. extract($data);
  54. $result = array ();
  55. $attributes_data = array ();
  56. if (isset ($value))
  57. {
  58. if ($priority == "tag")
  59. $result = trim($value);
  60. else
  61. $result["value"] = trim($value);
  62. }
  63. if (isset ($attributes) && $get_attributes) {
  64. foreach ($attributes as $attr => $val)
  65. {
  66. if ($priority == "tag")
  67. $attributes_data[$attr] = $val;
  68. else
  69. $result["attr"][$attr] = $val; //Set all the attributes in a array called "attr"
  70. }
  71. }
  72. if ($type == "open")
  73. {
  74. $parent[$level -1] = & $current;
  75. if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
  76. $current[$tag] = $result;
  77. if ($attributes_data)
  78. $current[$tag . "_attr"] = $attributes_data;
  79. $repeated_tag_index[$tag . "_" . $level] = 1;
  80. if (isset($tag) && $tag && isset($current[$tag])) {
  81. $current = & $current[$tag];
  82. }
  83. }
  84. else
  85. {
  86. if (isset ($current[$tag][0]))
  87. {
  88. $current[$tag][$repeated_tag_index[$tag . "_" . $level]] = $result;
  89. $repeated_tag_index[$tag . "_" . $level]++;
  90. }
  91. else
  92. {
  93. $current[$tag] = array (
  94. $current[$tag],
  95. $result
  96. );
  97. $repeated_tag_index[$tag . "_" . $level] = 2;
  98. if (isset ($current[$tag . "_attr"]))
  99. {
  100. $current[$tag]["0_attr"] = $current[$tag . "_attr"];
  101. unset ($current[$tag . "_attr"]);
  102. }
  103. }
  104. $last_item_index = $repeated_tag_index[$tag . "_" . $level] - 1;
  105. $current = & $current[$tag][$last_item_index];
  106. }
  107. }
  108. elseif ($type == "complete")
  109. {
  110. if (!isset ($current[$tag]))
  111. {
  112. $current[$tag] = $result;
  113. $repeated_tag_index[$tag . "_" . $level] = 1;
  114. if ($priority == "tag" && $attributes_data) {
  115. $current[$tag . "_attr"] = $attributes_data;
  116. }
  117. }
  118. else
  119. {
  120. if (isset ($current[$tag][0]) && is_array($current[$tag])) {
  121. $current[$tag][$repeated_tag_index[$tag . "_" . $level]] = $result;
  122. if ($priority == "tag" && $get_attributes && $attributes_data) {
  123. $current[$tag][$repeated_tag_index[$tag . "_" . $level] . "_attr"] = $attributes_data;
  124. }
  125. $repeated_tag_index[$tag . "_" . $level]++;
  126. }
  127. else
  128. {
  129. $current[$tag] = array (
  130. $current[$tag],
  131. $result
  132. );
  133. $repeated_tag_index[$tag . "_" . $level] = 1;
  134. if ($priority == "tag" && $get_attributes) {
  135. if (isset ($current[$tag . "_attr"]) && is_array($current[$tag]))
  136. {
  137. $current[$tag]["0_attr"] = $current[$tag . "_attr"];
  138. unset ($current[$tag . "_attr"]);
  139. }
  140. if ($attributes_data)
  141. {
  142. $current[$tag][$repeated_tag_index[$tag . "_" . $level] . "_attr"] = $attributes_data;
  143. }
  144. }
  145. $repeated_tag_index[$tag . "_" . $level]++; //0 and 1 index is already taken
  146. }
  147. }
  148. }
  149. elseif ($type == "close")
  150. {
  151. $current = & $parent[$level -1];
  152. }
  153. }
  154. return ($xml_array);
  155. }
  156. //获取accesstoken
  157. public function getAccessToken() {
  158. $tokenFile = "access_token.txt";
  159. $data = json_decode(file_get_contents($tokenFile,FILE_USE_INCLUDE_PATH));
  160. //accesstoken有效期是7200秒,这里用到的文件缓存
  161. //注意:文件权限问题
  162. if (!$data->expire_time || $data->expire_time < time()) {
  163. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::APP_ID."&secret=".self::APP_SECRET;
  164. $res = json_decode(file_get_contents($url));
  165. if($res) {
  166. $arr = array();
  167. $access_token = $res->access_token;
  168. $arr["expire_time"] = time() + 7000;
  169. $arr["access_token"] = $access_token;
  170. $fp = fopen($tokenFile, "w");
  171. fwrite($fp, json_encode($arr));
  172. fclose($fp);
  173. }
  174. } else {
  175. $access_token = $data->access_token;
  176. }
  177. return $access_token;
  178. }
  179. //post发送json数据
  180. public function curl_post($url,$post_data){
  181. $ch = curl_init();
  182. curl_setopt($ch, CURLOPT_URL, $url);
  183. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  184. curl_setopt($ch, CURLOPT_POST, 1);
  185. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  186. $res = curl_exec($ch);
  187. if(!$res){
  188. throw new Exception("发送消息失败:".curl_error($ch));
  189. }
  190. curl_close($ch);
  191. }
  192. };
  193. $wechatObj = new wechatAPI();
  194. //注意:第一步验证时打开,验证完成之后就可以注释了
  195. // $wechatObj->isValid();
  196. if($wechatObj->checkSignature() === true){
  197. $xmlstring = file_get_contents("php://input");
  198. $accept_info = $wechatObj->xml2Array($xmlstring)["xml"];
  199. if($accept_info){
  200. $ToUserName = $accept_info["ToUserName"];
  201. $FromUserName = $accept_info["FromUserName"];
  202. $CreateTime = $accept_info["CreateTime"];
  203. $MsgType = $accept_info["MsgType"];
  204. //$MsgId = $accept_info["MsgId"];
  205. // $Encrypt = $accept_info["Encrypt"];
  206. $data = array();
  207. if($MsgType == "text"){//接收文本
  208. $Content = $accept_info["Content"];//文本内容
  209. // "touser": "OPENID",
  210. // "msgtype": "link",
  211. // "link": {
  212. // "title": "Happy Day",
  213. // "description": "Is Really A Happy Day",
  214. // "url": "URL",
  215. // "thumb_url": "THUMB_URL"
  216. // }
  217. if($Content === "图文") {
  218. $data["touser"] = $FromUserName;
  219. $data["msgtype"] = "link";
  220. $data["link"]["title"] = urlencode("文章标题");
  221. $data["link"]["description"] = urlencode("好文章要分享");
  222. $data["link"]["url"] = "https://segmentfault.com";
  223. $data["link"]["thumb_url"] = "https://static.segmentfault.com/v-5a7c12fe/global/img/logo-b.svg";
  224. $wechatObj->send($data);exit;
  225. }
  226. //else if 可以做好多事
  227. }else if($MsgType === "image") {//接收图片
  228. }else if($MsgType === "event") {//进入客服窗口事件
  229. $Event = $accept_info["Event"];
  230. $SessionFrom = $accept_info["SessionFrom"];
  231. if($Event == "user_enter_tempsession") {
  232. $data["touser"] = $FromUserName;
  233. $data["msgtype"] = "text";
  234. $data["text"]["content"] = urlencode("您好很高兴为您服务");//urlencode 解决中文乱码问题
  235. $wechatObj->send($data);exit;
  236. }
  237. }
  238. echo "".$CreateTime."";
  239. }
  240. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28330.html

相关文章

  • 京东咚咚架构演进

    摘要:自从京东开始为第三方卖家提供入驻平台服务后,咚咚也就随之诞生了。之后开始接手了京东咚咚,并持续完善这个产品,进行了三次技术架构演进。涅槃至今年京东的组织架构发生了很大变化,从一个公司变成了一个集团,下设多个子公司。 showImg(https://segmentfault.com/img/remote/1460000009083941); 咚咚是什么?咚咚之于京东相当于旺旺之于淘宝,它...

    marser 评论0 收藏0
  • 每天服务600万用户不疲惫 阿里云推智能客服机器人

    摘要:在杭州云栖大会上,阿里云正式发布云小蜜一款智能会话客服机器人。阿里巴巴推出云小蜜是开放智能服务能力赋能客服行业生态的举措。阿里巴巴于年底正式推出无线端多领域私人助理阿里小蜜,一款人工智能购物助理虚拟机器人。在2017杭州云栖大会上,阿里云正式发布云小蜜 —— 一款智能会话客服机器人。据介绍,智能客服机器人云小蜜具备36个预置的细分领域知识包,支持中文英文会话,可以7*24小时在线工作。目前,...

    leiyi 评论0 收藏0
  • 【实战教程】使用知晓云完成程序客服消息的自动回复

    摘要:此次教程将再次带领大家体验消息推送,实现另一个微信消息推送的触发器,关于自动回复小程序客服消息的。我们需要通过控制台引擎触发器添加创建一个触发器,这个触发器将帮助我们在设置好的条件被触发的情况下运行云函数来实现用户消息的自动回复。 在上次推送完「卡券核销消息推送」的教程后,我们决定再多出点教程。 此次教程将再次带领大家体验「消息推送」,实现另一个「微信消息推送」的触发器,关于自动回复小...

    ytwman 评论0 收藏0

发表评论

0条评论

Lycheeee

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<