钉钉发送通知

from flask import Flask,render_template,request,jsonify
from flask_script import Manager
# from  urllib import request
import json,time,datetime
import hashlib
import requests

import const

from dingtalk import SecretClient, AppKeyClient
from dingtalk.model.message import TextBody


app = Flask(__name__)
app.config["DEBUG"]=True

manager = Manager(app)

@app.route("/")
def index():
    return jsonify({"msg":"index"})

# agentid – 企业应用id,这个值代表以哪个应用的名义发送消息
# msg_body – BodyBase 消息体
# touser_list – 员工id列表
# toparty_list – 部门id列表

@app.route('/send',methods=["POST"])
def send_touser_list():

    touser_list = request.form.get("touser_list","")
    body = request.form.get("body","")

    client = AppKeyClient(const.corpId, const.appkey, const.appsecret)  # 新 access_token 获取方式

    try:
        bumen = client.department.list()
        user = client.user.simple_list(bumen[0].get("id"))
        msg_body = time.time()
        toparty_list = str(bumen[0].get("id"))
        if not touser_list: raise "not touser_list"
        try:
            s = client.message.send(agentid=const.appid,msg_body=TextBody(body),touser_list=[touser_list])
        except Exception as e:
            pass
            # print("发送失败:",str(e))

        # print("发送完成")


    except Exception as e:
        pass
        # print("发送失败:",str(e))

    return jsonify({"msg":"send finish!"})



# @app.route("/tttime")
# def tttime():
#     url = "http://47.107.107.157:5000/send"
#     week = (datetime.datetime.now().strftime('%u'))
#     hours = (datetime.datetime.now().strftime('%H'))
#     r = ''
#     if(week in ["1","2","3","4","5"]):
#         if(hours in ["9","10","11","12","14","15","16","17","18"]):
#             r = requests.post(url,data={"touser_list":"1542764993017830","body":"哈哈哈哈"+str(time.time())})
#             print(r)
#     # print(week in ["1","2","3","4","5"])
#     # print(hours in ["9","10","11","12","14","15","16","17","18"])
#     return week+"-"+hours+str(r)


if __name__ == '__main__':
    manager.run()

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 anaf@163.com
目录