企业微信机器人脚本python_Python实现企业微信机器人每天定时发消息实例

#! -*- coding: utf-8 -*-

"""

Author: ZhenYuSha

Create type_time: 2020-2-24

Info: 定期向企业微信推送消息

"""

import requests, json

import datetime

import time

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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值