SQL Server通过钉钉机器人直接发送消息

时间:2022-08-16 08:48:03

 

RT

 

/**

启用 Ole Automation Procedures 选项
exec sp_configure 'show advanced options',1;
go
reconfigure;
go
sp_configure 'Ole Automation Procedures',1;
go
reconfigure;
go
*/
declare @PostData nVARCHAR(max) = '' , @ResponseText VARCHAR(max) = '' 
DECLARE @ServiceUrl AS VARCHAR(1000) 
set @ServiceUrl = N'https://oapi.dingtalk.com/robot/send?access_token=*****0'
set @PostData = N'{"msgtype": "markdown", "markdown": { 
"title":"机器人", 
"text": "
### 通知\n 
来自**机器人**的通知
" } }' 

DECLARE @Object AS INT ,
@status INT ,
@returnText AS VARCHAR(8000) ,
@HttpStatus VARCHAR(200) ,
@HttpMethod VARCHAR(20) = 'post'


EXEC @status = sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;

EXEC @status = sp_OAMethod @Object, 'open', NULL, @HttpMethod, @ServiceUrl, 'false'

EXEC @status = sys.sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type', 'application/json; charset=UTF-8'

EXEC @status = sp_OAMethod @Object, 'send', NULL, @PostData

EXEC @status = sys.sp_OAGetProperty @Object, 'Status', @HttpStatus OUT;

EXEC @status = sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

EXEC @status = sp_OADestroy @Object
print @ResponseText