提交 69b51a0d authored 作者: 陈泽健's avatar 陈泽健

实现钉钉群消息提醒接口调用,后续切换到其他测试公司进行定时推送验证。

上级 766eecac
...@@ -63,3 +63,5 @@ ...@@ -63,3 +63,5 @@
- 补充关于定制化项目的脚本路径,补充工商银行查询停车缴费系统数据的代码。 - 补充关于定制化项目的脚本路径,补充工商银行查询停车缴费系统数据的代码。
- 优化会议预约的脚本,增加预约类型、消息提醒、通知方式以及是否创建模板的代码判断,根据对应需求创建对应类型的会议。 - 优化会议预约的脚本,增加预约类型、消息提醒、通知方式以及是否创建模板的代码判断,根据对应需求创建对应类型的会议。
- 输出会议修改代码,增加预约类型的判断,处理对于周期会议的修改特殊处理。会议议题还需要补充完善。 - 输出会议修改代码,增加预约类型的判断,处理对于周期会议的修改特殊处理。会议议题还需要补充完善。
18. 2024-11-21
- 实现钉钉群消息提醒接口调用,后续切换到其他测试公司进行定时推送验证。
\ No newline at end of file
import requests
import json
def send_message(title, text, mobile):
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=b0eea0bbf097ce3badb4c832d2cd0267a50486f395ec8beca6e2042102bb295b' # 替换为实际的Webhook URL
headers = {'Content-Type': 'application/json'}
message = {
'msgtype': 'markdown',
'markdown': {
'title': title,
'text': text
},
"at": {
"atMobiles": mobile,
"isAtAll": False
}
}
try:
response = requests.post(webhook_url, data=json.dumps(message), headers=headers)
if response.status_code == 200:
print('消息发送成功!', message)
print('响应内容:', response.text)
else:
print('消息发送失败,状态码:', response.status_code)
print('响应内容:', response.text)
except requests.exceptions.RequestException as e:
print('请求异常:', e)
if __name__ == "__main__":
send_message('测试标题', '这是测试内容', '13724387318')
\ No newline at end of file
import logging
import time
import urllib
import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def send_message(title, text, mobile):
"""
发送钉钉机器人消息
:param title: 消息标题
:param text: 消息内容
:param mobile: 需要@的手机号列表
"""
# 钉钉机器人的 Webhook URL 和密钥
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=b0eea0bbf097ce3badb4c832d2cd0267a50486f395ec8beca6e2042102bb295b'
secret = 'SEC928b11659c5fd6476cfa2042edbf56da876abf759289f7e4d3c671fb9a81bf43'
# 生成时间戳
timestamp = str(round(time.time() * 1000))
# 生成签名
secret_enc = secret.encode('utf-8')
string_to_sign = f'{timestamp}\n{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))
# 构建最终的 Webhook URL
params = {
'access_token': webhook_url.split('=')[1],
'timestamp': timestamp,
'sign': sign
}
encoded_params = urlencode(params)
final_webhook_url = f'https://oapi.dingtalk.com/robot/send?{encoded_params}'
logging.info(f"钉钉机器人Webhook URL: {final_webhook_url}")
# 构建消息体
headers = {'Content-Type': 'application/json'}
message = {
'msgtype': 'markdown',
'markdown': {
'title': title,
'text': text
},
"at": {
"atMobiles": [mobile], # 将手机号列表化
"isAtAll": False
}
}
try:
# 发送 POST 请求
response = requests.post(final_webhook_url, data=json.dumps(message), headers=headers)
if response.status_code == 200:
logging.info('消息发送成功!')
logging.info(f'响应内容: {response.text}')
else:
logging.error(f'消息发送失败,状态码: {response.status_code}')
logging.error(f'响应内容: {response.text}')
except requests.exceptions.RequestException as e:
logging.error(f'请求异常: {e}')
if __name__ == "__main__":
send_message('测试标题', '这是测试内容', '13724387318')
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论