Try   HackMD

AI LINE Bot練功坊-L5 Webhook Event Type

Webhook Event

常見的通用基本屬性

  1. type : 識別事件的類型
  2. mode : channel的狀態
  3. timestamp : 事件發生的時間(以毫秒為單位)
  4. source : 包含有關事件來源的資訊
  5. webhookeventID : 標示webhook event的ID
  6. deliveryContext.isRedelivery : Webhook event是否是重新傳遞的event。

Reference:https://developers.line.biz/en/reference/messaging-api/#common-properties

Webhook Event的類型

基本的Webhook Event有以下幾種

Webhook Event 說明 一對一聊天室 群組聊天室
Message event 表示使用者發送一則訊息(可以回覆的事件)
Unsend event 表示使用者收回一則訊息
Follow event 表示你的Line官方帳號被加入好友或者是解除封鎖(可以回覆的事件)
Unfollow event 表示你的Line官方帳號被封鎖或被刪除好友
Join event 表示你的帳號被加入群組(可以回覆的事件)
Leave event 表示你的帳號被從群組中移除
Member join event 在您的帳號存在的群組中有人加入的事件(可以回覆的事件)
Member leave event 在您的帳號存在的群組中有人退出的事件
Postback event 配合Postback action的訊息事件(可以回覆的事件)
Video viewing complete event 用戶觀看完從LINE官方帳號發送的指定TrackingId的影片訊息(可以回覆的事件)

✔ Your bot server receives this event ❌ Your bot server doesn't receive this event

Reference : https://developers.line.biz/en/docs/messaging-api/receiving-messages/#webhook-event-in-one-on-one-talk-or-group-chat

Message Event

表示使用者發送一則訊息,官方帳號可以根據這則訊息進行應對的回覆
Message Object包括以下幾種 :

  • Text
  • Image
  • Video
  • Audio
  • File
  • Location
  • Sticker

引用套件

from linebot.v3.webhooks import (
    MessageEvent,
    TextMessageContent
)

程式碼

@handler.add(MessageEvent, message=TextMessageContent)
def handle_text_message(event):
    print(f'Got {event.type} event')

message的type(指要接收到的的訊息類型)有:

  • TextMessageContent
  • LocationMessageContent
  • StickerMessageContent
  • ImageMessageContent
  • VideoMessageContent
  • AudioMessageContent
  • FileMessageContent

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

Follow Event

表示你的Line官方帳號被加入好友或者是解除封鎖

引用套件

from linebot.v3.webhooks import (
    FollowEvent
)

程式碼

@handler.add(FollowEvent)
def handle_follow(event):
    print(f'Got {event.type} event')

加入好友後會傳送一則"Got Follow Event"的訊息

Reference : https://developers.line.biz/en/reference/messaging-api/#follow-event

Postback Event

配合Postback action的訊息事件
當有action type為postback發生時,則會透過messaging API回傳event給Webhook URL

引用套件

from linebot.v3.messaging import (
    MessagingApi,
    ReplyMessageRequest,
    TemplateMessage,
    ButtonsTemplate,
    PostbackAction
)
from linebot.v3.webhooks import (
    MessageEvent,
    PostbackEvent
)

程式碼

觸發postback action的前置作業(建立一個選單訊息)

@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
    line_bot_api = MessagingApi(api_client)
    with ApiClient(configuration) as api_client:
        if event.message.text == 'postback':
            buttons_template = ButtonsTemplate(
                title='Postback Sample',
                text='Postback Action',
                actions=[
                    PostbackAction(label='Postback Action', text='Postback Action Button Clicked!', data='postback'),
                ])
            template_message = TemplateMessage(
                alt_text='Postback Sample',
                template=buttons_template
            )
            line_bot_api.reply_message(
                ReplyMessageRequest(
                    reply_token=event.reply_token,
                    messages=[template_message]
                )
            )

其中action有下列幾種 :

  1. MessageAction
  2. URIAction
  3. PostbackAction
  4. DatetimePickerAction
  5. CameraAction
  6. CameraRollAction
  7. LocationAction

程式碼

觸發action後的動作

@handler.add(PostbackEvent)
def handle_postback(event):
    if event.postback.data == 'postback':
        print('Postback event is triggered')

按下名為Postback Action的按鈕後,回應一個postback action且值為"postback"
當postback action傳送的值為"postback"時,會回覆一則訊息"Postback Action Button Clicked!"
在伺服器則會輸出一段訊息"Postback event is triggered"

Reference:https://developers.line.biz/en/reference/messaging-api/#postback-event

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

相關教材連結

◀◀◀ L04 Echo Bot 製作 ◀◀◀
▶▶▶ L06 Sending Message ▶▶▶