python3实现钉钉消息推送的方法示例
背景
偶然发现一个python实现的按照农历/阴历推送消息提醒的程序,钉钉群消息推送。此处总结并对其可推送的消息做。
DingtalkNotice
环境:python3.7
安装:
pipinstallschedule#实现定时任务的模块 pipinstallDingtalkChatbot#python封装的各种消息的调用 pipinstallsxtwl#日历库
钉钉自定义机器人:
钉钉群机器人是一个高级扩展的功能,可以将第三方服务的信息聚合到钉钉群众,实现信息的自动化同步。1、通过聚合Github、Gitlab等源码管理服务,实现源码更新的同步;2、通过聚合Trello、JIRA等项目协调服务,实现项目信息同步;3、支持Webhook协议的自定义接入,可实现比如运维报警提醒、自动化测试结果报告提醒、工作与生活日程安排(上下班打卡、纪念日、生日)等等的提醒,均可通过自定义机器人聚合到钉钉中。目前自定义机器人支持文本(text)、链接(link)、markdown三种消息格式,五种消息类型。参考官方链接:钉钉自定义机器人,官方对各种消息的调用只提供了Java语言的封装,Python的封装见参考链接:https://www.nhooo.com/article/156661.htm,项目源码:源码
python实现推送生日提醒的消息的源码地址:DingtalkNotice
One2TwoDigit.py
importtime defOne2TwoDigit(a): a=int(a) ifa<10: a='0'+str(a) else: a=a returnstr(a) defaddYear(monthDay): monthDay=(time.strftime("%Y"))+str(monthDay) returnmonthDay
differ_days.py
#coding:utf8 importdatetime defdate_part(date='20170301'): globalyear,month,day year=date[0:4] month_first=int(date[4:5]) month=date[5:6] ifmonth_first==0: month=date[5:6] else: month=date[4:6] day=date[6:8] year=int(year) month=int(month) day=int(day) d=datetime.date(year,month,day) returnd
birthday_notice.py
#-*-coding:UTF-8-*- ''' pipinstallDingtalkChatbot pipinstallsxtwl ''' fromdingtalkchatbot.chatbotimportDingtalkChatbot importtime importsxtwl lunar=sxtwl.Lunar() fromOne2TwoDigitimportOne2TwoDigit,addYear fromdiffer_daysimportdate_part importdatetime #初始化机器人小丁 webhook='https://oapi.dingtalk.com/robot/send?access_token='#填写你自己创建的机器人 xiaoding=DingtalkChatbot(webhook) ymc=["11","12","01","02","03","04","05","06","07","08","09","10"] rmc=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] defbirthdayNotice_job(bri_name,bri_mon,bri_day,futureDays=3): print("birthdayNotice_jobisworking...") dayYinli2Yangli=lunar.getDayByLunar(int(time.strftime("%Y")),bri_mon,bri_day,False)#查询阴历2018年10月20日的信息,最后一个False表示是否是润月,填True的时候只有当年有润月的时候才生效 yangliDay=(str(dayYinli2Yangli.y)+One2TwoDigit(str(dayYinli2Yangli.m))+One2TwoDigit(str(dayYinli2Yangli.d))) yangliDayMsg='农历:'+(str(bri_mon)+'月'+(str(bri_day))+'日') print(bri_name+'阳历生日是:'+yangliDay) d2=date_part(yangliDay) d1=date_part(date=datetime.datetime.now().strftime('%Y%m%d')) differ_day=(d2-d1).days if0