分布式框架Celery六(Django发送邮件-短信-钉钉通知)

总代码



import requests


def get_access_token(appkey, appsecret):
    """
    获取access_token
    https://open.dingtalk.com/document/orgapp/obtain-orgapp-token

    :param appkey: 应用的唯一标识key
    :param appsecret: 应用的密钥
    :return:
    {
        "errcode": 0,
        "access_token": "96fc7a7axxx",
        "errmsg": "ok",
        "expires_in": 7200
    }
    """
    url = 'https://oapi.dingtalk.com/gettoken'
    params = {
        'appkey': appkey,
        'appsecret': appsecret
    }

    res = requests.get(url, params=params)
    return res.json()


def send_message(access_token, body):
    """
    发送应用消息
    https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
    """
    url = 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2'

    params = {
        'access_token': access_token,
    }

    res = requests.post(url, params=params, json=body)
    return res.json()


def get_dep_list(access_token):
    url = "https://oapi.dingtalk.com/department/list"
    data = {
        "access_token": access_token,
    }
    res_json = requests.get(url=url, params=data).json()

    print(res_json)
    return res_json['department'][0]['id']



def get_memberList(access_token,depId):
    url = "https://oapi.dingtalk.com/user/getDeptMember"
    data = {
        "access_token": access_token,
        "deptId": depId,
    }
    res_json = requests.get(url=url, params=data).json()

    return res_json


def get_all_member(access_token):
    url = "https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob"
    data = {
        "access_token": access_token,
    }
    body={
        'status_list':3,
        'offset':0,
        'size':50
    }
    res_json = requests.post(url=url, params=data,json=body).json()

    return res_json


if __name__ == '__main__':
    # 应用的唯一标识key
    appkey = 'dingonm5dhwzzwc1sgnj'

    # 应用的密钥
    appsecret = 'uIn2WinJ87THxLSjUAcuWQwg3hSVwG462UPm6yhi-mkvd2tb3ASjTOetYvuhvXef'

    # 发送消息时使用的微应用的AgentID
    agent_id = '3017843676'

    # 接收者的userid列表
    userid_list = ''

    token = get_access_token(appkey, appsecret)
    print(token)
    # 获取部门
    dept_id=get_dep_list(token['access_token'])

    # res=get_memberList(token['access_token'],dept_id)
    res=get_all_member(token['access_token'])
    print(res['result']['data_list'][0])
    # 发消息


    ret = send_message(token['access_token'], {
        "agent_id": agent_id,
        # "userid_list": res['userIds'][1],
        "userid_list": res['result']['data_list'][0],
        "msg": {
            "msgtype": "text",
            "text": {
                "content": "你好,钉钉通知~~~~"
            },
        },
    })
    # print(ret)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yjjpp2301

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值