Pages - Menu

2018年12月12日 星期三

[MSSQL] 使用LineNotify發送通知

同事寫出來的,掛在stored procedure 下,
發送line 通知使用。
我也不知道他從哪些網頁參考出來的XD 
取得token的方式,可參考前面文章  快速使用Line Notify
簡單說明
@token 就是對方的token
@msg就是內容




EXEC sp_configure 'show advanced options', 1;  
RECONFIGURE WITH OVERRIDE;  
EXEC sp_configure 'Ole Automation Procedures', 1;  
RECONFIGURE WITH OVERRIDE;  
EXEC sp_configure 'show advanced options', 0;
--调用webService----------------
declare @ServiceUrl as varchar (1000)
DECLARE @UrlAddress varchar (500)
declare @l_type varchar(500)
declare @au varchar (500)

--WebService地址:以http开头,结尾带斜杠,例如'https://notify-api.line.me/api/notify'
set @UrlAddress = 'https://notify-api.line.me/api/notify'
set @au='Bearer '+ @token
set @l_type='application/x-www-form-urlencoded'
set @msg= 'message='+@msg

--如果有参数可以在此处拼入
SET @ServiceUrl=@UrlAddress
--select @ServiceUrl
--访问地址获取结果
Declare @Object as Int
Declare @ResponseText as Varchar (8000) --必须8000
EXEC sp_OACreate 'MSXML2.XMLHTTP' , @Object OUT ; --创建OLE组件对象
Exec sp_OAMethod @Object, 'open' , NULL , 'POST' ,@ServiceUrl, 'false' --打开链接,注意是get还是post
--Exec sp_OAMethod @Object, 'Authorization:',  @au,'Content-Type:', @l_type, 'message=', @msg
Exec sp_OAMethod @Object,'setRequestHeader', NULL, 'Content-Type', @l_type
Exec sp_OAMethod @Object,'setRequestHeader', NULL, 'Authorization', @au
Exec sp_OAMethod @Object, 'send', NULL, @msg
EXEC sp_OAMethod @Object, 'responseText' , @ResponseText OUTPUT --输出参数
--Select @ResponseText --输出结果
Exec sp_OADestroy @Object
END

7 則留言:

  1. 板大您好~剛好在找此功能~看到這篇教學~但照方式修改taken,雖procedure有執行成功,但沒收到line訊息~
    請教板大是否有遇過此問題呢?

    回覆刪除
    回覆
    1. 有先使用過line notify送過訊息成功嗎?

      刪除
  2. 我也遇到相同問題, procedure有執行成功, 但沒收到Line訊息

    Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
    Configuration option 'Ole Automation Procedures' changed from 1 to 1. Run the RECONFIGURE statement to install.
    Configuration option 'show advanced options' changed from 1 to 0. Run the RECONFIGURE statement to install.

    完成時間: 2021-03-30T09:06:21.5434229+08:00

    回覆刪除
    回覆
    1. 作者已經移除這則留言。

      刪除
    2. 您的問題,可能先參考他的說明,更改設定後試試

      刪除
  3. 沒辦法用 可以協助怎排除嗎@@ 出現這個

    {"status":400,"message":"message: must not be empty"}


    程式碼:
    EXEC sp_configure 'show advanced options', 1;
    RECONFIGURE WITH OVERRIDE;

    EXEC sp_configure 'Ole Automation Procedures', 1;
    RECONFIGURE WITH OVERRIDE;

    EXEC sp_configure 'show advanced options', 0;
    RECONFIGURE WITH OVERRIDE;


    declare @ServiceUrl as varchar(1000)
    set @ServiceUrl = 'https://notify-api.line.me/api/notify' --restful api 介面
    DECLARE @data varchar(max);

    set @data='zzz' --入參
    Declare @Object as Int
    Declare @ResponseText AS varchar(8000) ;

    Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
    Exec sp_OAMethod @Object, 'open', NULL, 'POST','https://notify-api.line.me/api/notify','false'
    Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type','application/x-www-form-urlencoded'
    Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Authorization', 'Bearer y2UFDqF8UlAUpi5RHAja4cJAFMRTBkjhqjZuTrxupDa'
    Exec sp_OAMethod @Object, 'send', NULL, 'message:zz' --傳送資料

    Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
    --EXEC sp_OAGetErrorInfo @Object --異常輸出
    Select @ResponseText as Response
    Exec sp_OADestroy @Object
    GO

    回覆刪除
    回覆
    1. Exec sp_OAMethod @Object, 'send', NULL, 'message:zz' --傳送資料


      這裡錯了,應該要是'message=zz'

      刪除