当前位置:编程学习 > > 正文

如何用微信公众号发送信息(微信公众号之主动给用户发送消息功能)

时间:2022-01-23 02:13:16类别:编程学习

如何用微信公众号发送信息

微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。

即客户要求,他在后台编辑好文章后要主动给每个用户都发送消息,并可以让用户点击直接进入文章页面。

于是乎,当时脑子一热,想着没什么大的问题,so easy。模板消息不就得了。

后来在写代码的过程中却发现,并不行。

微信公众号中模板消息有很严格的限制。

1.必须有用户主动触发事件方可发送模板消息

2.模板消息一分钟只能发送六十条,超过六十条,不好意思。你懂。

于是乎,就想到了另一种方法:群发消息

但是一看文档中群发消息的限制,发现悲催了

群发消息服务号每个月最多只能发送4条。

而客户要求的是随时随地就能发送.这个4条明显不符合要求的。

怎么办呢?一度陷入困难之中。只好继续查看开发文档。

当看到客服功能时,突然想到,能不能用客服主动给用户发送消息呢?

于是乎,想到就去试验下。

首先,先在公众号后台功能模块中-》添加功能模块  添加   客服功能   模块

如图所示:

然后就进入代码环节了。

先添加客服。也可以公众号后台添加

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • //先添加客服
  • function addkf()
  • {
  •  $token = getToken();
  •  $url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='.$token;
  •  $data = '{
  •    "kf_account" : "system@system",
  •    "nickname" : "客服1",
  •    "password" : "admin",
  •  }';
  •  echo https_request($url,$data);
  • }
  • 然后就是主动发送消息的方法

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • //获取access_token的方法。
  • function getToken()
  • {
  •  $appid = 'appid';
  •  $appsecret = 'appsecret';
  •  $token_file = dirname(dirname(__FILE__)).'/data/token.txt';
  •  if(!file_exists($token_file) || ((time() - filemtime($token_file)) > 7000)){
  •   $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
  •   $json=file_get_contents($TOKEN_URL);
  •   $result=json_decode($json);
  •   $ACC_TOKEN=$result->access_token;
  •   file_put_contents($token_file,$ACC_TOKEN);
  •  }else{
  •   $ACC_TOKEN = file_get_contents($token_file);
  •  }
  •  return $ACC_TOKEN;
  • }
  • //调用发送方法
  • function sendmsg($content,$openid)
  • {
  •  $token = getToken();
  •  $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;
  •  $content = '感谢你的关注\n回复你厉害 \n例如<a href=\"http://www.baidu.com\">回复123456</a>';
  •  $data = '{
  •   "touser":"'.$openid.'",
  •   "msgtype":"text",
  •   "text":
  •   {
  •     "content":"'.$content.'"
  •   }
  •  }';
  •  https_request($url,$data);
  •  return true;
  • }
  • /**
  •  * request 请求
  •  */
  • function https_request($url, $data = null){
  •  $curl = curl_init();
  •  curl_setopt($curl, CURLOPT_URL, $url);
  •  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  •  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  •  if (!empty($data)){
  •   curl_setopt($curl, CURLOPT_POST, 1);
  •   curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  •  }
  •  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  •  $output = curl_exec($curl);
  •  curl_close($curl);
  •  return $output;
  • }
  • 使用的时候直接调用 sendmsg()方法传值就可以了。

    最后上完成的图

    如图所示,公众号可以给用户发送消息,而且发送的内容你可以使用html 标签哦

    虽然这样感觉很方便,但是同样,微信公众平台对此也有限制。

    1.用户必须关注公众号,方可收到信息。

    2.用户只能连续收到   20   条客服消息。超过后用户就收不到消息,

    超过后,用户必须   主动给公众号发送消息  。或者  点击菜单栏,这样20条消息的限制就会重置。

    总结

    以上所述是小编给大家介绍的微信公众号之主动给用户发送消息功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

    原文链接:https://www.cnblogs.com/YFYQ/p/11067840.html

    上一篇下一篇

    猜您喜欢

    热门推荐