- 语言:Python
- 使用库:dingtalkchatbot configparser
1、工作中的具体需求(解决什么问题)
(1)每周一提醒业务组同事创建日报wiki
(2)每周三提醒开发owner查看bugly问题
(3)每周五提醒学习小组owner组织分享
…等等诸如此类的需求
2、整体思路(需要做什么)
(1)发钉钉通知、确定通知内容、通知谁
(2)发送时间
3、有哪些解决方法
(1)钉钉通知
钉钉公开API
dingtalkchatbot(基于官方AP二次I封装)
(2)定时任务
python 定时任务(sleep / Timer / schedule / APScheduler)
jenkisn 日程表
最后的选择:
- dingtalkchatbot + jenkisn定时任务
4、具体实现
(1)钉钉通知
- 添加webhook
- 添加固定通知内容
from dingtalkchatbot.chatbot import DingtalkChatbot
from configparser import ConfigParser
class weekly():
def weeklyAlert(self, webhook, phone):
webhook = webhook
dingding = DingtalkChatbot(webhook)
# 发送 link消息
dingding.send_link(title='Python小助手', text='点击这里,查看本周的Python分享', message_url='https://wiki.lalal.ccc')
# 发送 Text消息@某人
at_mobiles = [phone]
dingding.send_text(msg='本周Python小助手', at_mobiles=at_mobiles)
print("phone : " + str(phone))
print(str("提醒成功"))
def read_config(self, cfg_file):
cfg = ConfigParser()
cfg.read(cfg_file)
return cfg
weekly = weekly()
cfg = weekly.read_config("/Users/test/Documents/weekly_base/weekphone.ini")
count = cfg.get('weekly', 'count')
webhook_week = 'https://oapi.dingtalk.com/robot/send?access_token=xxx'
if count == '0':
phone = '189xxx' # nico
weekly.weeklyAlert(webhook_week, phone)
cfg.set('weekly', 'count', '1')
cfg.write(open("/Users/test/Documents/weekly_base/weekphone.ini", "w"))
else :
phone = '158xxx' # budy
weekly.weeklyAlert(webhook_week, phone)
cfg.set('weekly', 'count', '0')
cfg.write(open("/Users/test/Documents/weekly_base/weekphone.ini", "w"))
(2)更新通知人
- 通过config替换
[weekly]
count = 3
(2)定时提醒
- jenkins 日程表
- H 9 1-7,15-21 * 1