Try   HackMD

AI LINE Bot練功坊-L6 Sending Message

傳送訊息的方式

  • Reply Messgae
  • Push Message
  • Broadcast
  • Narrowcast
  • Multicast

匯入函式庫

from linebot.v3.messaging import (
    ReplyMessageRequest,
    PushMessageRequest,
    BroadcastRequest,
    MulticastRequest
)

Send Reply Message

當使用者將官方帳號加入好友或傳送訊息給官方帳號時,您可以用Messaging API做回覆。要傳送回覆訊息時,必須要有包含Reply Token的Webhook Event Object

程式碼

單純回覆訊息

line_bot_api.reply_message(
    ReplyMessageRequest(
        reply_token=event.reply_token,
        messages=[TextMessage(text=event.message.text)]
    )
)

有回傳HTTP Status Code

line_bot_api.reply_message_with_http_info(
    ReplyMessageRequest(
        reply_token=event.reply_token,
        messages=[TextMessage(text=event.message.text)]
    )
)

詳細HTTP狀態碼可參考官網

Reference:
https://developers.line.biz/en/reference/messaging-api/#status-codes

屬性

  1. reply_token
    當在使用reply_token時,需要確認以下幾件事情
    • Replytoken只能被使用一次
    • Reply Token必須在收到 Webhook 後一分鐘內使用。使用超過一分鐘並不能保證有效。
    • Reply Token在以下情況不能保證能使用
      • 原本的webhook中的Reply Token已被使用
      • Webhook Event發生20分鐘後
  2. messages
    放要傳送的訊息內容(最大數量是五則)
  3. notificationDisabled (傳送訊息的通知)
    預設為 false,傳送訊息時會有通知,如果將其設為 true,就會發送給使用者無聲訊息,使用者收到訊息後不會有通知

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
若是超過五則,則不會回應訊息,並且伺服器會出現以下錯誤

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/reference/messaging-api/#send-reply-message

Send Push Message

您可以發送Push Message的對象 :

  • 有將您的官方帳號加入好友的使用者
  • 有將您的官方帳號加入群組中
  • 七天內在一對一聊天中向您的LINE官方帳戶發送過訊息的用戶

以下情況可以發送Push Message但用戶不會收到:

  • 刪除LINE帳號的用戶
  • 封鎖發送訊息的LINE官方帳號的用戶
  • 尚未將LINE官方帳號新增為好友的使用者

使用者可以向尚未新增為好友的LINE官方帳號發訊息,若LINE官方帳號收到非好友的使用者的訊息,LINE官方帳號在七天內可以向該使用者發送Push Message

程式碼

單純推播訊息

line_bot_api.push_message(
    PushMessageRequest(
        to=event.source.user_id,
        messages=[TextMessage(text='PUSH!')]
    )
)

有回傳HTTP Status Code

line_bot_api.push_message_with_http_info(
    PushMessageRequest(
        to=event.source.user_id,
        messages=[TextMessage(text='PUSH!')]
    )
)

屬性

  1. to
    接收者的UserID或者是GroupID、RoomID(最大數量是五則)
  2. messages
    放要傳送的訊息內容
  3. notificationDisabled (傳送訊息的通知)
    預設為 false,傳送訊息時會有通知,如果將其設為true,就會發送給使用者無聲訊息,使用者收到訊息後不會有通知

Reference : https://developers.line.biz/en/reference/messaging-api/#send-push-message

Send Multicast Message

這是有效的傳送相同訊息給多位使用者的API,但是不能傳送到群組或者是多人聊天室
也可以傳送給一位使用者,但是還是建議使用Push Message,因為Push Message的延遲比較低
可以發訊息送給將官方帳號加入好友的使用者
如果回傳錯誤,訊息就不會被送出
以下情況可以發送Multicast message但用戶不會收到:

  • 刪除LINE帳號的使用者
  • 封鎖官方帳號的使用者
  • 沒有將官方帳號加入好友的使用者
  • channel中不存在的使用者ID

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    不同provider下的另一個channel取得的使用者ID

程式碼

單純推播訊息給多位使用者

line_bot_api.multicast(
    MulticastRequest(
        to=[event.source.user_id],
        messages=[TextMessage(text="Multicast Message")]
    )
)

有回傳HTTP Status Code

line_bot_api.multicast_with_http_info(
    MulticastRequest(
        to=[event.source.user_id],
        messages=[TextMessage(text="Multicast Message")]
    )
)

屬性

  1. to
    接收者的UserID的陣列(最多500個UserID)
  2. messages
    放要傳送的訊息內容(最大數量是五則)
  3. notificationDisabled (傳送訊息的通知)
    預設為 false,傳送訊息時會有通知,如果將其設為 true,就會發送給使用者無聲訊息,使用者收到訊息後不會有通知

Reference : https://developers.line.biz/en/reference/messaging-api/#send-multicast-message

Send Narrowcast Message

傳送訊息給多位加入官方帳號好友的使用者,可以指定特殊屬性的使用者(例如:年齡、性別、地區等等),但是不能傳送到群組或者是多人聊天室
如果回傳錯誤,訊息就不會被送出

特點:

  • 目標人數必須為100人以上

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    這個目標人數指的是LINE官方統計在LINE服務上活躍並具有高度確定性的使用者,不是發出訊息的對象的總人數

  • 特定受眾(至少50人)
  • 指定多個受眾作為發送條件時,每個受眾都至少要有50位收件者,否則會在訊息傳遞開始時發生錯誤

以下受眾沒有接收人數限制:

  • 從LINE官方帳號管理者或Messaging API建立的受眾
  • 聊天標籤生成受眾(Target Audience)

發送訊息剩餘量

  • 如果當月能發送的剩餘訊息量小於您LINE官方帳戶的目標人數(受眾)時,在發送narrowcast messages時會發生錯誤
  • 即使實際的收件人數夠小,當月能發送的剩餘訊息量也可能會暫時耗盡,導致在發送narrowcast messages期間無法傳送其他訊息

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
LINE平台收到request後,在後台非同步發送Narrowcast Message

以下情況可以發送Narrowcast Message但用戶不會收到:

  • 刪除LINE帳號的使用者
  • 封鎖官方帳號的使用者
  • 沒有將官方帳號加入好友的使用者
  • channel中不存在的使用者ID

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    不同provider下的另一個channel取得的使用者ID

利用AND、OR、NOT邏輯運算子將複數個接收者的條件結合

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
確定只有AND、OR、NOT三個條件,不可以指定空陣列

人口統計過濾物件

人口統計過濾物件是指過濾收件人的標準,可以利用剛才介紹的邏輯運算子將不同標準做結合
屬性

  • 用於人口統計過濾的資料都是三天前的
  • 如果沒有指定屬性,將會發送訊息給所有人,甚至是"未知"屬性值的使用者

Reference : https://developers.line.biz/en/reference/messaging-api/#send-narrowcast-message

Send Broadcast Message

向所有將您的官方帳號加入好友的人發送訊息
如果回傳錯誤,訊息就不會被送出

程式碼

單純群發訊息

line_bot_api.broadcast(
    BroadcastRequest(
        messages=[TextMessage(text='Broadcast Message')]
    )
)

有回傳HTTP Status Code

line_bot_api.broadcast_with_http_info(
    BroadcastRequest(
        messages=[TextMessage(text='Broadcast Message')]
    )
)

屬性

  1. messages
    放要傳送的訊息內容
    (最大數量是五則)
  2. notificationDisabled (傳送訊息的通知)
    預設為false,傳送訊息時會有通知,如果將其設為true,就會發送給使用者無聲訊息,使用者收到訊息後不會有通知

Reference : https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message

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官方帳號

相關教材連結

◀◀◀ L05 Webhook Event Type ◀◀◀
▶▶▶ L07 Message Type(1) ▶▶▶