• python实现钉钉推送


    python推送消息到钉钉python钉钉消息推送python钉钉机器人

    1. import json
    2. import hashlib
    3. import base64
    4. import hmac
    5. import os
    6. import time
    7. import requests
    8. from urllib.parse import quote_plus
    9. class Messenger:
    10. def __init__(self, token=os.getenv("DD_ACCESS_TOKEN"), secret=os.getenv("DD_SECRET")):
    11. self.timestamp = str(round(time.time() * 1000))
    12. self.URL = "https://oapi.dingtalk.com/robot/send"
    13. self.headers = {'Content-Type': 'application/json'}
    14. secret = secret
    15. secret_enc = secret.encode('utf-8')
    16. string_to_sign = '{}\n{}'.format(self.timestamp, secret)
    17. string_to_sign_enc = string_to_sign.encode('utf-8')
    18. hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    19. self.sign = quote_plus(base64.b64encode(hmac_code))
    20. self.params = {'access_token': token, "sign": self.sign}
    21. def send_text(self, content):
    22. """
    23. 发送文本
    24. @param content: str, 文本内容
    25. """
    26. data = {"msgtype": "text", "text": {"content": content}}
    27. self.params["timestamp"] = self.timestamp
    28. return requests.post(
    29. url=self.URL,
    30. data=json.dumps(data),
    31. params=self.params,
    32. headers=self.headers
    33. )
    34. if __name__ == "__main__":
    35. m = Messenger(
    36. token="19985c90103a1cb38f2c9e8fxxxxxxxxxxxxxxxx你的token",
    37. secret="SEC602a8ce2713cb077deb3xxxxxxxxxxxxxxxx你的secret"
    38. )
    39. # print(m.send_text("电费不足"))
    40. # print(m.send_text("电费剩余"))

    推送文本2

    1. #发送文本
    2. from dingtalkchatbot.chatbot import DingtalkChatbot
    3. from datetime import datetime
    4. def dingtalk_robot(webhook,secret):
    5. dogBOSS = DingtalkChatbot(webhook, secret)
    6. url = 'http://kamo.fun'
    7. dogBOSS.send_text(
    8. msg=f'电费剩余',
    9. is_at_all=False)
    10. if __name__ == '__main__':
    11. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=19985c90103a1cb38f2c9e8f0f3d9e475xx'
    12. secrets = 'SEC602a8ce2713cb077deb398d8543xx'
    13. dingtalk_robot(webhook=webhook,
    14. secret=secrets)

    推送markdown

    1. #发送markdown格式
    2. from dingtalkchatbot.chatbot import DingtalkChatbot
    3. from datetime import datetime
    4. def dingtalk_robot(webhook,secret):
    5. dogBOSS = DingtalkChatbot(webhook, secret)
    6. red_msg = '级别:危险'
    7. orange_msg = '级别:警告'
    8. now_time = datetime.now().strftime('%Y.%m.%d %H:%M:%S')
    9. url = 'http://kamo.fun'
    10. dogBOSS.send_markdown(
    11. title=f'电费剩余',
    12. text=f'### **我是主内容的第一行**\n'
    13. f'**{red_msg}**\n\n'
    14. f'**{orange_msg}**\n\n'
    15. f'**发送时间:** {now_time}\n\n'
    16. f'**相关网址:**[点击跳转]({url}) \n',
    17. is_at_all=True)
    18. if __name__ == '__main__':
    19. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=19985c90103a1cb38f2c9xxx'
    20. secrets = 'SEC602a8ce2713cb077deb398d85432bxxx'
    21. dingtalk_robot(webhook=webhook,
    22. secret=secrets)

     

  • 相关阅读:
    NFT的下一个叙事:动态NFT
    【DevOps】Git 图文详解(四):Git 使用入门
    C语言中的宏定义和内联函数有什么区别?
    Django基础理论整理
    基于verilog的CRC校验(汇总)
    VEX —— Functions|Nodes
    企业应用架构研究系列二十六:信号量SemaphoreSlim与Semaphore
    Mybatis笔记一
    带头+双向+循环链表
    土耳其商务团一行莅临优积科技考察交流
  • 原文地址:https://blog.csdn.net/qq_28821897/article/details/133375775