钉钉值班机器人发送

import datetime
import time
import json
import urllib.request
import pandas as pd



def get_Copywriting():
    # 判断当天是周几选择出文案的函数
    #获取当天日期
    today = datetime.date.today()
    #获取当天是周几
    todayweek = datetime.date.isoweekday(today)
    #利用IF语句判断周几选出当天要发送的文案
    if todayweek == 1:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**~感谢所有默默搬砖的你们,今天还是要以正能量的姿态迎接更大的挑战,加油,小伙伴们\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/133003hktlmylm71mtfk9o.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办]"
    elif todayweek == 2:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**,今日又是富有挑战的一天~\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/1330036np2rovyhpbjf4on.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办]"
    elif todayweek == 3:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**,愿你努力后的今天更精彩。早安!\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/133004t4oepiqsxps6ixt3.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办]"
    elif todayweek == 4:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**~不抛弃不放弃,没有办法的时候,死磕也是种办法。\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/133005zff8ctaxr979y090.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办]"
    elif todayweek == 5:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**,恩,现在可以笑出来了。又是新的一天,加油。\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/133007w8l029ak8gyy7l9n.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办] "
    elif todayweek == 6:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**,成功已越来越近啦~\n\n > ![screenshot](https://attach.bbs.miui.com/forum/201209/17/13300667pi6jztj2g6dmd2.jpg.thumb.jpg)\n  > ##### 08点00分发布 [党政办]"
    elif todayweek == 7:
        Copywriting = "### 每日值班  \n > 通知,今日值班人员为**%s**,各位早安,愿好。\n\n > ![screenshot](http://img.ivsky.com/img/bizhi/pre/201110/29/jianyue_mingyan-111.jpg)\n  > ##### 08点00分发布 [党政办] "
    return Copywriting


def send_request(url, datas):
    #传入url和内容发送请求
    # 构建一下请求头部
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    sendData = json.dumps(datas)  # 将字典类型数据转化为json格式
    sendDatas = sendData.encode("utf-8")  # python3的Request要求data为byte类型
    # 发送请求
    request = urllib.request.Request(url=url, data=sendDatas, headers=header)
    # 将请求发回的数据构建成为文件格式
    opener = urllib.request.urlopen(request)
    # 7、打印返回的结果
    print(opener.read())


def get_datas():
    today = datetime.date.today()
    #获取当天是周几
    todayweek = datetime.date.isoweekday(today)
    workbook = pd.read_excel('D:\\招财猫\\值班名单.xlsx',sheetname="记录数据")
    #读取表格中数据
    if todayweek == 1:
        datas=workbook["值班人员"][0]
    elif todayweek == 2:
        datas=workbook["值班人员"][1]
    elif todayweek == 3:
        datas=workbook["值班人员"][2]
    elif todayweek == 4:
        datas=workbook["值班人员"][3]
    elif todayweek == 5:
        datas=workbook["值班人员"][4]
    elif todayweek == 6:
        datas=workbook["值班人员"][5]
    elif todayweek == 7:
        datas=workbook["值班人员"][6]
    # 返回所需的数据
    return datas


def main():
    #按照钉钉给的数据格式设计请求内容  链接https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.p7hJKp&treeId=257&articleId=105735&docType=1
    my_data = {
        "msgtype": "markdown",
        "markdown": {"title": "每日值班人员通知",
                     "text": " "
                     },
        "at": {

            "isAtAll": True
        }
    }
    #获取当天文案
    my_Copywriting = get_Copywriting()
    #获取值班信息
    my_mydata = get_datas()
    my_Copywriting = my_Copywriting % my_mydata
    #把文案内容写入请求格式中
    my_data["markdown"]["text"] = my_Copywriting
    #你的钉钉机器人url
    my_url = "https://oapi.dingtalk.com/robot/send?access_token=e140d26ad802e5f1cee0e3fe4a7ae78baffde5cabc22b7dbb5d49fc1c78ebe05"
    send_request(my_url, my_data)


if __name__ == "__main__":
    main();

 

发布了14 篇原创文章 · 获赞 3 · 访问量 9973
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览