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

目录

一、 创建群机器人

二、机器人配置

三、机器人信息推送

四、线上使用

 五、推送效果


一、 创建群机器人

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

二、机器人配置

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

三、机器人信息推送

  • 当前自定义机器人 支持文本(text)、markdown(markdown)、图片(image)、图文(news)四种消息类型
  • 我们只要根据它的文档说明,将指定类型的消息发送给 webhook 地址即可实现消息推送
// 文本消息类型
{
    "msgtype": "text",
    "text": {
        "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
        "mentioned_list":["wangqing","@all"],
        "mentioned_mobile_list":["13800001111","@all"]
    }
}

// markdown消息类型
{
    "msgtype": "markdown",
    "markdown": {
        "content": "实时新增用户反馈<font color="warning">132例</font>,请相关同事注意。\n
         >类型:<font color="comment">用户反馈</font>
         >普通用户反馈:<font color="comment">117例</font>
         >VIP用户反馈:<font color="comment">15例</font>"
    }
}

// 图片消息类型
{
    "msgtype": "image",
    "image": {
        "base64": "DATA",
        "md5": "MD5"
    }
}

// 图文消息类型
{
    "msgtype": "news",
    "news": {
       "articles" : [
           {
               "title" : "中秋节礼品领取",
               "description" : "今年中秋节公司有豪礼相送",
               "url" : "www.qq.com",
               "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
           }
        ]
    }
}

四、线上使用

  • 一般我们自己接入的程序中选择makedown消息推送和普通消息推送较多
  • 以下代码仅分享普通消息推送,作案例展示,具体请根据自己需求接入
<?php
/**
 * Created by PhpStorm.
 * User: autofelix
 * Date: 2021/5/18
 * Time: 22:00
 * Desc: 机器人实例.
 */

class robot
{
    //你的机器人webhook地址
    const ROBOT_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

    //日志上报
    public function report($content, $list = ['@all'])
    {
        $data = [
            'msgtype' => 'text',
            'text' => [
                'content' => $content,
                'mentioned_list' => $list,
                'mentioned_mobile_list' => $list
            ]
        ];

        $result = $this->post_curl(self::ROBOT_URL, json_encode($data));

        $result = json_decode($result, true);

        if ($result['errcode'] == 0) {
            //上报成功之后的逻辑
            echo '上报结果:' . $result['errmsg'];
        } else {
            //上报失败之后的逻辑
            echo '上报错误:' . $result['errmsg'];
        }
    }

    //请求
    protected function post_curl($url, $post_data, $header = [], $timeout = 5)
    {
        $ch = curl_init();  //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);  //抓取指定网页
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置header
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //设置不输出直接返回字符串
        curl_setopt($ch, CURLOPT_POST, 1);  //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $result = curl_exec($ch);  //运行curl
        curl_close($ch);

        return $result;
    }
}

(new robot())->report('上报错误日志');

 五、推送效果

评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江户川码农

你的支持是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值