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

/**

启用 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=09b97e3306c80e7b9847e5344bf99c77a92e912746efbbc5806f947ba838912f'
declare @内容 nVARCHAR(max)
declare @联系人 nVARCHAR(max)
set @内容=N'测试系统'
set @联系人=N'15936402945","17337796839'
set @PostData = N'{"msgtype":"text","text":{"content":"报警:'+@内容+'"},"at":{"isAtAll":"false","atMobiles":["'+@联系人+'"]}}'
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

posted @ 2023-05-26 18:26  感生  阅读(103)  评论(0编辑  收藏  举报