Skip to content

Commit

Permalink
Merge pull request #7 from nfe-w/feat/push-channel/wecom-bot
Browse files Browse the repository at this point in the history
Feat/push channel/wecom bot
  • Loading branch information
nfe-w authored May 6, 2024
2 parents c0882b9 + 2b5dbaa commit 72751d3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ docker run -d -v [配置文件的绝对路径]/config.yml:/mnt/config.yml nfew/a
|---------------|------------------|:-------:|-------------------------------------------------------------------------------------------------------------|
| Server酱_Turbo | serverChan_turbo || 🤖方便,不用安装app,免费用户5次/天,适合频率不高的用户<br/>👉https://sct.ftqq.com |
| 企业微信自建应用 | wecom_apps || 😢新用户不再推荐,2022年6月20日之后新创建的应用,需要配置可信IP<br/>👉https://work.weixin.qq.com/wework_admin/frame#apps/createApiApp |
| 企业微信群聊机器人 | wecom_bot || 🥳推荐,新建群聊添加自定义机器人即可<br/>👉https://developer.work.weixin.qq.com/document/path/99110 |
| 钉钉群聊机器人 | dingtalk_bot || 🥳推荐,新建群聊添加自定义机器人即可,自定义关键词使用"【"<br/>👉https://open.dingtalk.com/document/robots/custom-robot-access |
| 飞书自建应用 | feishu_apps || 🤔可以使用个人版,创建应用,授予其机器人权限<br/>👉https://open.feishu.cn/app?lang=zh-CN |
| 飞书群聊机器人 | feishu_bot | ❌(暂不支持) | 🤩推荐,新建群聊添加自定义机器人即可,自定义关键词使用"【"<br/>👉https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot |
Expand Down
8 changes: 6 additions & 2 deletions common/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import requests
import urllib3
from fake_useragent import UserAgent

from common.logger import log
from common.proxy import my_proxy

# 关闭ssl警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

ua = UserAgent(os=["macos"], min_version=120.0)


Expand All @@ -19,7 +23,7 @@ def requests_get(url, module_name="未指定", headers=None, params=None, use_pr
headers.setdefault('user-agent', random_ua)
proxies = _get_proxy() if use_proxy else None
try:
response = requests.get(url, headers=headers, params=params, proxies=proxies, timeout=10)
response = requests.get(url, headers=headers, params=params, proxies=proxies, timeout=10, verify=False)
except Exception as e:
log.error(f"【{module_name}】:{e}", exc_info=True)
return None
Expand All @@ -34,7 +38,7 @@ def requests_post(url, module_name="未指定", headers=None, params=None, data=
headers.setdefault('user-agent', random_ua)
proxies = _get_proxy() if use_proxy else None
try:
response = requests.post(url, headers=headers, params=params, data=data, json=json, proxies=proxies, timeout=10)
response = requests.post(url, headers=headers, params=params, data=data, json=json, proxies=proxies, timeout=10, verify=False)
except Exception as e:
log.error(f"【{module_name}】:{e}", exc_info=True)
return None
Expand Down
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ push_channel:
agent_id:
# 应用Secret,如果启动该推送则必填
corp_secret:
- name: 推送通道_企业微信机器人
enable: false
type: wecom_bot
# 机器人key,如果启动该推送则必填
key:
- name: 推送通道_钉钉机器人
enable: false
type: dingtalk_bot
Expand Down
Binary file modified docs/image/aio-dynamic-push.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions push_channel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .telegram_bot import TelegramBot
from .webhook import Webhook
from .wecom_apps import WeComApps
from .wecom_bot import WeComBot

push_channel_dict = {}

Expand All @@ -20,6 +21,8 @@ def get_push_channel(config):
return ServerChanTurbo(config)
if channel_type == "wecom_apps":
return WeComApps(config)
if channel_type == "wecom_bot":
return WeComBot(config)
if channel_type == "dingtalk_bot":
return DingtalkBot(config)
if channel_type == "feishu_apps":
Expand Down
41 changes: 41 additions & 0 deletions push_channel/wecom_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json

from common import util
from common.logger import log
from . import PushChannel


class WeComBot(PushChannel):
def __init__(self, config):
super().__init__(config)
self.key = str(config.get("key", ""))
if self.key == "":
log.error(f"【推送_{self.name}】配置不完整,推送功能将无法正常使用")

def push(self, title, content, jump_url=None, pic_url=None):
push_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
headers = {
"Content-Type": "application/json"
}
params = {
"key": self.key
}
body = {
"msgtype": "news",
"news": {
"articles": [
{
"title": title,
"description": content,
"url": jump_url,
}
]
}
}

if pic_url is not None:
body["news"]["articles"][0]["picurl"] = pic_url

response = util.requests_post(push_url, self.name, headers=headers, params=params, data=json.dumps(body))
push_result = "成功" if util.check_response_is_ok(response) else "失败"
log.info(f"【推送_{self.name}{push_result}")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PyYAML==6.0.1
requests==2.31.0
requests_toolbelt==1.0.0
schedule==1.2.1
urllib3==1.26.9

0 comments on commit 72751d3

Please sign in to comment.