zoukankan      html  css  js  c++  java
  • python&shell发送钉钉消息

    python发送钉钉消息

    import requests
    import time
    import hashlib
    import hmac
    import base64
    import re
    
    
    def SendMessageBySEC(message = ''):
        """
        机器人设置了加密签名的访问方式
        :param message:
        :return:
        """
        # secret:密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串,例如:SECxxxxxxxx
        secret = 'SECxxxxxxxx'
        # access_token:创建完钉钉机器人之后会自动生成,例如:access_tokenxxxx
        access_token = 'access_tokenxxxx'
        # timestamp:当前时间戳,单位是毫秒,与请求调用时间误差不能超过1小时
        timestamp = int(round(time.time() * 1000))
    
        # 加密,获取sign和timestamp
        data = (str(timestamp) + '
    ' + secret).encode('utf-8')
        secret = secret.encode('utf-8')
        signature = base64.b64encode(hmac.new(secret, data, digestmod=hashlib.sha256).digest())
        reg = re.compile(r"'(.*)'")
        signature = str(re.findall(reg,str(signature))[0])
    
        # 发送信息
        url = 'https://oapi.dingtalk.com/robot/send?access_token=%s&sign=%s&timestamp=%s' % (access_token,signature,timestamp)
        headers = {"Content-Type": "application/json ;charset=utf-8 "}
        try:
            response = requests.post(url, headers=headers, json=message, timeout=(3, 60))
            print(response)
            response_msg = str(response.status_code) + ' ' + str(response.content)
            print(response_msg)
        except Exception as error_msg:
            print('error_msg==='+str(error_msg))
            response_msg = error_msg
    
        return response_msg
    
    
    def SendMessageByKeywords(message=''):
        """
        钉钉机器人设置了关键字的访问方式
        :param message:
        :return:
        """
        # access_token:创建完钉钉机器人之后会自动生成,例如:access_tokenxxxx
        access_token = 'access_tokenxxxx'
        # 发送信息
        url = 'https://oapi.dingtalk.com/robot/send?access_token=%s' % access_token
        headers = {"Content-Type": "application/json ;charset=utf-8 "}
        try:
            response = requests.post(url, headers=headers, json=message, timeout=(3, 60))
            print(response)
            response_msg = str(response.status_code) + ' ' + str(response.content)
            print(response_msg)
        except Exception as error_msg:
            print('error_msg==='+str(error_msg))
            response_msg = error_msg
    
        return response_msg
    
    
    if __name__ == "__main__":
        # msg = {"msgtype": "text", "text": {"content": "test msg"}, "at": {"isAtAll": False}}
        # SendMessageBySEC(msg)
        msg = {"msgtype": "text", "text": {"content": "keywords: test msg"}, "at": {"isAtAll": False}} # 将keywords替换为设置钉钉机器人人时设置的关键字
        SendMessageByKeywords(msg)

    shell发送钉钉消息

    钉钉机器人设置了关键字的发送方式

    curl 'https://oapi.dingtalk.com/robot/send?access_token=access_tokenxxxx' -H 'Content-Type: application/json' -d '
    {"msgtype": "text", 
      "text": {
          "content": "keywords: test msg "
       }
    }'
    # 将keywords替换为设置钉钉机器人人时设置的关键字
  • 相关阅读:
    精妙Sql语句
    andrid向sdcard写入文件
    mysql的编码问题以及购物网站的数据库设计
    (转)ASP.NET MVC最佳实践(3)
    (转)Asp.Net MVC 体验 4 Unity使用
    (转)Visual Studio 2008 单元测试
    (转)ASP.NET MVC最佳实践(1)
    (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART3——依赖注入
    (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)
    (转)
  • 原文地址:https://www.cnblogs.com/golinux/p/12199973.html
Copyright © 2011-2022 走看看