Home > Article > PHP Framework > Tutorial for beginners: laravel combined with easywechat to send public account template messages
Recently, I received a new demand. I need to send template messages to users who follow the service account. I need to write a blog to record the journey to complete this demand. The expansion package uses easywechat
laravel Detailed instructions for sending public account template messages combined with easywechat. Thanks to the author of easywechat. It is very useful for novices!
Because our situation is quite special, the official account bound to the mini program and the official account to be pushed are not the same. This involves the possibility that the union_Id is inconsistent, so both official accounts need to be bound to the WeChat open platform. , if not, just register and bind
WeChat Open Platform Document
Configure js secure domain name
Generate secret (mainly save it, follow-up Resetting will affect the online business)
Fill in and enable the server configuration
The server address filled in here will be used to receive various event callbacks from the official account in the future, such as following and canceling
Local debugging requires intranet penetration. Search for specific tutorials yourself. I don’t know how
Modify the configuration WeChat will verify whether the filled in server address can be received normally, so it will go through a verification process and the interface needs to give the correct return parameters
Access document link reference
Because of WeChat verification and subsequent callbacks This route will be used, and the verification is a GET request, and subsequent event callbacks and the like are post requests, so the route needs to be set to any typeRoute::any('official/notify', 'WechatController@officialNotify');easywechat author An Zhengchao has considered it for usServer verification is compatible with message reception and reply in one link, so you can use it directly according to the document
public function officialNotify() { Log::channel('wechat')->info("公众号回调!!!!!1" ); $body = file_get_contents('php://input'); Log::channel('wechat')->info($body); $config = [ 'app_id' => config('wechat.yueliu_official_account.app_id'), 'secret' => config('wechat.yueliu_official_account.secret'), 'token' => config('wechat.yueliu_official_account.token'),// 'aes_key' => config('wechat.yueliu_official_account.aes_key'), // 明文模式请勿填写 EncodingAESKey 'aes_key' => '', // 明文模式请勿填写 EncodingAESKey 'log' => [ 'level' => 'error', 'file' => storage_path('logs/wechat.log'), ], 'response_type' => 'array' ]; $app = Factory::officialAccount($config); $app->server->push(function ($message) { Log::channel('wechat')->info($message); switch ($message['MsgType']) { case 'event': return '收到事件消息'; break; case 'text': return '收到文本消息'; break; case 'image': return '收到图片消息'; break; case 'voice': return '收到语音消息'; break; case 'video': return '收到视频消息'; break; case 'location': return '收到坐标消息'; break; case 'link': return '收到链接消息'; break; case 'file': return '收到文件消息'; // ... 其它消息 default: return '收到其它消息'; break; } }); // 在 laravel 中: $response = $app->server->serve(); // $response 为 `Symfony\Component\HttpFoundation\Response` 实例 // 对于需要直接输出响应的框架,或者原生 PHP 环境下 $response->send(); // 而 laravel 中直接返回即可: return $response; }
My business needs, after the user pays attention Send a message to the user that can jump to the mini program. Here, after receiving the event message, you need to determine whether it is an event of interest, and then change the return message to the following code. Click the a link here to directly open the mini program. , there will be no prompts like asking the user whether to confirm.
It should be noted that: following the public account, you can get the unionid and some basic information through [$app->user->get($openId);]. Unfollowing can only get openid
case 'event': return '欢迎关注音视频资产管理与协同交付平台「laravel」官方微信。 <a>点击跳转</a> 网页版请至: https://learnku.com'; break;
Rendering
##The follow callback event of the WeChat official account will store the user’s basic information It is also sent together with the unionid. Be sure to save the openid and unionid of the official account. Subsequent template messages will be sent according to the openid of the official account.Apply to activate the template message on the WeChat public platform. Find the template message in "New Features" at the bottom of the right menu and click to apply for activation. It will take about 1-3 working days. I passed it in just one day
After activation, select the industry and template type. If the template library provided by WeChat cannot be found and If your business is the same, you need to submit the application yourself, but this takes a long time, about 7-15 days. It is recommended to use the template library
here The template id must be stored in the code, and you need to use
$openId = '公众号的openid'; $config = [ 'app_id' => config('wechat.yueliu_official_account.app_id'), 'secret' => config('wechat.yueliu_official_account.secret'), 'token' => config('wechat.yueliu_official_account.token'), // 'aes_key' => config('wechat.yueliu_official_account.aes_key'), // 明文模式请勿填写 EncodingAESKey 'aes_key' => '', // 明文模式请勿填写 EncodingAESKey 'log' => [ 'level' => 'error', 'file' => storage_path('logs/wechat.log'), ], 'response_type' => 'array' ]; $app = Factory::officialAccount($config);// $user = $app->user->get($openId);// dd($user); // 发送模板消息 $app->template_message->send([ 'touser' => $openId, 'template_id' => '模板id', 'url' => 'http://www.网站.cn', 'miniprogram' => [ // 跳转到小程序,和上面的url同时存在的话,则优先显示小程序 'appid' => '小程序的id', 'pagepath' => '小程序页面地址', ], 'data' => [ 'first' => [ 'value' => '赵师傅已加入群组演示项目', 'color' => '#888888' ], 'keyword1' => [ 'value' => '加入项目' ], 'keyword2' => [ 'value' => '加入成功' ], 'keyword3' => [ 'value' => '2021-12-10 14:21:05' ], 'remark' => [ 'value' => '点击打开小程序' ], ], ]);Rendering The above is the entire process of laravel combined with easywechat to send public account template messages, complete!
Related recommendations: The latest five Laravel video tutorials
The above is the detailed content of Tutorial for beginners: laravel combined with easywechat to send public account template messages. For more information, please follow other related articles on the PHP Chinese website!