美文网首页
钉钉群消息机器人 GO Python

钉钉群消息机器人 GO Python

作者: 会爬虫的小蟒蛇 | 来源:发表于2022-08-02 22:22 被阅读0次
    dd.png

    Go语言版本

    package main
    
    import "github.com/wanghuiyt/ding"
    
    func main() {
        d := ding.Webhook{
            // https://oapi.dingtalk.com/robot/send?access_token=48194add59d4c9d3aae83a99364008ca5299681e9bd310cb8c4d660d78057846
            AccessToken: "48194add59d4c9d3aae83a99364008ca5299681e9bd310cb8c4d660d78057846",  // 上面获取的 access_token
            Secret:      "SEC94255729d740033bb3986f9a56b2e0597c71b4dcc9eb5dfe872a2cdea8fb09e7",  // 上面获取的加签的值
        }
        _ = d.SendMessage("这是普通的群消息")
    }
    

    Python版本

    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse
    import requests
    
    
    def time_sign_get():
        timestamp = str(round(time.time() * 1000))
        # 这里输入你的密钥
        secret = 'SEC94255729d740033bb3986f9a56b2e0597c71b4dcc9eb5dfe872a2cdea8fb09e7'
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(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()
        sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    
        return timestamp, sign
    
    
    def send_message(access_token,msg):
        timestamp, sign = time_sign_get()
    
        headers = {
            'Content-Type': 'application/json',
        }
        params = {
            'sign': sign,
            'timestamp': timestamp,
        }
    
        data = {"msgtype": "text",
                "text": {
                    "content": msg
                    }
                }
    
        response = requests.post(
            f'https://oapi.dingtalk.com/robot/send?access_token={access_token}',
            headers=headers, params=params, json=data)
        print(response.text)
    
    send_message("48194add59d4c9d3aae83a99364008ca5299681e9bd310cb8c4d660d78057846",
                 "这是普通的群消息")
    

    对比起来 Golang精简而优雅

    相关文章

      网友评论

          本文标题:钉钉群消息机器人 GO Python

          本文链接:https://www.haomeiwen.com/subject/xvvewrtx.html