Skip to content

Commit

Permalink
Merge pull request #67 from xinmans/main
Browse files Browse the repository at this point in the history
add dingtalk alert run every 4 hours
  • Loading branch information
cooderl authored May 2, 2024
2 parents cac6b59 + 1573073 commit 64a877e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ docker run -d \

- `CRON_EXPRESSION` 定时更新订阅源Cron表达式,默认为 `35 5,17 * * *`

## 支持钉钉通知

进入wewe-rss-dingtalk目录按照README.md指引部署

## 使用方式

Expand Down
10 changes: 10 additions & 0 deletions wewe-rss-dingtalk/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.8.12-slim

WORKDIR /app
COPY . .

RUN pip install -r requirements.txt
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive

CMD python3 /app/main.py
19 changes: 19 additions & 0 deletions wewe-rss-dingtalk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

### 修改main.py,输入dingtalk的access_token和secret

```
access_token = ''
secret = '' # 创建机器人时钉钉设置页面有提供
```

### 修改根目录下的docker-compose.yaml文件,去掉以下字段的注释
```
# ports:
# - 13306:3306
```

### python3 main.py就可以运行
### 或者部署成docker,运行
```
sudo docker-compose up -d
```
6 changes: 6 additions & 0 deletions wewe-rss-dingtalk/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.9'

services:
wewe-rss-dingtalk:
build: .
container_name: wewe-rss-dingtalk
85 changes: 85 additions & 0 deletions wewe-rss-dingtalk/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import mysql.connector
import requests
import json
import os
import time
from datetime import datetime, timedelta
import pytz
from dingtalkchatbot.chatbot import DingtalkChatbot, ActionCard, FeedLink, CardItem

def get_subjects_json():
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
port="13306",
user="root",
password="123456",
database="wewe-rss"
)
# 查询符合条件的数据, 用created_at来判断,因为publish_time是发文时间,rss更新时间会滞后
mycursor = mydb.cursor()
query = """SELECT a.id, a.title, a.pic_url, a.publish_time, b.mp_name
FROM articles AS a, feeds AS b
WHERE a.mp_id = b.id
AND a.created_at >= NOW() - INTERVAL 12 HOUR
ORDER BY a.publish_time DESC"""
# 4hour +8 to fix created time is UTC time.
mycursor.execute(query)
results = mycursor.fetchall()

# 组装数据为JSON格式
data = []
for result in results:
subject = {
"id": result[0],
"title": result[1],
"pic_url": result[2],
"publish_time": result[3],
"mp_name": result[4]
}
data.append(subject)

json_data = json.dumps(data, indent=4)
print(json_data)
return json_data

def dingbot_markdown(access_token, secret, rss_list):
new_webhook = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}'
xiaoding = DingtalkChatbot(new_webhook, secret=secret, pc_slide=True, fail_notice=False)

text = []
for data in rss_list:
# 创建CardItem对象
mp_name = data['mp_name']
url = 'https://mp.weixin.qq.com/s/' + str(data["id"])
unix_timestamp = data['publish_time']
# 将 Unix 时间戳转换为北京时间
#转换成localtime
time_local = time.localtime(unix_timestamp)
#转换成新的时间格式(2016-05-05 20:28:54)
beijing_time = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
text_content = f'> **{mp_name}** [' + data["title"] + '](' + url + ') ' + str(beijing_time) + '\n'
# Markdown消息@指定用户
text.append(text_content)

title = '## 微信公众号<最近4小时更新> \n\n'
markdown_text = title + '\n'.join(text)
print(markdown_text)
res = xiaoding.send_markdown(title=title, text=markdown_text)
print(f"send sucess, res: {res}")


def send_dingtalk_msg(access_token, secret):
data = get_subjects_json()
rss_list = json.loads(data)
if len(rss_list) != 0:
dingbot_markdown(access_token, secret, rss_list)

if __name__ == '__main__':

access_token = ''
secret = '' # 创建机器人时钉钉设置页面有提供

while True:
send_dingtalk_msg(access_token, secret)
time.sleep( 4 * 60 * 60 ) # run every 4 hours
4 changes: 4 additions & 0 deletions wewe-rss-dingtalk/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DingtalkChatbot==1.5.3
mysql-connector-python
jason
pytz

0 comments on commit 64a877e

Please sign in to comment.