隨著網(wǎng)上商城的流行,微信商城借助微信這一超級(jí)流量入口,成為電商企業(yè)的標(biāo)配。微信商城中的自動(dòng)回復(fù)是一種提升商城體驗(yàn)的方式,如何實(shí)現(xiàn)文本消息自動(dòng)回復(fù)及系統(tǒng)關(guān)注自動(dòng)回復(fù),本文安菲云將以實(shí)際代碼給大家分享。
首先,我們需要設(shè)計(jì)數(shù)據(jù)庫表,將自動(dòng)回復(fù)的消息類型和內(nèi)容、觸發(fā)回復(fù)的關(guān)鍵字等預(yù)設(shè)好模板,我們按照如下設(shè)計(jì)好數(shù)據(jù)庫:
我們以安菲多用戶商城的微信商城代碼為例:
消息自動(dòng)回復(fù):
/*************** 被動(dòng)回復(fù)消息 ****************/
public function _doText($postObj)
{
$time = time();
$msgType = "text";
$keyword =
trim($postObj->Content);// 用戶發(fā)送過來的關(guān)鍵字
$keyword = "$keyword";
$m = Db::name('wx_passive_replys');
$msgType =
$m->where([['keyword','=',$keyword]])->value('msgType');
if($msgType=='text'){
$contentStr =
$m->where([['keyword','=',$keyword],['dataFlag','=',1]])->value('content');
$resultStr = sprintf($this->tpl['text'],
$postObj->FromUserName, $postObj->ToUserName, $time, $contentStr);
echo $resultStr;
}elseif($msgType=='news'){
// 圖文消息最多發(fā)送10條
$news =
$m->field('title,description,picurl,url')->where([['keyword','=',$keyword],['dataFlag','=',1]])->limit(10)->select();
$count = count($news);
$newC='';
for($i=0;$i<$count;++$i){
$newC .=
sprintf($this->tpl['content'], $news[$i]['title'], $news[$i]['description'],
WSTConf('CONF.resourcePath').'/'.$news[$i]['picurl'], $news[$i]['url']);
}
//將內(nèi)容輸出到新聞模板
$news =
sprintf($this->tpl['news'], $postObj->FromUserName,
$postObj->ToUserName, $time, $count, $newC);
echo $news;
}
exit;
}
系統(tǒng)關(guān)注自動(dòng)回復(fù):
/**
* 關(guān)注回復(fù)事件第一條用關(guān)注事件推送,之后的用客服消息推送
*/
public function
subscribeEvent($postObj){
$m = Db::name('wx_passive_replys');
$subscribes =
$m->where([['dataFlag','=',1],['isSubscribe','=',1]])->order('subscribeSort
asc,id desc')->limit(10)->select();
$userOpenId = (string)$postObj->FromUserName;
foreach ($subscribes as $key => $v) {
if($key==0){
$resultStr = '';
switch ($v['msgType']) {
case 'text':$resultStr
= sprintf($this->tpl['text'], $postObj->FromUserName, $postObj->ToUserName,
time(), $v['content'], 0);
break;
case 'news':$newC =
sprintf($this->tpl['content'], $v['title'], $v['description'],
WSTConf('CONF.resourcePath').'/'.$v['picUrl'], $v['url']);
$resultStr =
sprintf($this->tpl['news'], $postObj->FromUserName,
$postObj->ToUserName, time(), 1, $newC);
break;
}
echo $resultStr;
}else{
$sendData = [];
$sendData['touser'] = $userOpenId;
switch ($v['msgType']) {
case
'text':$sendData['msgtype'] = 'text';
$sendData['text'] = ['content'=>urlencode($v['content'])];
break;
case 'news':$sendData['msgtype'] =
'news';
$sendData['news'] = ['articles'=>[[
'title'=>urlencode($v['title']),
'description'=>urlencode($v['description']),
'url'=>$v['url'],
'picurl'=>WSTConf('CONF.resourcePath').'/'.$v['picUrl']
]]
];
break;
}
$this->sendCustomMessage(urldecode(json_encode($sendData)));
}
}
}