自动统计bug - 向钉钉和企业微信群,发送消息(智能群助手)

1. 在钉钉和微信群助手中,添加智能机器人(选择自定义机器人)

    

(钉钉)                                                                                                             (微信)

 

 2. 获取webhook地址

一般如下格式:

https://oapi.dingtalk.com/robot/send?access_token=123abc(钉钉)

https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=123abc(微信)

群发消息的本质,即向这个webhook地址发送http请求(post),发送请求时,必须将字符集编码设置成UTF-8

 

3. 钉钉中需要进行安全设置

可以自定义关键词:最多可以设置10个关键词,消息中至少包含其中1个关键词才可以发送成功

微信无此限制

 

4. 支持格式如下:

钉钉:支持文本 (text)、链接 (link)、markdown(markdown)、ActionCard、FeedCard消息类型

微信:支持文本、markdown、图片、图文

 

5. 可以编写代码发送群消息啦~

以发送文本消息为例:

import requests
import json


# 机器人基类
class RobotBase:
    def __init__(self):
        self.__headers = {'Content-Type': 'application/json;charset=utf-8'}
        self.url = ''

    def send_msg(self,text):
        json_text = {
            "msgtype": "text",
            "text": {
                "content": text
            },
            "at": {
                "atMobiles": [
                    ""
                ],
                "isAtAll": True
            }
        }
        return requests.post(self.url, json.dumps(json_text), headers=self.__headers).content


# 机器人子类 - 钉钉机器人
class RobotDingtalk(RobotBase):
    def __init__(self):
        super().__init__()
        # 填写钉钉机器人的url
        self.url = 'https://oapi.dingtalk.com/robot/send?access_token=123abc'


# 机器人子类 - 微信机器人
class RobotWeixin(RobotBase):
    def __init__(self):
        super().__init__()
        # 填写微信机器人的url
        self.url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=123-abc'


if __name__ == '__main__':
    robot_ding = RobotDingtalk()
    robot_ding.send_msg('hello dingding')   # 向钉钉群发消息

    robot_weixin = RobotWeixin()
    robot_weixin.send_msg('hello weixin')   # 向微信群发消息

 以上是艾特所有人,如果要艾特指定人,如下:

# 机器人基类
class RobotBase:
    def __init__(self):
        self.__headers = {'Content-Type': 'application/json;charset=utf-8'}
        self.url = ''

    def send_msg(self,text):
        json_text = {
            "msgtype": "text",
            "text": {
                "content": text
            },
            "at": {
                "atMobiles": [
                    "17765006069"   # 艾特指定人(手机号)
                ],
                "isAtAll": False    # 不艾特所有人
            }
        }
        return requests.post(self.url, json.dumps(json_text), headers=self.__headers).content

 

6. 开发文档 

 钉钉机器人开发文档:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/26eaddd5

 微信机器人开发文档:在企业微信群机器人配置页面(配置说明中)

 

.

posted @ 2020-05-09 15:31  小虫虫大虫虫  阅读(1121)  评论(0编辑  收藏  举报