当前位置:   article > 正文

企业微信接入群聊机器人详细步骤_企业微信机器人

企业微信机器人

目录

一、 创建群机器人

二、机器人配置

三、机器人信息推送

四、线上使用

 五、推送效果


一、 创建群机器人

  • 先选择一个企业微信群
  • 右键添加机器人
  • 完善机器人的头像、名称即可

二、机器人配置

  • 查看生成的机器人webhook地址
  • 点击地址,里面可以查看文档和一些简单的配置
  • 自定义配置可以配置IP白名单,以及推送消息示例

三、机器人信息推送

  • 当前自定义机器人 支持文本(text)、markdown(markdown)、图片(image)、图文(news)四种消息类型
  • 我们只要根据它的文档说明,将指定类型的消息发送给 webhook 地址即可实现消息推送
  1. // 文本消息类型
  2. {
  3. "msgtype": "text",
  4. "text": {
  5. "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
  6. "mentioned_list":["wangqing","@all"],
  7. "mentioned_mobile_list":["13800001111","@all"]
  8. }
  9. }
  10. // markdown消息类型
  11. {
  12. "msgtype": "markdown",
  13. "markdown": {
  14. "content": "实时新增用户反馈<font color="warning">132例</font>,请相关同事注意。\n
  15. >类型:<font color="comment">用户反馈</font>
  16. >普通用户反馈:<font color="comment">117例</font>
  17. >VIP用户反馈:<font color="comment">15例</font>"
  18. }
  19. }
  20. // 图片消息类型
  21. {
  22. "msgtype": "image",
  23. "image": {
  24. "base64": "DATA",
  25. "md5": "MD5"
  26. }
  27. }
  28. // 图文消息类型
  29. {
  30. "msgtype": "news",
  31. "news": {
  32. "articles" : [
  33. {
  34. "title" : "中秋节礼品领取",
  35. "description" : "今年中秋节公司有豪礼相送",
  36. "url" : "www.qq.com",
  37. "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
  38. }
  39. ]
  40. }
  41. }

四、线上使用

  • 一般我们自己接入的程序中选择makedown消息推送和普通消息推送较多
  • 以下代码仅分享普通消息推送,作案例展示,具体请根据自己需求接入
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: autofelix
  5. * Date: 2021/5/18
  6. * Time: 22:00
  7. * Desc: 机器人实例.
  8. */
  9. class robot
  10. {
  11. //你的机器人webhook地址
  12. const ROBOT_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
  13. //日志上报
  14. public function report($content, $list = ['@all'])
  15. {
  16. $data = [
  17. 'msgtype' => 'text',
  18. 'text' => [
  19. 'content' => $content,
  20. 'mentioned_list' => $list,
  21. 'mentioned_mobile_list' => $list
  22. ]
  23. ];
  24. $result = $this->post_curl(self::ROBOT_URL, json_encode($data));
  25. $result = json_decode($result, true);
  26. if ($result['errcode'] == 0) {
  27. //上报成功之后的逻辑
  28. echo '上报结果:' . $result['errmsg'];
  29. } else {
  30. //上报失败之后的逻辑
  31. echo '上报错误:' . $result['errmsg'];
  32. }
  33. }
  34. //请求
  35. protected function post_curl($url, $post_data, $header = [], $timeout = 5)
  36. {
  37. $ch = curl_init(); //初始化curl
  38. curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
  39. if ($header) {
  40. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置header
  41. }
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置不输出直接返回字符串
  43. curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  45. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  48. $result = curl_exec($ch); //运行curl
  49. curl_close($ch);
  50. return $result;
  51. }
  52. }
  53. (new robot())->report('上报错误日志');

 五、推送效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/178432
推荐阅读
相关标签
  

闽ICP备14008679号