Try   HackMD

AI LINE Bot練功坊-L7 Message Type (1)

Message Type

  • Text Message 文字訊息
  • Sticker Message 貼圖訊息
  • Image Message 圖片訊息
  • Video Message 影片訊息
  • Audio Message 音訊訊息
  • Location Message 地點訊息
  • Imagemap Message 圖片地圖訊息
  • Template Message 模板訊息
  • Flex Message 彈性訊息

後面的課程再仔細介紹Imagemap Message、Template Message、Flex Message

匯入函式庫

from linebot.v3.messaging import (
    TextMessage,
    StickerMessage,
    ImageMessage,
    VideoMessage,
    AudioMessage,
    LocationMessage
)

1. Text message 文字訊息

最基本的Message API類型之一,只傳遞文字

規則:

  1. Unicode 表情符號
  2. Line 表情符號
  3. 最多5000字

程式碼:

TextMessage(text="這是文字訊息")

透過TextMessage(text="這是文字訊息")把特定文字傳入聊天室。

可以加入表情符號

#設定表情符號,index為表情符號在文字訊息的字元位置(從0開始算);product_id為表情包編號;emoji_id為特定表情符號編號
emojis = [Emoji(index=0, product_id="5ac1bfd5040ab15980c9b435", emoji_id="001"),
          Emoji(index=12, product_id="5ac1bfd5040ab15980c9b435", emoji_id="002")]
#$代表我們的表情符號
messages=[TextMessage(text='$ LINE 表情符號 $', emojis=emojis)]

emoji 對應的編號 : https://developers.line.biz/en/docs/messaging-api/emoji-list/#line-emoji-definitions

效果展示:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Reference:https://developers.line.biz/en/docs/messaging-api/message-types/#text-messages

2. Sticker message 貼圖訊息

最基本的Message API類型之一,只傳遞貼圖

規則:

  1. 輸入貼圖的ID

程式碼:

StickerMessage(
    package_id="446",
    sticker_id="1988"
)

package_id是貼圖包的編號,sticker_id是特定貼圖的編號。

連結是Line官方提供的貼圖包和特定貼圖的id,可以查詢想用的貼圖使用:
https://developers.line.biz/en/docs/messaging-api/sticker-list/#sticker-definitions

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

圖片來源:Line Developer官方網站

效果展示:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Reference: https://developers.line.biz/en/docs/messaging-api/message-types/#sticker-messages

3. Image message 圖片訊息

最基本的Message API類型之一,只傳遞圖片

規則:

  1. 圖片 URL (最多字數: 2000)
  2. 前綴為HTTPS
  3. 圖片格式: JPEG 或 PNG
  4. 圖片檔案大小: 10 MB以內

程式碼:

ImageMessage(
    original_content_url=url, 
    preview_image_url=url
)

original_content_url=url是要傳送的圖片的位置, preview_image_url=url是預覽圖片的位置。

效果展示:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

url要提供想要傳送的圖片的位置,可以使用本地端的圖片或網路上的圖:

3-1 網路圖片

輸入圖片網址,例如:
url = "https://example.com/original.jpg"

3-2 本地圖片

抓取本地圖片到架設好的伺服器上。

#伺服器的url+圖片在本地端的位置
url = request.url_root + '/static/Logo.jpg'
#把https前綴改成http
url = url.replace("http", "https")

app.logger.info("url=" + url)

Reference: https://developers.line.biz/en/docs/messaging-api/message-types/#image-messages

4. Video message 影片訊息

最基本的Message API類型之一,只傳遞影片

規則:

  1. 影片 URL (最多字數: 2000)
  2. 前綴為HTTPS
  3. 影片格式: mp4
  4. 影片檔案大小: 200 MB以內

程式碼:

VideoMessage(
    original_content_url=url, 
    preview_image_url=url
)

影片的程式內容和圖片相似,可參考<3. Image message 圖片訊息>

效果展示:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Reference: https://developers.line.biz/en/docs/messaging-api/message-types/#video-messages

5. Audio message 音訊訊息

最基本的Message API類型之一,只傳遞音訊

規則:

  1. 音訊 URL (最多字數: 2000)
  2. 前綴為HTTPS
  3. 音訊格式: mp3 或 m4a
  4. 音訊檔案大小: 200 MB

程式碼:

AudioMessage(
    original_content_url=url, 
    duration=duration
)

音訊的程式內容和圖片相似,可參考<3. Image message 圖片訊息>

效果展示:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Reference: https://developers.line.biz/en/docs/messaging-api/message-types/#audio-messages

6. Location message 地點訊息

最基本的Message API類型之一,只傳遞地點

規則:

  1. latitude、longitude,最多字數各100字

程式碼:

LocationMessage(
    title='Location',
    address="Taipei",
    latitude=25.0475,
    longitude=121.5173
)               

title是位置訊息的標題, address放你想說明的位置,latitude、longitude是位置的座標。

效果展示:
image

Reference: https://developers.line.biz/en/docs/messaging-api/message-types/#location-messages

Youtube 課程影片

關於我們

image 國立臺北教育大學 教育大數據微學程

先修微課程

🤖 AI LineBot 練功坊系列課程
從入門到精通,學習如何開發並應用 LINE Bot,讓你輕鬆掌握最前沿的聊天機器人技術。

👨‍💻 Python 初學小教室
針對零基礎學員設計,循序漸進地教授 Python 基本語法及實作技巧,幫助你快速上手。

📊 統計學小教室
系統講解統計學理論及其應用,適合想要提升數據分析能力的學習者。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Facebook
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Instagram
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Threads
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
YouTube
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Line官方帳號

相關教材連結

◀◀◀ L06 Sending Message ◀◀◀
▶▶▶ L08 Message Type(2) Template Message ▶▶▶