iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0

event

群組相關事件有4種

  • Join event 加入事件
  • Leave event 離開事件
  • Member join event 成員加入事件
  • Member leave event 成員離開事件
from linebot.models.events import (
    JoinEvent,
    LeaveEvent,
    MemberJoinedEvent,
    MemberLeftEvent
)

Join event

當你的官方帳號加入群組聊天室或多人聊天室時觸發

{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"join","timestamp":1632490426295,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"replyToken":"9e5f8c4cf7ea41d7853c71030831b8ba","mode":"active"}]}

Join event可得到的資訊
https://ithelp.ithome.com.tw/upload/images/20210924/20140165yl42NZ27Oc.jpg

Leave event

當你的官方帳號離開群組聊天室或多人聊天室時觸發

{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"leave","timestamp":1632490394283,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"mode":"active"}]}

Leave event資料沒有replyToken,其他和Join event一樣

Member join event

當成員加入群組觸發

{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"memberJoined","joined":{"members":[{"type":"user","userId":"Ufd738a53faa5ffea5de965c8e0d5a913"}]},"timestamp":1632489956072,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"replyToken":"01ff21658b634dd390098fd44185d3b4","mode":"active"}]}

https://ithelp.ithome.com.tw/upload/images/20210924/20140165Nno6rXedHk.jpg

Member leave event

當成員離開群組觸發

{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"memberLeft","left":{"members":[{"type":"user","userId":"Ufd738a53faa5ffea5de965c8e0d5a913"}]},"timestamp":1632489890949,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"mode":"active"}]}

Leave event資料沒有replyToken,Member join event成員資訊是joined,Member leave event是left,其他和Join event一樣

群組資訊

群組概括(group summary)

if(event.message.text=="Get group summary"):
        summary=line_bot_api.get_group_summary(event.source.group_id)
        message_text="Group summary\n"
        message_text+="group id"+summary.group_id+"\n"
        message_text+="group name"+summary.group_name+"\n"
        message_text+="picture url"+summary.picture_url
        message=TextSendMessage(message_text)

https://ithelp.ithome.com.tw/upload/images/20210924/201401659S7PRPCSz1.jpg

群組人數(group members count)

if(event.message.text=="Get group members count"):
        message=TextSendMessage(line_bot_api.get_group_members_count(event.source.group_id))

https://ithelp.ithome.com.tw/upload/images/20210924/20140165jaOwnHZlkt.jpg

群組成員使用者id(group member user IDs)

這個功能只有驗證過的官方帳號或premium帳號可以使用

member_ids_res = line_bot_api.get_group_member_ids(group_id)

print(member_ids_res.member_ids)
print(member_ids_res.next)

群組成員資料(group member profile)

profile = line_bot_api.get_room_member_profile(room_id, user_id)

print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)

離開群組(Leave group)

if(event.message.text=="Leave group"):
        message=TextSendMessage(line_bot_api.leave_group(event.source.group_id))

https://ithelp.ithome.com.tw/upload/images/20210924/20140165AU4SvFjpIe.jpg
參考:
https://developers.line.biz/en/reference/messaging-api#Group
https://developers.line.biz/en/reference/messaging-api/#webhook-event-objects
https://github.com/line/line-bot-sdk-python


上一篇
DAY 11 Quick replies & Action objects in Messaging API
下一篇
DAY 13 資料庫-建立並操作Heroku PostgreSQL
系列文
LINE Messaging API SDK for Python 實現群組團購輔助機器人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言