2017年1月2日 星期一

LINE Notify 入門到進階應用(3) --- Excel傳送網路圖片到Line Notify

LINE Notify 入門到進階應用(1) --- 訊息傳送環境設定 與 LINE Notify 入門到進階應用(2) --- Excel傳送文字到LINE Notify 介紹了環境安裝與傳送文字方法,今天介紹如何傳送URL圖片到Line Notify中,至於從本機端傳送圖片到Line Notify,由於操作過程複雜,後續再找時間分享。
沿用 LINE Notify 入門到進階應用(2) --- Excel傳送文字到LINE Notify 文章內的程式碼做些微修改,傳送URL中的圖片有別於一般傳送本機圖片,重點在於網路圖片的真實連結,而非網頁連結,很重要!很重要!很重要!(因為重要,所以講三次)
Postman使用。

Sub SendMessageToLineNotify()
    Dim oXML As Object
    Dim strToken As String
    Dim strMessage As String, strAllMessage As String
    Dim strImageThumbnail As String
    Dim strImageFullsize As String
    Dim strStickerPackageId As String
    Dim strStickerId As String
    Dim strPicURL As String
    Dim URL As String
    
    '指定的LineNotify Token
    strToken = "你的Token"
    
    'Line Notify的傳送訊息網址
    URL = "https://notify-api.line.me/api/notify"
        
    '中文字
    strMessage = "傳網頁圖檔測試 家扶娃娃撲滿 ~!@#$%^*()1234567890ABCabc"

    '網路圖片連結 家扶娃娃撲滿
    strPicURL = "https://www.beclass.com/share/201507/18379b4559c82b51a7a71010r.jpg"
    
    '傳送參數內容
    strMessage = "message=" & strMessage
    strImageThumbnail = "imageThumbnail=" & strPicURL
    strImageFullsize = "imageFullsize=" & strPicURL
    strStickerPackageId = "stickerPackageId=" & "1"
    strStickerId = "stickerId=" & "106"
    
    strAllMessage = strMessage & "&" & strImageThumbnail & "&" & strImageFullsize & "&" & strStickerPackageId & "&" & strStickerId
    
    '建立Ajax物件
    Set oXML = CreateObject("Microsoft.XMLHTTP")
    With oXML
        '使用同步傳輸
        .Open "POST", URL, 0
        
        '設定傳送封包Header
        .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .SetRequestHeader "Authorization", "Bearer " & strToken
        
        '執行Ajax傳送
        .send (strAllMessage)
        
        Debug.Print oXML.responseText
    End With
    
    '釋放物件資源
    Set oXML = Nothing
End Sub
程式碼第22行:家扶娃娃撲滿圖片的真實連結。

程式碼第27、28行:傳送圖片的參數imageThumbnail、imageFullsize,需成對存在傳送。

程式碼第31行:將全部字串組起來時,記得要用 "&" 串接起來。
程式碼第40行:由於使用application/x-www-form-urlencoded 為 Content-Type,所以不需要對中文字進行UTF-8的轉碼,使用multipart/form-data 為 Content-Type,才須對中文字進行UTF-8轉碼。
執行結果

參考資料