python实现钉钉推送

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

python 复制代码
import json
import hashlib
import base64
import hmac
import os
import time
import requests
from urllib.parse import quote_plus


class Messenger:
    def __init__(self, token=os.getenv("DD_ACCESS_TOKEN"), secret=os.getenv("DD_SECRET")):
        self.timestamp = str(round(time.time() * 1000))
        self.URL = "https://oapi.dingtalk.com/robot/send"
        self.headers = {'Content-Type': 'application/json'}
        secret = secret
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(self.timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        self.sign = quote_plus(base64.b64encode(hmac_code))
        self.params = {'access_token': token, "sign": self.sign}

    def send_text(self, content):
        """
        发送文本
        @param content: str, 文本内容
        """
        data = {"msgtype": "text", "text": {"content": content}}
        self.params["timestamp"] = self.timestamp
        return requests.post(
            url=self.URL,
            data=json.dumps(data),
            params=self.params,
            headers=self.headers
        )
if __name__ == "__main__":
    m = Messenger(
        token="19985c90103a1cb38f2c9e8fxxxxxxxxxxxxxxxx你的token",
        secret="SEC602a8ce2713cb077deb3xxxxxxxxxxxxxxxx你的secret"
    )
    # print(m.send_text("电费不足"))
    # print(m.send_text("电费剩余"))

推送文本2

python 复制代码
#发送文本
from dingtalkchatbot.chatbot import DingtalkChatbot
from datetime import  datetime
def dingtalk_robot(webhook,secret):
    dogBOSS = DingtalkChatbot(webhook, secret)
    url = 'http://kamo.fun'
    dogBOSS.send_text(
        msg=f'电费剩余',
        is_at_all=False)
if __name__ == '__main__':
    webhook = 'https://oapi.dingtalk.com/robot/send?access_token=19985c90103a1cb38f2c9e8f0f3d9e475xx'
    secrets = 'SEC602a8ce2713cb077deb398d8543xx'
    dingtalk_robot(webhook=webhook,
                   secret=secrets)

推送markdown

python 复制代码
#发送markdown格式
from dingtalkchatbot.chatbot import DingtalkChatbot
from datetime import  datetime
def dingtalk_robot(webhook,secret):
    dogBOSS = DingtalkChatbot(webhook, secret)
    red_msg = '<font color="#dd0000">级别:危险</font>'
    orange_msg = '<font color="#FFA500">级别:警告</font>'

    now_time = datetime.now().strftime('%Y.%m.%d %H:%M:%S')
    url = 'http://kamo.fun'
    dogBOSS.send_markdown(
        title=f'电费剩余',
        text=f'### **我是主内容的第一行**\n'
              f'**{red_msg}**\n\n'
              f'**{orange_msg}**\n\n'
              f'**发送时间:**  {now_time}\n\n'
              f'**相关网址:**[点击跳转]({url}) \n',
        is_at_all=True)

if __name__ == '__main__':
    webhook = 'https://oapi.dingtalk.com/robot/send?access_token=19985c90103a1cb38f2c9xxx'
    secrets = 'SEC602a8ce2713cb077deb398d85432bxxx'
    dingtalk_robot(webhook=webhook,
                   secret=secrets)
相关推荐
英英_44 分钟前
python 自动化教程
开发语言·python·自动化
万能程序员-传康Kk1 小时前
【Python+flask+mysql】网易云数据可视化分析(全网首发)
python·mysql·信息可视化·数据分析·flask·可视化·网易云
先做个垃圾出来………1 小时前
汉明距离(Hamming Distance)
开发语言·python·算法
测试者家园1 小时前
用 VS Code / PyCharm 编写你的第一个 Python 程序
ide·vscode·python·职场和发展·零基础·pycharm·零基础学python
墨绿色的摆渡人2 小时前
pytorch小记(二十一):PyTorch 中的 torch.randn 全面指南
人工智能·pytorch·python
大叔_爱编程2 小时前
p024基于Django的网上购物系统的设计与实现
python·django·vue·毕业设计·源码·课程设计·网上购物系统
一个天蝎座 白勺 程序猿2 小时前
Python爬虫(29)Python爬虫高阶:动态页面处理与云原生部署全链路实践(Selenium、Scrapy、K8s)
redis·爬虫·python·selenium·scrapy·云原生·k8s
90后小陈老师2 小时前
WebXR教学 09 项目7 使用python从0搭建一个简易个人博客
开发语言·python·web
weixin-WNXZ02182 小时前
闲上淘 自动上货工具运行原理解析
爬虫·python·自动化·软件工程·软件需求
正在走向自律2 小时前
Conda 完全指南:从环境管理到工具集成
开发语言·python·conda·numpy·fastapi·pip·开发工具