欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-24 13:25:30
背景 偶然发现一个python实现的按照农历/阴历推送消息提醒的程序,钉钉群消息推送。此处总结并对其可推送的消息做。 dingtalknotice 环境:python...

背景

偶然发现一个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的封装见参考链接:,项目源码:源码

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?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 + '】的生日????\n再过' + str(differ_day) + '天就到了~\n', is_at_all=true)   # text消息@所有人
    print(time.strftime("%y-%m-%d") + name + '的生日提前提醒发送完毕~\n')
  elif differ_day==0 :
    name = bri_name
    xiaoding.send_text(msg='今天是【' + name + '】的生日????\n祝寿星生日快乐!\n', is_at_all=true)   # text消息@所有人
    print(time.strftime("%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)

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