dingding.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import time
  2. import requests
  3. import hashlib
  4. import base64
  5. import urllib.parse
  6. import hmac
  7. import json
  8. def dingding_alert1(phone:list,msg):
  9. dingding_webhook="https://oapi.dingtalk.com/robot/send?access_token=9c78c711f14ba3345d6dc492dc5ca8118c421516d611b5de46854fb8e158565f"
  10. timestamp = str(round(time.time() * 1000))
  11. secret = 'SEC09f744f81b55c8a18f0d77a3ab60bf4e1ec3f16b85f025e6a4f75ddff00a8fd4'
  12. secret_enc = secret.encode('utf-8')
  13. string_to_sign = '{}\n{}'.format(timestamp, secret)
  14. string_to_sign_enc = string_to_sign.encode('utf-8')
  15. hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
  16. sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
  17. dingding_url = dingding_webhook + '&timestamp=' + timestamp + "&sign=" + sign
  18. if phone:
  19. header = {
  20. "Content-Type": "application/json"
  21. }
  22. data = {
  23. "at": {
  24. "atMobiles": phone,
  25. },
  26. "text": {
  27. "content": msg
  28. },
  29. "msgtype": "text"
  30. }
  31. else:
  32. header = {
  33. "Content-Type": "application/json"
  34. }
  35. data = {
  36. "at": {
  37. "isAtAll": True
  38. },
  39. "text": {
  40. "content": msg
  41. },
  42. "msgtype": "text"
  43. }
  44. res = requests.post(url=dingding_url, data=json.dumps(data), headers=header)
  45. def dingding_alert11(phone:list,msg):
  46. dingding_webhook="https://oapi.dingtalk.com/robot/send?access_token=c3be52d9ae7307dd7ceb0b677f569c47de2ff81b20fe30c328c75fbd9ab81383"
  47. timestamp = str(round(time.time() * 1000))
  48. secret = 'SEC14f583c6f735044be3f9f6fdc53edb7cf0b7d56d9b63c4b78bcafa9880814f9e'
  49. secret_enc = secret.encode('utf-8')
  50. string_to_sign = '{}\n{}'.format(timestamp, secret)
  51. string_to_sign_enc = string_to_sign.encode('utf-8')
  52. hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
  53. sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
  54. dingding_url = dingding_webhook + '&timestamp=' + timestamp + "&sign=" + sign
  55. if phone:
  56. header = {
  57. "Content-Type": "application/json"
  58. }
  59. data = {
  60. "at": {
  61. "atMobiles": phone,
  62. },
  63. "text": {
  64. "content": msg
  65. },
  66. "msgtype": "text"
  67. }
  68. else:
  69. header = {
  70. "Content-Type": "application/json"
  71. }
  72. data = {
  73. "at": {
  74. "isAtAll": True
  75. },
  76. "text": {
  77. "content": msg
  78. },
  79. "msgtype": "text"
  80. }
  81. res = requests.post(url=dingding_url, data=json.dumps(data), headers=header)