栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

通过Webhook接入钉钉群自定义机器人

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Webhook接入钉钉群自定义机器人

在钉钉群中添加Webhook自定义机器人,

复制Webhook地址保存:

https://oapi.dingtalk.com/robot/send?access_token=xxxxxx

安全设置:选择加签方式,复制加签保存

#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse

timestamp = str(round(time.time() * 1000))
secret = '复制的安全设置中的加签'
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))
print(timestamp)
print(sign)

headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx×tamp=' + timestamp + "&sign=" + sign

def msg(text):
    json_text= {
    "msgtype": "text",
    "at": {
    "atMobiles": [
    "135xxxxxxx"],
    "isAtAll": False
    },
    "text": {
    "content": text
    }
    }
    print (requests.post(api_url,json.dumps(json_text),headers=headers).content)

if __name__ == '__main__':
    text = "我就是我, 是不一样的烟火"
    msg(text)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994432.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号