其他分享
首页 > 其他分享> > 钉钉机器人webhook的使用

钉钉机器人webhook的使用

作者:互联网

1、群启动webhook机器人:右上角设置--智能助手---添加机器人---其他机器人 2、获取webhook地址 0 3、调用方式不一样,则访问方式就不一样

 

--------------------------------------------方式一:加签----------------------------------------------- 1、python加签名的调用方式 pip install requests
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse

def send_text(countent_text):
    timestamp = str(round(time.time() * 1000))
    secret = 'SECc1495b626ab6ceb2415ee163ca3002cf2c77cbba82f6f3d55d9014c2ee58ed41'  #签名id
    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))
    url = f"https://oapi.dingtalk.com/robot/send?access_token=8684ad760333e6fbb8ec8c0c12e2c00fcf7e58e58eb5d0152d9cebb06dee&timestamp={timestamp}&sign={sign}"
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    data = {
    "msgtype":"text",
    "at": {"atMobiles": ["18381001111"], #群中@的人员
         "isAtAll": False},
    "text": {"content": countent_text}, "msgtype": "text"}
    print(json.dumps(data))
    print(countent_text)
    requests.post(url, headers=headers, data=json.dumps(data))

dx=f"-----------------------------sdfsdfsdf--------------------------------"
send_text(dx)

 

--------------------------------------------方式二:自定义关键词--------------------------------------------- 1、自定义关键词test
import requests
import json

def send_text(countent_text):
    url = f"https://oapi.dingtalk.com/robot/send?access_token=8684ad760333e6fbb8ec8c0c12e2c00fcf7e58e58eb5d0152d906de"
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    data = {"msgtype": "text", "text": {"content": countent_text}}
    print(json.dumps(data))
    print(countent_text)
    requests.post(url, headers=headers, data=json.dumps(data))
dx=f"test----------------------------sdfsdfsdf--------------------------------"
send_text(dx)
2、linux下直接curl调用
curl -H "Content-Type: application/json" -X POST -d '{"msgtype": "text", "text": {"content": "test1"}}' "https://oapi.dingtalk.com/robot/send?access_token=8684ad760333e6fbb8ec8c0c12e2c00fcf7e58e58eb5d0152d9cebb06dee"

  

  

 

标签:text,机器人,webhook,send,sign,json,使用,import,data
来源: https://www.cnblogs.com/wukc/p/16525473.html