WeChat public number custom menu to activate WeChat customer service


1. preparation work: Certified subscription number, opening customer service function, adding a customer service, closing the developer function of public address (or completing business logic on its own).

2. train of thought: click on the menu bar to jump to the public address page, request the background, get the openid backstage, then send the customer service message to the current micro signal (the background sends the customer service message to the user at this time, the user has not yet established a connection with the customer service. After the user sends the message, the customer service can automatically connect when the customer service web page is connected to the WeChat customer service), and turn off the current page to the public address.

After the user sends the message, the WeChat public number customer service web page can be received.

The effect is as follows. The picture is from the Internet

3. Specific code implementation

I use the TP framework in demo, as long as I have the same idea

Background code, which can not be used directly, can be adjusted according to its actual business logic

namespace Home\Controller;
use Think\Controller;
class KefuController extends Controller {
	//The code cannot be used directly, and can be adjusted according to its actual business logic
    public function index(){
    	$openid = session('openid')
		if(empty($openid)){//Wechat login if there is no openid
			$this->wxLogin();//Specific wechat login business logic self implementation
			//send message
			//Access token is obtained, the code is not implemented, and it is implemented according to its own business logic
			$accessToken = $this->getAccessToken();
			$sendurl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$accessToken;
			//     "touser":"OPENID",
			//     "msgtype":"text",
			//     "text":
			//     {
			//          "content":"Hello World"
			//     }
			// }
			$data = array(
				'touser' => session('openid'),
				'msgtype' => 'text',
				'text' => array(
					'content' => "Please input the question you want to ask. If you haven't replied for a long time, there may be no customer service online. Please try again later. Online time: 9 working days:00-18:00"
			$res = $this->httpPost(json_encode($data,JSON_UNESCAPED_UNICODE),$sendurl);//Send request
			//var_dump($res); you can make a result judgment or other logic
		//Because JSSDK interface is required, please refer to the development document for the details of the required parameters
		$signPackage = $this->getSignPackage();
    private function httpPost($curlPost,$url){
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$return_str = curl_exec($curl);
		return $return_str;

Foreground code, which can not be used directly, can be adjusted according to its actual business logic

<!doctype html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <meta http-equiv="X-UA-Compatible"content="IE=edge">
    <meta content="telephone=no" name="format-detection" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <title>Customer service</title>
		<!-- Content can be defined by yourself -->
		//Skipping to customer service
	<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
	   * Be careful:
	   * 1. All JS interfaces can only be invoked under the domain name of the public number. The developer of the public number needs to login to the WeChat public platform to enter the "function settings" of the "public number settings" to fill in the "JS interface security domain name".
	   * 2. If you find that you cannot share custom content on Android, go to the official website to download the latest package coverage installation. The Android custom sharing interface needs to be upgraded to version or above.
	   * 3. FAQ and complete JS SDK document address: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
	   * Problems encountered in the development are detailed in the document "Appendix 5 - common errors and solutions". If they are still not solved, feedback can be provided through the following channels:
	   * Email address: weixin-open@qq.com
	   * Email subject: [wechat JS-SDK feedback] specific questions
	   * Description of email content: describe the problem in a concise language, and explain the scene of the problem clearly. You can attach a screenshot. Wechat team will deal with your feedback as soon as possible.
	    debug: false,
	    appId: '{$signPackage["appId"]}',
	    timestamp: {$signPackage["timestamp"]},
	    nonceStr: '{$signPackage["nonceStr"]}',
	    signature: '{$signPackage["signature"]}',
	    jsApiList: [
	      // All API s to be called are added to this list
	      'closeWindow'//Close window interface
	  wx.ready(function () {
	    // Call API here

4. in the public menu settings, the menu is linked to its own background link. When it comes into effect, clicking the menu will jump to the public address page, automatically send the message to the user according to the logic, and users can send messages to customer service directly in the public number.

Reference document

JS-SDK description

Customer service message - send message

The code is not easy. Please indicate the source of reprint

Published 1 original article, praised 0 and visited 3
Private letter follow

Tags: curl SDK Session Android

Posted on Sun, 02 Feb 2020 06:39:42 -0500 by Tonka1979