模板消息
模板消息僅用于公眾號向用戶發送重要的服務通知,只能用于符合其要求的服務場景中,如信用卡刷卡通知、商品購買成功通知等。不支持廣告等營銷類消息以及其它所有可能對用戶造成騷擾的消息。
修改賬號所屬行業
$officialAccount->templateMessage->setIndustry($industryId1, $industryId2);
獲取支持的行業列表
$officialAccount->templateMessage->getIndustry();
添加模板
在公眾號后臺獲取 $shortId
并添加到賬戶。
$officialAccount->templateMessage->addTemplate($shortId);
獲取所有模板列表
$officialAccount->templateMessage->getPrivateTemplates();
刪除模板
$officialAccount->templateMessage->deletePrivateTemplate($templateId);
發送模板消息
<?php
$officialAccount->templateMessage->send([
'touser' => 'user-openid',
'template_id' => 'template-id',
'url' => 'http://www.fe88.cn',
'miniprogram' => [
'appid' => 'xxxxxxx',
'pagepath' => 'pages/xxx',
],
'data' => [
'key1' => 'VALUE',
'key2' => 'VALUE2',
// ...
],
]);
如果 url
和 miniprogram
字段都傳,會優先跳轉小程序。
發送一次性訂閱消息
<?php
$officialAccount->templateMessage->sendSubscription([
'touser' => 'user-openid',
'template_id' => 'template-id',
'url' => 'http://www.fe88.cn',
'scene' => 1000,
'data' => [
'key1' => 'VALUE',
'key2' => 'VALUE2',
// ...
],
]);
如果你想為發送的內容字段指定顏色,你可以將 "data"
部分寫成下面 4
種不同的樣式,不寫 color
將會是默認黑色:
'data' => [
'foo' => '你好', // 不需要指定顏色
'bar' => ['你好', '#F00'], // 指定為紅色
'baz' => ['value' => '你好', 'color' => '#550038'], // 與第二種一樣
'zoo' => ['value' => '你好'], // 與第一種一樣
]