Home  >  Article  >  WeChat Applet  >  WeChat applet implements sending template messages

WeChat applet implements sending template messages

小云云
小云云Original
2018-05-09 21:50:318760browse

WeChat Mini Program You Draw I Guess demo complete source code WeChat Mini Program Consumption Calculator demo complete source code WeChat Mini program imitates Shanlinbao demo complete source code

WeChat mini program imitates Bilibili Video Station demo complete source code WeChat mini program simple metronome demo complete source code

Chaige WeChat Mini Program Application Store Source Code WeChat Mini Program demo: Guoku updated version WeChat Mini Program demo: Yang Tao

WeChat mini program demo: Zhihu Daily Recommended demo for learning WeChat mini program: chat room + audio and video + adding content + Baidu translation

Click the tabbar to change icon WeChat mini program demo: imitation mall WeChat mini program demo: imitation NetEase Cloud Music

Simple Left swipe operation and waterfall flow layout More mini program template source code

This article mainly shares with you the WeChat mini program to implement sending template messages, which is different from the official account sending template messages. There is no subscription (following) in the mini program, so there is no way to push messages to users through the dialogue window. Template messages issued by the mini program are uniformly sent through WeChat "Service Notification", as shown below:

But its implementation The process is the same as sending template messages from public accounts. The steps are as follows:
1. Obtain access_token
access_token is a globally unique interface calling credential. Developers need to use access_token when calling each interface and need to keep it properly. I have done WeChat development and public account development, which are the most basic. The method of obtaining access_token here is exactly the same as that of public account. For the method, see: Obtain WeChat basic interface credential Access_token

2. Add template message
and follow The same as the official account, you need to add a template message in the background of the mini program, and then obtain the corresponding template message ID.

3. Send template message
According to the template message keyword corresponding to the template message ID, the message content is constructed on the backend. When the user submits the form or makes payment, the form_id is obtained (in the form submission scenario, It is the formId brought in the submit event; in the payment scenario, it is the prepay_id of this payment), and then the end sends the message. The message parameters are as follows:

The code implementation example is as follows:

function sendtpl(){
	$accessToken = 'xxxxxxxxxxxxxxxxxxxxxx';
	$postData = array(
		"touser"		=>'xxxxxxxxxxxxxxxxxxxxxx',  	//用户openid
		"template_id"	=>'xxxxxxxxxxxxxxxxxxxxxx', 	//模板消息ID
		"page"			=>'pages/index/index',          
		"form_id"		=>'xxxxxxxxxxxxxxxxxxxxxx',   	//表单提交场景下,事件带上的 formId;支付场景下,为本次支付的 prepay_id 
		"data"			=>array(
				'keyword1'	=> array('value'=>'港荣蒸蛋糕','color'=>'#000000'),
				'keyword2'	=> array('value'=>'您购买的的港荣蒸蛋糕已经发货,点击进入小程序查看详情','color'=>'#000000')
			),
		'emphasis_keyword'=>''
	);
	$postData =  json_encode($postData,JSON_UNESCAPED_UNICODE);
	$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
	$rtn = request($url,true,'POST',$postData);
	
	return $rtn;
}

Related recommendations:

Implement WeChat applet Unrestricted template messages, unlimited active push

PHP implementation of WeChat template message example

WeChat development template message reply

The above is the detailed content of WeChat applet implements sending template messages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn