feat: 初始化微信自动消息发送项目
添加项目基础文件结构,包括: - README.md 占位文件 - Python 版本配置文件 - gitignore 配置 - 项目配置文件 - 项目依赖配置 - 定时任务调度器 - 微信消息发送主逻辑 - 飞书消息通知功能
This commit is contained in:
commit
96e67e8b04
13
.gitignore
vendored
Normal file
13
.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
# Python-generated files
|
||||
__pycache__/
|
||||
*.py[oc]
|
||||
build/
|
||||
dist/
|
||||
wheels/
|
||||
*.egg-info
|
||||
|
||||
# Virtual environments
|
||||
.venv
|
||||
uv.lock
|
||||
.python-version
|
||||
*.log
|
||||
6
config.py
Normal file
6
config.py
Normal file
@ -0,0 +1,6 @@
|
||||
CONFIG = {
|
||||
"app_id": "cli_a8de57f7e95f100c",
|
||||
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp",
|
||||
"open_id": "ou_bd33a5f75dd2ccb005a21e4dd01f930c",
|
||||
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\"
|
||||
}
|
||||
15
pyproject.toml
Normal file
15
pyproject.toml
Normal file
@ -0,0 +1,15 @@
|
||||
[project]
|
||||
name = "wxmsg"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"wxauto",
|
||||
"lark-oapi",
|
||||
"schedule"
|
||||
]
|
||||
|
||||
[[tool.uv.index]]
|
||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
|
||||
default = true
|
||||
19
scheduler.py
Normal file
19
scheduler.py
Normal file
@ -0,0 +1,19 @@
|
||||
import schedule
|
||||
import time
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
def run_sendmsg():
|
||||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||||
subprocess.run([sys.executable, script_path])
|
||||
|
||||
# 设置每天12点运行
|
||||
schedule.every().day.at("12:00").do(run_sendmsg)
|
||||
|
||||
print("定时任务已启动,将在每天12:00运行sendmsg.py")
|
||||
|
||||
# 保持程序运行
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
37
send_filemsg.py
Normal file
37
send_filemsg.py
Normal file
@ -0,0 +1,37 @@
|
||||
import json
|
||||
|
||||
import lark_oapi as lark
|
||||
from lark_oapi.api.im.v1 import *
|
||||
from config import CONFIG
|
||||
|
||||
def main():
|
||||
client = lark.Client.builder() \
|
||||
.app_id(CONFIG['app_id']) \
|
||||
.app_secret(CONFIG['app_secret']) \
|
||||
.log_level(lark.LogLevel.DEBUG) \
|
||||
.build()
|
||||
|
||||
# 构造请求对象
|
||||
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
||||
.receive_id_type("open_id") \
|
||||
.request_body(CreateMessageRequestBody.builder()
|
||||
.receive_id(CONFIG['open_id'])
|
||||
.msg_type("text")
|
||||
.content("{\"text\":\"未找到指定图片\"}")
|
||||
.build()) \
|
||||
.build()
|
||||
|
||||
# 发起请求
|
||||
response: CreateMessageResponse = client.im.v1.message.create(request)
|
||||
|
||||
# 处理失败返回
|
||||
if not response.success():
|
||||
lark.logger.error(
|
||||
f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}")
|
||||
return
|
||||
|
||||
# 处理业务结果
|
||||
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
38
send_openmsg.py
Normal file
38
send_openmsg.py
Normal file
@ -0,0 +1,38 @@
|
||||
import json
|
||||
|
||||
import lark_oapi as lark
|
||||
from lark_oapi.api.im.v1 import *
|
||||
from config import CONFIG
|
||||
|
||||
def main():
|
||||
# 创建client
|
||||
client = lark.Client.builder() \
|
||||
.app_id(CONFIG['app_id']) \
|
||||
.app_secret(CONFIG['app_secret']) \
|
||||
.log_level(lark.LogLevel.DEBUG) \
|
||||
.build()
|
||||
|
||||
# 构造请求对象
|
||||
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
||||
.receive_id_type("open_id") \
|
||||
.request_body(CreateMessageRequestBody.builder()
|
||||
.receive_id(CONFIG['open_id'])
|
||||
.msg_type("text")
|
||||
.content("{\"text\":\"请打开微信或者登录微信\"}")
|
||||
.build()) \
|
||||
.build()
|
||||
|
||||
# 发起请求
|
||||
response: CreateMessageResponse = client.im.v1.message.create(request)
|
||||
|
||||
# 处理失败返回
|
||||
if not response.success():
|
||||
lark.logger.error(
|
||||
f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}")
|
||||
return
|
||||
|
||||
# 处理业务结果
|
||||
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
36
sendmsg.py
Normal file
36
sendmsg.py
Normal file
@ -0,0 +1,36 @@
|
||||
from wxauto import WeChat
|
||||
from datetime import date
|
||||
import sys
|
||||
import os
|
||||
import subprocess
|
||||
import json
|
||||
from config import CONFIG
|
||||
import time
|
||||
|
||||
|
||||
today = date.today()
|
||||
formatted_date = today.strftime('%Y-%m-%d')
|
||||
file_name = formatted_date+".png"
|
||||
file_path = CONFIG['file_path']+file_name
|
||||
try:
|
||||
wx = WeChat()
|
||||
except:
|
||||
#运行send_openmsg.py
|
||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
msg = 'hello, wxauto!'
|
||||
who = 'File Transfer'
|
||||
|
||||
if os.path.isfile(file_path):
|
||||
error_msg = ""
|
||||
else:
|
||||
error_msg = "没找到指定文件"
|
||||
#运行send_filemsg.py
|
||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||
sys.exit(1)
|
||||
|
||||
print("找到了指定文件!")
|
||||
wx.SendFiles(filepath=file_path, who="File Transfer")
|
||||
wx.SendMsg(msg=msg, who=who)
|
||||
Loading…
x
Reference in New Issue
Block a user