├── img
├── 1.png
├── 2.png
├── 3.png
├── 4.png
└── 5.png
├── README.md
├── Demo_QY_WX.py
└── .gitignore
/img/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShaShiDiZhuanLan/Demo_QY_WX/HEAD/img/1.png
--------------------------------------------------------------------------------
/img/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShaShiDiZhuanLan/Demo_QY_WX/HEAD/img/2.png
--------------------------------------------------------------------------------
/img/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShaShiDiZhuanLan/Demo_QY_WX/HEAD/img/3.png
--------------------------------------------------------------------------------
/img/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShaShiDiZhuanLan/Demo_QY_WX/HEAD/img/4.png
--------------------------------------------------------------------------------
/img/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShaShiDiZhuanLan/Demo_QY_WX/HEAD/img/5.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 1、Python语言的应用 之 Demo_QY_WX
2 | 企业微信机器人,定时发消息实例:每天定时推送某消息 用来提醒群里面所有人
3 |
4 | # 2、更新信息
5 | 开发者:沙振宇(沙师弟专栏)
6 | 创建时间:2020-2-24
7 | CSDN博客名称:Python开发 之 企业微信机器人每天定时发消息实例
8 | CSDN博客地址:https://shazhenyu.blog.csdn.net/article/details/104480682
9 |
10 | # 3、效果图
11 | 
12 |
--------------------------------------------------------------------------------
/Demo_QY_WX.py:
--------------------------------------------------------------------------------
1 | #! -*- coding: utf-8 -*-
2 | """
3 | Author: ZhenYuSha
4 | Create type_time: 2020-2-24
5 | Info: 定期向企业微信推送消息
6 | """
7 | import requests, json
8 | import datetime
9 | import time
10 |
11 | wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4baf3c3c-f3ea-4554-9a45-9fbbb2076269" # 测试机器人1号
12 | send_message = "测试:测试机器人1号………………………………!"
13 |
14 |
15 | def get_current_time():
16 | """获取当前时间,当前时分秒"""
17 | now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
18 | hour = datetime.datetime.now().strftime("%H")
19 | mm = datetime.datetime.now().strftime("%M")
20 | ss = datetime.datetime.now().strftime("%S")
21 | return now_time, hour, mm, ss
22 |
23 |
24 | def sleep_time(hour, m, sec):
25 | """返回总共秒数"""
26 | return hour * 3600 + m * 60 + sec
27 |
28 |
29 | def send_msg(content):
30 | """艾特全部,并发送指定信息"""
31 | data = json.dumps({"msgtype": "text", "text": {"content": content, "mentioned_list":["@all"]}})
32 | r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
33 | print(r.json)
34 |
35 |
36 | def every_time_send_msg(interval_h=0, interval_m=1, interval_s=0, special_h="00", special_m="00", mode="special"):
37 | """每天指定时间发送指定消息"""
38 |
39 | # 设置自动执行间隔时间
40 | second = sleep_time(interval_h, interval_m, interval_s)
41 | # 死循环
42 | while 1 == 1:
43 | # 获取当前时间和当前时分秒
44 | c_now, c_h, c_m, c_s = get_current_time()
45 | print("当前时间:", c_now, c_h, c_m, c_s)
46 | if mode == "special":
47 | if c_h == special_h and c_m == special_m:
48 | # 执行
49 | print("正在发送...")
50 | send_msg(send_message)
51 | else:
52 | send_msg(send_message)
53 | print("每隔" + str(interval_h) + "小时" + str(interval_m) + "分" + str(interval_s) + "秒执行一次")
54 | # 延时
55 | time.sleep(second)
56 |
57 |
58 | if __name__ == '__main__':
59 | every_time_send_msg(mode="no")
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------