0
# User and Profile Management
1
2
User profile access, group and room management, follower insights, and membership features. This module provides comprehensive user interaction capabilities including profile retrieval, group management, and follower analytics.
3
4
## Capabilities
5
6
### User Profile Management
7
8
Access detailed user profile information and interaction history.
9
10
```python { .api }
11
class MessagingApi:
12
def get_profile(self, user_id: str) -> UserProfileResponse:
13
"""
14
Get user profile information including display name and profile picture.
15
16
Args:
17
user_id: LINE user ID
18
19
Returns:
20
UserProfileResponse: User profile data
21
"""
22
23
def get_group_member_profile(self, group_id: str, user_id: str) -> GroupUserProfileResponse:
24
"""
25
Get user profile within a specific group context.
26
27
Args:
28
group_id: LINE group ID
29
user_id: LINE user ID within the group
30
31
Returns:
32
GroupUserProfileResponse: User profile in group context
33
"""
34
35
def get_room_member_profile(self, room_id: str, user_id: str) -> RoomUserProfileResponse:
36
"""
37
Get user profile within a specific room context.
38
39
Args:
40
room_id: LINE room ID
41
user_id: LINE user ID within the room
42
43
Returns:
44
RoomUserProfileResponse: User profile in room context
45
"""
46
```
47
48
### Group Management
49
50
Manage group chats including member access and group information retrieval.
51
52
```python { .api }
53
class MessagingApi:
54
def get_group_summary(self, group_id: str) -> GroupSummaryResponse:
55
"""
56
Get group information including name and member count.
57
58
Args:
59
group_id: LINE group ID
60
61
Returns:
62
GroupSummaryResponse: Group metadata and information
63
"""
64
65
def get_group_members_count(self, group_id: str) -> GroupMemberCountResponse:
66
"""
67
Get total number of members in a group.
68
69
Args:
70
group_id: LINE group ID
71
72
Returns:
73
GroupMemberCountResponse: Member count information
74
"""
75
76
def get_group_member_user_ids(self, group_id: str, start: Optional[str] = None) -> MembersIdsResponse:
77
"""
78
Get user IDs of group members with pagination.
79
80
Args:
81
group_id: LINE group ID
82
start: Pagination start token
83
84
Returns:
85
MembersIdsResponse: Member user IDs and pagination info
86
"""
87
88
def leave_group(self, group_id: str) -> dict:
89
"""
90
Make the bot leave a group chat.
91
92
Args:
93
group_id: LINE group ID to leave
94
95
Returns:
96
dict: Empty response on success
97
"""
98
```
99
100
### Room Management
101
102
Manage room chats with member access and room information retrieval.
103
104
```python { .api }
105
class MessagingApi:
106
def get_room_member_count(self, room_id: str) -> RoomMemberCountResponse:
107
"""
108
Get total number of members in a room.
109
110
Args:
111
room_id: LINE room ID
112
113
Returns:
114
RoomMemberCountResponse: Member count information
115
"""
116
117
def get_room_member_user_ids(self, room_id: str, start: Optional[str] = None) -> MembersIdsResponse:
118
"""
119
Get user IDs of room members with pagination.
120
121
Args:
122
room_id: LINE room ID
123
start: Pagination start token
124
125
Returns:
126
MembersIdsResponse: Member user IDs and pagination info
127
"""
128
129
def leave_room(self, room_id: str) -> dict:
130
"""
131
Make the bot leave a room chat.
132
133
Args:
134
room_id: LINE room ID to leave
135
136
Returns:
137
dict: Empty response on success
138
"""
139
```
140
141
### Follower Management
142
143
Access follower information and manage follower relationships.
144
145
```python { .api }
146
class MessagingApi:
147
def get_followers(self, start: Optional[str] = None, limit: Optional[int] = None) -> GetFollowersResponse:
148
"""
149
Get list of users who follow the bot with pagination.
150
151
Args:
152
start: Pagination start token
153
limit: Maximum number of followers to return (max 300)
154
155
Returns:
156
GetFollowersResponse: Follower user IDs and pagination info
157
"""
158
159
def get_bot_info(self) -> BotInfoResponse:
160
"""
161
Get bot information including display name and settings.
162
163
Returns:
164
BotInfoResponse: Bot profile and configuration data
165
"""
166
```
167
168
### Membership Management
169
170
Handle membership features for premium LINE services and subscriptions.
171
172
```python { .api }
173
class MessagingApi:
174
def get_membership_subscription(self, user_id: str) -> GetMembershipSubscriptionResponse:
175
"""
176
Get user's membership subscription information.
177
178
Args:
179
user_id: LINE user ID
180
181
Returns:
182
GetMembershipSubscriptionResponse: Subscription details and status
183
"""
184
185
def get_memberships(self) -> MembershipListResponse:
186
"""
187
Get list of available memberships for the bot.
188
189
Returns:
190
MembershipListResponse: Available membership plans
191
"""
192
193
def get_joined_membership_users(
194
self,
195
membership_id: str,
196
start: Optional[str] = None,
197
limit: Optional[int] = None
198
) -> GetJoinedMembershipUsersResponse:
199
"""
200
Get users who have joined a specific membership.
201
202
Args:
203
membership_id: Membership plan identifier
204
start: Pagination start token
205
limit: Maximum number of users to return
206
207
Returns:
208
GetJoinedMembershipUsersResponse: Member users and pagination info
209
"""
210
```
211
212
### Message Reading Management
213
214
Manage message read status and user interaction tracking.
215
216
```python { .api }
217
class MessagingApi:
218
def mark_messages_as_read(self, mark_messages_as_read_request: MarkMessagesAsReadRequest) -> dict:
219
"""
220
Mark messages as read for chat management.
221
222
Args:
223
mark_messages_as_read_request: Messages to mark as read
224
225
Returns:
226
dict: Empty response on success
227
"""
228
```
229
230
## User Management Models
231
232
### Profile Response Models
233
234
```python { .api }
235
class UserProfileResponse:
236
display_name: str
237
user_id: str
238
language: str
239
picture_url: Optional[str] = None
240
status_message: Optional[str] = None
241
242
class GroupUserProfileResponse:
243
display_name: str
244
user_id: str
245
picture_url: Optional[str] = None
246
247
class RoomUserProfileResponse:
248
display_name: str
249
user_id: str
250
picture_url: Optional[str] = None
251
```
252
253
### Group and Room Models
254
255
```python { .api }
256
class GroupSummaryResponse:
257
group_id: str
258
group_name: str
259
picture_url: Optional[str] = None
260
261
class GroupMemberCountResponse:
262
count: int
263
264
class RoomMemberCountResponse:
265
count: int
266
267
class MembersIdsResponse:
268
member_ids: List[str]
269
next: Optional[str] = None
270
```
271
272
### Follower Models
273
274
```python { .api }
275
class GetFollowersResponse:
276
user_ids: List[str]
277
next: Optional[str] = None
278
279
class BotInfoResponse:
280
basic_id: str
281
premium_id: Optional[str] = None
282
display_name: str
283
picture_url: Optional[str] = None
284
chat_mode: str
285
mark_as_read_mode: str
286
```
287
288
### Membership Models
289
290
```python { .api }
291
class GetMembershipSubscriptionResponse:
292
memberships: List[Membership]
293
next: Optional[str] = None
294
295
class Membership:
296
membership_id: str
297
subscription_id: str
298
title: str
299
description: str
300
image_url: Optional[str] = None
301
premium_id: str
302
303
class MembershipListResponse:
304
memberships: List[Membership]
305
306
class GetJoinedMembershipUsersResponse:
307
users: List[SubscribedMembershipUser]
308
next: Optional[str] = None
309
310
class SubscribedMembershipUser:
311
user_id: str
312
subscription: Subscription
313
314
class Subscription:
315
subscription_id: str
316
membership_id: str
317
plan: SubscribedMembershipPlan
318
319
class SubscribedMembershipPlan:
320
plan_id: str
321
title: str
322
description: str
323
```
324
325
### Message Management Models
326
327
```python { .api }
328
class MarkMessagesAsReadRequest:
329
def __init__(self, chat_id: str):
330
"""
331
Request to mark messages as read in a chat.
332
333
Args:
334
chat_id: Chat identifier (user, group, or room ID)
335
"""
336
self.chat_id = chat_id
337
```
338
339
## Usage Examples
340
341
### Basic User Profile Access
342
343
```python
344
from linebot.v3.messaging import MessagingApi, Configuration
345
346
# Initialize API client
347
config = Configuration(access_token='YOUR_ACCESS_TOKEN')
348
messaging_api = MessagingApi(config)
349
350
# Get user profile
351
try:
352
profile = messaging_api.get_profile("USER_ID_HERE")
353
print(f"User: {profile.display_name}")
354
print(f"Language: {profile.language}")
355
if profile.picture_url:
356
print(f"Profile Picture: {profile.picture_url}")
357
if profile.status_message:
358
print(f"Status: {profile.status_message}")
359
except Exception as e:
360
print(f"Failed to get profile: {e}")
361
```
362
363
### Group Management
364
365
```python
366
# Get group information
367
group_id = "GROUP_ID_HERE"
368
369
# Basic group info
370
group_info = messaging_api.get_group_summary(group_id)
371
print(f"Group: {group_info.group_name}")
372
373
# Member count
374
member_count = messaging_api.get_group_members_count(group_id)
375
print(f"Members: {member_count.count}")
376
377
# Get member list with pagination
378
members_response = messaging_api.get_group_member_user_ids(group_id)
379
print(f"Member IDs: {members_response.member_ids}")
380
381
# Get more members if available
382
if members_response.next:
383
more_members = messaging_api.get_group_member_user_ids(
384
group_id,
385
start=members_response.next
386
)
387
print(f"Additional members: {more_members.member_ids}")
388
389
# Get individual member profiles
390
for user_id in members_response.member_ids[:5]: # First 5 members
391
try:
392
member_profile = messaging_api.get_group_member_profile(group_id, user_id)
393
print(f"Member: {member_profile.display_name} ({user_id})")
394
except Exception as e:
395
print(f"Could not get profile for {user_id}: {e}")
396
```
397
398
### Follower Analytics
399
400
```python
401
# Get bot information
402
bot_info = messaging_api.get_bot_info()
403
print(f"Bot Name: {bot_info.display_name}")
404
print(f"Basic ID: {bot_info.basic_id}")
405
print(f"Chat Mode: {bot_info.chat_mode}")
406
407
# Get follower list with pagination
408
def get_all_followers():
409
all_followers = []
410
start_token = None
411
412
while True:
413
response = messaging_api.get_followers(start=start_token, limit=300)
414
all_followers.extend(response.user_ids)
415
416
if not response.next:
417
break
418
start_token = response.next
419
420
return all_followers
421
422
followers = get_all_followers()
423
print(f"Total followers: {len(followers)}")
424
425
# Get follower profiles (sample)
426
for user_id in followers[:10]: # First 10 followers
427
try:
428
profile = messaging_api.get_profile(user_id)
429
print(f"Follower: {profile.display_name} - {profile.language}")
430
except Exception as e:
431
print(f"Could not get profile for {user_id}: {e}")
432
```
433
434
### Room Management
435
436
```python
437
room_id = "ROOM_ID_HERE"
438
439
# Get room member count
440
room_count = messaging_api.get_room_member_count(room_id)
441
print(f"Room members: {room_count.count}")
442
443
# Get room member IDs
444
room_members = messaging_api.get_room_member_user_ids(room_id)
445
print(f"Room member IDs: {room_members.member_ids}")
446
447
# Get room member profiles
448
for user_id in room_members.member_ids:
449
try:
450
member_profile = messaging_api.get_room_member_profile(room_id, user_id)
451
print(f"Room member: {member_profile.display_name}")
452
except Exception as e:
453
print(f"Could not get room profile for {user_id}: {e}")
454
455
# Leave room if needed
456
# messaging_api.leave_room(room_id)
457
```
458
459
### Membership Management
460
461
```python
462
# Get available memberships
463
memberships = messaging_api.get_memberships()
464
print(f"Available memberships: {len(memberships.memberships)}")
465
466
for membership in memberships.memberships:
467
print(f"Membership: {membership.title}")
468
print(f"Description: {membership.description}")
469
470
# Get users who joined this membership
471
joined_users = messaging_api.get_joined_membership_users(membership.membership_id)
472
print(f"Joined users: {len(joined_users.users)}")
473
474
# Check specific user's membership
475
user_id = "USER_ID_HERE"
476
try:
477
user_memberships = messaging_api.get_membership_subscription(user_id)
478
print(f"User memberships: {len(user_memberships.memberships)}")
479
480
for membership in user_memberships.memberships:
481
print(f"User has membership: {membership.title}")
482
except Exception as e:
483
print(f"Could not get user memberships: {e}")
484
```
485
486
### Advanced User Analytics
487
488
```python
489
from collections import Counter
490
import time
491
492
class UserAnalytics:
493
def __init__(self, messaging_api: MessagingApi):
494
self.api = messaging_api
495
496
def analyze_follower_languages(self):
497
"""Analyze follower language distribution."""
498
followers = self.get_all_followers()
499
languages = []
500
501
# Rate limit: get profiles with delays
502
for i, user_id in enumerate(followers):
503
try:
504
profile = self.api.get_profile(user_id)
505
languages.append(profile.language)
506
507
# Avoid rate limiting
508
if i % 50 == 0:
509
time.sleep(1)
510
511
except Exception as e:
512
print(f"Could not get profile for {user_id}: {e}")
513
continue
514
515
# Analyze language distribution
516
language_counts = Counter(languages)
517
print("Follower language distribution:")
518
for lang, count in language_counts.most_common():
519
percentage = (count / len(languages)) * 100
520
print(f"{lang}: {count} ({percentage:.1f}%)")
521
522
return language_counts
523
524
def analyze_group_activity(self, group_id: str):
525
"""Analyze group member activity and profiles."""
526
group_info = self.api.get_group_summary(group_id)
527
member_count = self.api.get_group_members_count(group_id)
528
529
print(f"Group Analysis: {group_info.group_name}")
530
print(f"Total members: {member_count.count}")
531
532
# Get member profiles
533
members = self.api.get_group_member_user_ids(group_id)
534
active_members = 0
535
536
for user_id in members.member_ids:
537
try:
538
# Get both group profile and general profile
539
group_profile = self.api.get_group_member_profile(group_id, user_id)
540
general_profile = self.api.get_profile(user_id)
541
542
print(f"Member: {group_profile.display_name}")
543
print(f" Language: {general_profile.language}")
544
active_members += 1
545
546
except Exception as e:
547
# User might have blocked the bot or left
548
continue
549
550
print(f"Accessible member profiles: {active_members}/{len(members.member_ids)}")
551
return {
552
'total_members': member_count.count,
553
'accessible_members': active_members,
554
'group_name': group_info.group_name
555
}
556
557
def get_all_followers(self):
558
"""Get all follower IDs with pagination."""
559
all_followers = []
560
start_token = None
561
562
while True:
563
try:
564
response = self.api.get_followers(start=start_token, limit=300)
565
all_followers.extend(response.user_ids)
566
567
if not response.next:
568
break
569
start_token = response.next
570
571
except Exception as e:
572
print(f"Error fetching followers: {e}")
573
break
574
575
return all_followers
576
577
# Usage
578
analytics = UserAnalytics(messaging_api)
579
language_distribution = analytics.analyze_follower_languages()
580
```
581
582
### Message Reading Management
583
584
```python
585
from linebot.v3.messaging.models import MarkMessagesAsReadRequest
586
587
# Mark messages as read in a chat
588
def mark_chat_as_read(chat_id: str):
589
"""Mark all messages in a chat as read."""
590
try:
591
request = MarkMessagesAsReadRequest(chat_id=chat_id)
592
messaging_api.mark_messages_as_read(request)
593
print(f"Marked messages as read for chat: {chat_id}")
594
except Exception as e:
595
print(f"Failed to mark messages as read: {e}")
596
597
# Mark messages as read for multiple chats
598
def mark_multiple_chats_as_read(chat_ids: list):
599
"""Mark messages as read for multiple chats."""
600
for chat_id in chat_ids:
601
mark_chat_as_read(chat_id)
602
time.sleep(0.1) # Small delay to avoid rate limiting
603
```