由于办公需要“每天定时推送某消息用来提醒群里面所有人”,有同事提议用企业微信自带的机器人来实现此功能。我觉得企业微信的这个工具还不错,具体使用方法我来一一讲述。
企业微信API
具体见官网说明:
企业微信机器人开发思路
想到几种方式:
直接写个sh脚本,并用linux定时器执行此脚本就可以了。这种方式简单实用,不过缺点就是修改起来稍微麻烦一点
写个Qt/VS客户端程序,做好页面和每天想推送的内容,还是有点麻烦
直接写个后台程序,指定时间推消息吧,稍微快些(用Python更快)
Python开发企业微信机器人每天定时发消息最终效果
Python开发企业微信机器人每天定时发消息实例源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#! -*- coding: utf-8 -*- """ Author: ZhenYuSha Create type_time: 2020-2-24 Info: 定期向企业微信推送消息 """ import requests, json import datetime import time send_message = "测试:测试机器人1号………………………………!" def get_current_time(): """获取当前时间,当前时分秒""" now_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' ) hour = datetime.datetime.now().strftime( "%H" ) mm = datetime.datetime.now().strftime( "%M" ) ss = datetime.datetime.now().strftime( "%S" ) return now_time, hour, mm, ss def sleep_time(hour, m, sec): """返回总共秒数""" return hour * 3600 + m * 60 + sec def send_msg(content): """艾特全部,并发送指定信息""" data = json.dumps({ "msgtype" : "text" , "text" : { "content" : content, "mentioned_list" :[ "@all" ]}}) r = requests.post(wx_url, data, auth = ( 'Content-Type' , 'application/json' )) print (r.json) def every_time_send_msg(interval_h = 0 , interval_m = 1 , interval_s = 0 , special_h = "00" , special_m = "00" , mode = "special" ): """每天指定时间发送指定消息""" # 设置自动执行间隔时间 second = sleep_time(interval_h, interval_m, interval_s) # 死循环 while 1 = = 1 : # 获取当前时间和当前时分秒 c_now, c_h, c_m, c_s = get_current_time() print ( "当前时间:" , c_now, c_h, c_m, c_s) if mode = = "special" : if c_h = = special_h and c_m = = special_m: # 执行 print ( "正在发送..." ) send_msg(send_message) else : send_msg(send_message) print ( "每隔" + str (interval_h) + "小时" + str (interval_m) + "分" + str (interval_s) + "秒执行一次" ) # 延时 time.sleep(second) if __name__ = = '__main__' : every_time_send_msg(mode = "no" ) |
最好是一个人的群,方便测试
创建好后,添加一个群机器人
给机器人起名字、添加头像
创建好后,复制Webhook地址后,点完成
配置程序到supervisor中启动
具体ini配置:
1
2
3
4
5
6
7
8
9
10
11
12
|
[program:Demo_QY_WX] directory = / root / software / python_Demo / Demo / Demo_QY_WX / command = python3 - u Demo_QY_WX.py autostart = true autorestart = true startsecs = 5 user = root redirect_stderr = true stdout_logfile = / data / logs / supervisord / Demo_QY_WX.log [group:Demo] programs = Demo_QY_WX ;server,progname2 each refers to 'x' in [program:x] definitions priority = 999 ; the relative start priority (default 999 ) |
update一下OK
完成以上步骤后,你可以在企业微信上每天指定时间推送消息,也可以间隔时间推送消息咯。