Demo_QY_WX.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Author: ZhenYuSha
  5. Create type_time: 2020-2-24
  6. Info: 定期向企业微信推送消息
  7. """
  8. import requests
  9. import json
  10. import datetime
  11. import time
  12. wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4ebd03c3-3418-40a4-998d-8c84268814da" # 测试机器人1号
  13. send_message = "测试:测试机器人1号………………………………!"
  14. def get_current_time():
  15. """获取当前时间,当前时分秒"""
  16. now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  17. hour = datetime.datetime.now().strftime("%H")
  18. mm = datetime.datetime.now().strftime("%M")
  19. ss = datetime.datetime.now().strftime("%S")
  20. return now_time, hour, mm, ss
  21. def sleep_time(hour, m, sec):
  22. """返回总共秒数"""
  23. return hour * 3600 + m * 60 + sec
  24. def send_msg(content):
  25. """艾特全部,并发送指定信息"""
  26. data = json.dumps({"msgtype": "text", "text": {
  27. "content": content, "mentioned_list": ["@all"]}})
  28. r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
  29. print(r.json)
  30. def every_time_send_msg(interval_h=0, interval_m=1, interval_s=0, special_h="00", special_m="00", mode="special"):
  31. """每天指定时间发送指定消息"""
  32. # 设置自动执行间隔时间
  33. second = sleep_time(interval_h, interval_m, interval_s)
  34. # 死循环
  35. while 1 == 1:
  36. # 获取当前时间和当前时分秒
  37. c_now, c_h, c_m, c_s = get_current_time()
  38. print("当前时间:", c_now, c_h, c_m, c_s)
  39. if mode == "special":
  40. if c_h == special_h and c_m == special_m:
  41. # 执行
  42. print("正在发送...")
  43. send_msg(send_message)
  44. else:
  45. send_msg(send_message)
  46. print("每隔" + str(interval_h) + "小时" +
  47. str(interval_m) + "分" + str(interval_s) + "秒执行一次")
  48. # 延时
  49. time.sleep(second)
  50. if __name__ == '__main__':
  51. every_time_send_msg(mode="no")
  52. # import requests
  53. # url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4ebd03c3-3418-40a4-998d-8c84268814da"
  54. # headers = {
  55. # "Content-Type": "application/json"
  56. # }
  57. # data = {
  58. # "msgtype": "text",
  59. # "text": {
  60. # "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
  61. # "mentioned_list": ["wangqing", "@all"],
  62. # "mentioned_mobile_list": ["13800001111", "@all"]
  63. # }
  64. # }
  65. # response = requests.post(url, json=data, headers=headers)
  66. # print(response.status_code)
  67. # print(response.text)
  68. # import requests
  69. # import schedule
  70. # import time
  71. # def send_message(content):
  72. # url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4ebd03c3-3418-40a4-998d-8c84268814da"
  73. # headers = {
  74. # "Content-Type": "application/json"
  75. # }
  76. # data = {
  77. # "msgtype": "text",
  78. # "text": {
  79. # "content": content
  80. # }
  81. # }
  82. # response = requests.post(url, json=data, headers=headers)
  83. # print(response.status_code, response.text)
  84. # def send_weather_message():
  85. # send_message("重庆今日天气:29度,大部分多云,降雨概率:60%")
  86. # def send_ad_message():
  87. # send_message("宝宝喝AD哦")
  88. # # 定义定时任务
  89. # schedule.every().day.at("08:10").do(send_weather_message)
  90. # schedule.every().day.at("10:00").do(send_ad_message)
  91. # # 保持脚本运行
  92. # while True:
  93. # schedule.run_pending()
  94. # time.sleep(1)