ddrobot.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import time
  2. import hmac
  3. import hashlib
  4. import base64
  5. import urllib.parse
  6. import requests,json #导入依赖库
  7. def test():
  8. timestamp = str(round(time.time() * 1000))
  9. secret = 'SEC2f90ac798a4a94780ce52281047c10362b20bc6c6f27181cf7ae5ef548525f41'
  10. secret_enc = secret.encode('utf-8')
  11. string_to_sign = '{}\n{}'.format(timestamp, secret)
  12. string_to_sign_enc = string_to_sign.encode('utf-8')
  13. hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
  14. sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
  15. # print(timestamp)
  16. #print(sign)
  17. headers={'Content-Type': 'application/json'} #定义数据类型
  18. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=8020df878c6f6508f076c5dc291960f798b982aca1b0aa4e4bee9facec8321ba&timestamp='+timestamp+"&sign="+sign
  19. #定义要发送的数据
  20. #"at": {"atMobiles": "['"+ mobile + "']"
  21. data = {
  22. "msgtype": "text",
  23. "text": {"content": '都谁没加到群里来?小心升不了班'},
  24. "at": {
  25. "atMobiles": [
  26. "13032201605"
  27. ],
  28. "isAtAll":False
  29. }
  30. }
  31. data2 = {
  32. "feedCard": {
  33. "links": [
  34. {
  35. "title": "argparse库",
  36. "messageURL": "https://blog.csdn.net/codename_cys/article/details/107505718",
  37. "picURL": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRBba_xRVN7Di9Io6_1Oc54lFzcyA7FzDXEtQ&usqp=CAU"
  38. },
  39. {
  40. "title": "pydantic库简介",
  41. "messageURL": "https://blog.csdn.net/codename_cys/article/details/107675748",
  42. "picURL": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRBba_xRVN7Di9Io6_1Oc54lFzcyA7FzDXEtQ&usqp=CAU"
  43. }
  44. ]
  45. },
  46. "msgtype": "feedCard"
  47. }
  48. data3 = {
  49. "msgtype": "markdown",
  50. "markdown": {
  51. "title":"杭州天气",
  52. "text": "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n"
  53. },
  54. "at": {
  55. "atMobiles": [
  56. "13032201605"
  57. ],
  58. "isAtAll": False
  59. }
  60. }
  61. res = requests.post(webhook, data=json.dumps(data3), headers=headers) #发送post请求
  62. print(res.text)
  63. def sendMsg(msg, atuser):
  64. timestamp = str(round(time.time() * 1000))
  65. secret = 'SEC2f90ac798a4a94780ce52281047c10362b20bc6c6f27181cf7ae5ef548525f41'
  66. secret_enc = secret.encode('utf-8')
  67. string_to_sign = '{}\n{}'.format(timestamp, secret)
  68. string_to_sign_enc = string_to_sign.encode('utf-8')
  69. hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
  70. sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
  71. # print(timestamp)
  72. # print(sign)
  73. headers = {'Content-Type': 'application/json'} # 定义数据类型
  74. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=8020df878c6f6508f076c5dc291960f798b982aca1b0aa4e4bee9facec8321ba&timestamp=' + timestamp + "&sign=" + sign
  75. atTittle = ""
  76. for n in atuser:
  77. atTittle += "@{}".format(n)
  78. data = {
  79. "msgtype": "markdown",
  80. "markdown": {
  81. "title": "通知",
  82. "text": "{}{}".format(atTittle, msg)
  83. },
  84. "at": {
  85. "atMobiles": atuser,
  86. "isAtAll": False
  87. }
  88. }
  89. res = requests.post(webhook, data=json.dumps(data), headers=headers) # 发送post请求
  90. print(res.text)
  91. if __name__ == '__main__':
  92. MSG = """
  93. #### 机号:{}
  94. ##### 状态:{}
  95. ##### 时间:{}
  96. ##### 内容:{}
  97. ###### -------------
  98. """.format('B-6419','未完成', '22点31分', 'xxxxxxxxxxx')
  99. nmsg = ""
  100. for i in range(10):
  101. nmsg+=MSG
  102. sendMsg(nmsg, ['13032201605'])