当前位置:首页 >> 脚本专栏

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

背景

偶然发现一个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的封装见参考链接:https://www.jb51.net/article/156661.htm,项目源码:源码

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

One2TwoDigit.py

import time
def 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 DingtalkChatbot
pip install sxtwl
'''
 
from dingtalkchatbot.chatbot import DingtalkChatbot
import time
import sxtwl
lunar = sxtwl.Lunar() 
from One2TwoDigit import One2TwoDigit,addYear
from differ_days import date_part
import datetime
 
# 初始化机器人小丁
webhook = 'https://oapi.dingtalk.com/robot/send"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 + '】的生日"%Y-%m-%d") + name + '的生日提前提醒发送完毕~\n')
  elif differ_day==0 :
    name = bri_name
    xiaoding.send_text(msg='今天是【' + name + '】的生日"%Y-%m-%d") + name + '的当天生日提醒发送完毕~\n')

run.py

# -*- coding: utf-8 -*-
 
from birthday_notice import birthdayNotice_job
import schedule
import time
def run():
  print("定时任务开始...")
  f_douhao = open(r"data.csv","r")
  line_douhao = f_douhao.readlines()
  for i in range(6):
    bri_name = (line_douhao[i].split(";")[0])
    bri_mon = (line_douhao[i].split(";")[1])
    bri_day = (line_douhao[i].split(";")[2])
    birthdayNotice_job(bri_name,int(bri_mon),int(bri_day),futureDays=5)
  f_douhao.close()
 
schedule.every().day.at("16:49").do(run)
while True:
  schedule.run_pending()
  time.sleep(1)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。