Skip to content

Commit

Permalink
✨ feat: Add WeCom bot push support
Browse files Browse the repository at this point in the history
  • Loading branch information
qiu121 committed Dec 28, 2024
1 parent 2f82d8a commit eda0d69
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/bing_wallpaper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ jobs:
FEISHU_SIGNING_KEY: ${{ secrets.FEISHU_SIGNING_KEY }} # 签名密钥

DINGTALK_WEBHOOK_URL: ${{ secrets.DINGTALK_WEBHOOK_URL }}
DINGTALK_SIGNING_KEY: ${{ secrets.DINGTALK_SIGNING_KEY }}
DINGTALK_SIGNING_KEY: ${{ secrets.DINGTALK_SIGNING_KEY }}

WECOME_WEBHOOK_URL: ${{ secrets.WECOME_WEBHOOK_URL }}
2 changes: 2 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
DINGTALK_WEBHOOK_URL,
DINGTALK_SIGNING_KEY,

WECOME_WEBHOOK_URL,

BING_URL,
BING_API,
)
2 changes: 2 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
DINGTALK_WEBHOOK_URL = os.getenv('DINGTALK_WEBHOOK_URL', '')
DINGTALK_SIGNING_KEY = os.getenv('DINGTALK_SIGNING_KEY', '')

WECOME_WEBHOOK_URL = os.getenv('WECOME_WEBHOOK_URL', '')

BING_URL = 'https://bing.com'
BING_API = f'{BING_URL}/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from wallpaper import get_bing_wallpaper
from feishu import get_feishu_token, send_to_feishu
from dingtalk import send_to_dingtalk
from wecom import send_to_wecome


def main():
Expand All @@ -20,6 +21,8 @@ def main():
send_to_feishu(wallpaper_url, wallpaper_description, wallpaper_title, access_token)
send_to_dingtalk(wallpaper_url, wallpaper_title, wallpaper_description)

send_to_wecome(wallpaper_url, wallpaper_title, wallpaper_description)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions wecom/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .wecom_api import (
send_to_wecome
)
39 changes: 39 additions & 0 deletions wecom/wecom_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import requests

from config import WECOME_WEBHOOK_URL


def send_to_wecome(image_url: str, title: str, description: str) -> dict:
headers = {"Content-Type": "application/json"}
# 企业微信的Markdown渲染不支持图片的嵌入;
# 通过news消息类型生成卡片消息时,图片默认显示在标题下方,描述在最下方,这种消息格式是固定的!!!
# https://developer.work.weixin.qq.com/document/path/91770#%E5%9B%BE%E6%96%87%E7%B1%BB%E5%9E%8B
content = {
"msgtype": "news",
"news": {
"articles": [{
"title": f'🔖 {description}',
# "title": f'🔖 {title}',
# "description": f'📝 {description}',
"url": image_url,
"picurl": image_url,
}]
}
}
# content = {
# "msgtype": "markdown",
# "markdown": {
# "content": f"🔖 **{title}**\n\n"
# f"![BingWallpaper]({image_url})\n\n"
# f"> 📝 {description}"
# }
# }

response = requests.post(WECOME_WEBHOOK_URL, data=json.dumps(content), headers=headers)

print(f'Request URL: {response.request.url}')
print(f'Request Method: {response.request.method}')
print(json.dumps(response.json(), ensure_ascii=False, indent=2))

return response.json()

0 comments on commit eda0d69

Please sign in to comment.