iT邦幫忙

0

用 Python 暢玩 Line bot - 19:加入與移除好友

  • 分享至 

  • twitterImage
  •  

當加入一個 line bot 的時候,通常 bot 會傳送一個歡迎訊息給使用者,而此設定除了可以透過 Line Official Account Manger內,聊天室相關的部份去做設定之外,也可以透過 Line API 觸發 follow/join event,去作出更彈性的歡迎訊息。
而 follow event 跟 join event 的差別在於,前者是個人加入 bot 時候會觸發,後者是 bot 加入一個群組時會觸發,有一點要注意的是,如果一個群組內已經有一個 line bot,便無法加入第二個 Line bot,會看到他自動退出。

(圖一,歡迎訊息設定介面)
https://ithelp.ithome.com.tw/upload/images/20211023/20142564rVKFBKrn03.png

(圖二、加入第二個 Line bot 到群組)
https://ithelp.ithome.com.tw/upload/images/20211023/201425643xV2H0Hdcw.jpg

加入

FollowEvent

follow enevt 跟 message event 一樣有 reply token,也就是說可以透過reply_message去進行傳送歡迎訊息,而如果想要記錄下該使用者的資料,可以從 source 從取得 userID,配合下章會說明的取得使用者資料,存入資料庫中以便日後使用。

{
"mode": "active",
"replyToken": "099c617dedde4a309ebd9e989b61317f",
"source": 
    {
    "type": "user",
    "userId": "Ub8c776653b3d290b3c3f9d1b9c852cb9"
    },
"timestamp": 1634882086789,
"type": "follow"
}

JoinEvent

跟 follow enevt 基本上一樣,不過此處 source 內的會是 groupID,發送的訊息是會到該群組,且不能透過成員在群組中觸發 event,去私下傳送訊息給在該成員。

{
"mode": "active",
"replyToken": "099c617dedde4a309ebd9e989b61317f",
"source": 
    {
    "groupId": "Ccf4e0e2b490e66ea6fb456102ee7b7ec",
    "type": "group"
    },
"timestamp": 1634882746613,
"type": "join"
}

離開

離開或封鎖該 Line bot 時候就會觸發的 event,而因為該 event 並沒有 reply token,因此不能在被封鎖的時候還傳送訊息,而這部份的 event 可以結合 follow event 運用在會員資料上,例如當觸發 follow event 時,就將該名使用者的資料加入資料庫中,當觸發 unfollow event 時,就將該名使用者的資料移除資料庫。

UnfollowEvent

{
"mode": "active",
"source": 
    {
    "type": "user",
    "userId": "Ub8c776653b3d290b3c3f9d1b9c852cb9"
    },
"timestamp": 1634882067569,
"type": "unfollow"
}

LeaveEvent

{
"mode": "active",
"source": 
    {
    "groupId": "Ccf4e0e2b490e66ea6fb456102ee7b7ec",
    "type": "group"
    },
"timestamp": 1634882775084,
"type": "leave"
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言