Python开发 之 企业微信机器人每天定时发消息实例
2022-10-24 15:45:48
236
{{single.collect_count}}

1、背景

由于办公需要“每天定时推送某消息用来提醒群里面所有人”,有同事提议用企业微信自带的机器人来实现此功能。我觉得企业微信的这个工具还不错,具体使用方法我来一一讲述。

2、企业微信API

具体见官网说明:https://work.weixin.qq.com/help?person_id=1&doc_id=13376

3、想法

想到几种方式:

  • 直接写个sh脚本,并用linux定时器执行此脚本就可以了。这种方式简单实用,不过缺点就是修改起来稍微麻烦一点
  • 写个Qt/VS客户端程序,做好页面和每天想推送的内容,还是有点麻烦
  • 直接写个后台程序,指定时间推消息吧,稍微快些(用Python更快)

4、效果

在这里插入图片描述

5、源代码

#! -*- coding: utf-8 -*-"""Author: ZhenYuShaCreate type_time: 2020-2-24Info: 定期向企业微信推送消息"""import requests, jsonimport datetimeimport timewx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4baf3c3c-f3ea-4554-9a45-9fbbb2076269"# 测试机器人1号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, ssdef sleep_time(hour, m, sec):"""返回总共秒数"""return hour * 3600 + m * 60 + secdef 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")

6、Github源码分享

代码已上传至Github:https://github.com/ShaShiDiZhuanLan/Demo_QY_WX

7、具体步骤

7.1、创建一个群

最好是一个人的群,方便测试
在这里插入图片描述

7.2、创建好后,添加一个群机器人

在这里插入图片描述

7.3、给机器人起名字、添加头像

在这里插入图片描述

7.4、创建好后,复制Webhook地址后,点完成

在这里插入图片描述

7.5、配置程序到supervisor中启动

具体ini配置:

[program:Demo_QY_WX]directory = /root/software/python_Demo/Demo/Demo_QY_WX/command= python3 -u Demo_QY_WX.pyautostart = trueautorestart=truestartsecs = 5user =rootredirect_stderr = truestdout_logfile = /data/logs/supervisord/Demo_QY_WX.log[group:Demo]programs=Demo_QY_WX;server,progname2 each refers to 'x' in [program:x] definitionspriority=999; the relative start priority (default 999)

别忘了update一下哈。
使用supervisor的具体方法,在我这篇文章中有讲过:https://shazhenyu.blog.csdn.net/article/details/85158056

7.6、完成

over,你可以在企业微信上每天指定时间推送消息,也可以间隔时间推送消息咯。

8、说明

本文讲解的机器人接口,由于不停被骚扰,已经被我移除了,想要搞得自己配置一个机器人吧。

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}