首页 > 编程 > Python > 正文

python3实现钉钉消息推送的方法示例

2020-01-04 13:37:00
字体:
来源:转载
供稿:网友

背景

偶然发现一个python实现的按照农历/阴历推送消息提醒的程序,钉钉消息推送。此处总结并对其可推送的消息做。

DingtalkNotice

环境:python3.7

安装:

pip install schedule #实现定时任务的模块pip install DingtalkChatbot #python封装的各种消息的调用pip install sxtwl #日历库

钉钉自定义机器人:

钉钉群机器人是一个高级扩展的功能,可以将第三方服务的信息聚合到钉钉群众,实现信息的自动化同步。1、通过聚合Github、Gitlab等源码管理服务,实现源码更新的同步;2、通过聚合Trello、JIRA等项目协调服务,实现项目信息同步;3、支持Webhook协议的自定义接入,可实现比如运维报警提醒、自动化测试结果报告提醒、工作与生活日程安排(上下班打卡、纪念日、生日)等等的提醒,均可通过自定义机器人聚合到钉钉中。目前自定义机器人支持文本(text)、链接(link)、markdown三种消息格式,五种消息类型。参考官方链接:钉钉自定义机器人,官方对各种消息的调用只提供了Java语言的封装,项目源码:源码

python 实现推送生日提醒的消息的源码地址:DingtalkNotice

One2TwoDigit.py

import timedef One2TwoDigit(a):  a= int(a)  if a<10:    a = '0'+str(a)  else:    a=a  return str(a)  def addYear(monthDay):  monthDay = (time.strftime("%Y")) + str(monthDay)  return monthDay

differ_days.py

#coding:utf8 import datetime def date_part(date='20170301'):  global year,month,day  year=date[0:4]  month_first=int(date[4:5])  month = date[5:6]  if month_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)  return d

birthday_notice.py

# -*- coding: UTF-8 -*-'''pip install DingtalkChatbotpip install sxtwl''' from dingtalkchatbot.chatbot import DingtalkChatbotimport timeimport sxtwllunar = sxtwl.Lunar() from One2TwoDigit import One2TwoDigit,addYearfrom differ_days import date_partimport datetime # 初始化机器人小丁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"]  def birthdayNotice_job(bri_name,bri_mon,bri_day,futureDays=3):  print("birthdayNotice_job is working...")  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    if 0<differ_day<=futureDays:    name = bri_name    xiaoding.send_text(msg= yangliDayMsg + '是【' + name + '】的生日
注:相关教程知识阅读请移步到python教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表