Chat Feature

Client

class ChatClient(channel_id: str, authorization_key: str | None = None, session_key: str | None = None, chat_channel_id: str | None = None, loop: AbstractEventLoop | None = None)

Bases: Client

Represents a client to connect Chzzk (Naver Live Streaming). Addition, this class includes chat feature.

async blind_message(message: ChatMessage) None

Blinds a chat.

Parameters:

message (ChatMessage) – A Chat to blind.

async close()

Close the connection to chzzk.

async delete_notice_message() None

Delete a pinned message.

async history(count: int = 50) list[ChatMessage]

Get messages the user has previously sent.

Parameters:

count (Optional[int]) – Number of messages to fetch from the most recent, by default 50

Returns:

Returns the user’s most recently sent messages, in order of appearance

Return type:

list[ChatMessage]

property is_connected: bool

Specifies if the client successfully connected with chzzk.

async live_detail(channel_id: str | None = None)

Get a live detail info of broadcaster.

Parameters:

channel_id (Optional[str]) – The channel ID of broadcaster, default by channel id of ChatClient.

Returns:

Return LiveDetail info. Sometimes the broadcaster is not broadcasting, returns None.

Return type:

Optional[LiveDetail]

async live_status(channel_id: str | None = None)

Get a live status info of broadcaster.

Parameters:

channel_id (Optional[str]) – The channel ID of broadcaster, default by channel id of ChatClient.

Returns:

Return LiveStatus info. Sometimes the broadcaster is not broadcasting, returns None.

Return type:

Optional[LiveStatus]

login(authorization_key: str, session_key: str)

Login at Chzzk. Used for features that require a login. (ex. user method)

Parameters:
  • authorization_key (str) – A NID_AUT value in the cookie.

  • session_key (str) – A NID_SES value in the cookie.

manage(channel_id: str | None = None) ManageClient

Get a client provided broadcast management functionality.

Parameters:

channel_id (Optional[str]) – A channel id to manage broadcasts. The default value is the last channel id used. If initally use the manage method and don’t have a channel_id argument, the default value is channel id of ChatClient.

Returns:

Return a client provided broadcast management functionality.

Return type:

ManageClient

property manage_self: ManageClient

Get a client provided self-channel management functionally.

async profile_card(user: PartialUser | str) Profile

Get a profile card.

Parameters:

user (ParticleUser | str) – A user object to get profile card. Instead, it can be user id.

Returns:

Returns a profile card with this channel information (include following info).

Return type:

Profile

async request_recent_chat(count: int = 50)

Send a request recent chat to chzzk. This method only makes a “request”. If you want to get the recent chats of participants, use the history method.

Parameters:

count (Optional[int]) – Number of messages to fetch from the most recent, by default 50

Raises:

RuntimeError – Occurs when the client can’t connect to a broadcaster’s chat

async send_chat(message: str) None

Send a message.

Parameters:

message (str) – Message to Broadcasters

Raises:

RuntimeError – Occurs when the client can’t connect to a broadcaster’s chat

async set_notice_message(message: ChatMessage) None

Set a pinned messsage.

Parameters:

message (ChatMessage) – A Chat to pin.

async temporary_restrict(user: PartialUser | str) PartialUser

Give temporary restrict to user. A temporary restriction cannot be lifted arbitrarily.

Parameters:

user (ParticleUser | str) – A user object to give temporary restrict activity. Instead, it can be user id.

Returns:

Returns an user temporary restricted in chat.

Return type:

ParticleUser

wait_for(event: str, check: Callable[[...], bool] | None = None, timeout: float | None = None)

Waits for a WebSocket event to be dispatched.

Parameters:
  • event (str) – The event name. For a list of events, read event()

  • check (Optional[Callable[..., bool]],) – A predicate to check what to wait for. The arguments must meet the parameters of the event being waited for.

  • timeout (Optional[float]) – The number of seconds to wait before timing out and raising asyncio.TimeoutError.

async wait_until_connected() None

Waits until the client’s internal cache is all ready.

Event Refenence

This section describes the events listened that ChatClient received. You can received event with decorator event method.

For example:

>>> @client.event
... async def on_chat(message: ChatMessage):
...     print(message.content)

All event method must be a coroutine. Otherwise, unexpected errors may occur.

async on_chat(message: ChatMessage)

Call when a ChatMesage is created and sent.

Parameters:

message (ChatMessage) – The current message.

async on_connect()

Called when the client has successfully connected to chzzk chat.

async on_donation(donation: DonationMessage)

Called when a broadcaster received donation. Donation types include Chat, Video, and Mission, which are all invoked.

Parameters:

message (DonationMessage) – The message included donation info.

async on_system_message(system_message: SystemMessage)

Called when a SystemMessage is created and sent.

Parameters:

message (SystemMessage) – The system message.

async on_subscription(subscription: SubscriptionMessage)

Called when a broadcast participant registered a new subscription.

Parameters:

message (SubscriptionMessage) – The message included subscription info.

async on_recent_chat(messages: RecentChat)

Called when a client requests a recent chat and receives a response.

Parameters:

messages (RecentChat) – The historical messages

async on_pin(message: NoticeMessage)

Called when a broadcaster created a pin message. You can use on_notice event hanlder, instead of on_pin event handler.

Parameters:

message (NoticeMessage) – The notice message that a broadcaster pinned.

async on_unpin(message: NoticeMessage)

Called when a broadcaster removed a pin message.

Parameters:

message (NoticeMessage) – The notice message that a broadcaster un-pinned.

async on_blind(message: Blind)

Called when a broadcaster or manager blinded a chat.

Parameters:

message (Blind) – The blinded message.

async on_mission_completed(mission: MissionDonation)

Called when a broadcaster completed a mission.

Parameters:

mission (MissionDonation) – The mission donation that a broadcaster cleared.

async on_mission_pending(mission: MissionDonation)

Called when a broadcast participant created a new mission.

Parameters:

mission (MissionDonation) – The mission donation that a broadcaster cleared.

async on_mission_approved(mission: MissionDonation)

Called when a broadcaster approved a mission.

Parameters:

mission (MissionDonation) – The mission donation that a broadcaster approved.

async on_mission_rejected(mission: MissionDonation)

Called when a broadcaster rejected a mission.

Parameters:

mission (MissionDonation) – The mission donation that a broadcaster rejected.

async on_client_error(exception: Exception, *args, **kwargs)

Called when an event hanlder raised exception. The *args and **kwargs argument includes event handler arguments.

Blind

This model is used in the on_blind event handler, which contains the blinded message.

class Blind
blind_type: str
blind_user_id: str | None
message: str | None
service_id: str
time: datetime
user_id: str

Connection

class ConnectedInfo
access_token: str
auth: Literal['SEND', 'READ']
session_id: str
uuid: str | None

Donation

class DonationRank
donation_amount: int
nickname: str
ranking: int
user_id_hash: str
verified_mark: bool
class BaseDonation
donation_type: str
donation_user_weekly_rank: DonationRank | None
is_anonymous: bool
pay_amount: int
pay_type: str
weekly_rank_list: list[DonationRank] | None
class ChatDonation

Bases: BaseDonation

donation_type: Literal['CHAT']
class VideoDonation

Bases: BaseDonation

donation_type: Literal['VIDEO']
class MissionDonation

Bases: BaseDonation

donation_type: Literal['MISSION']
duration_time: int
mission_created_time: datetime.datetime
mission_donation_id: str | None
mission_donation_type: str | None
mission_end_time: datetime.datetime | None
mission_start_time: datetime.datetime | None
mission_text: str
status: str | Literal['PENDING', 'REJECTED', 'APPROVED', 'COMPLETED']
success: bool

Message

class Message
channel_id: str
content: str
created_time: datetime.datetime
extras: Json[E] | None
property is_me: bool

Verify that this message is from a user signed in to the client.

profile: Json[Profile] | None
async send(message: str)

Send message to broadcaster.

service_id: str
time: datetime.datetime
type: ChatType
updated_time: datetime.datetime | None
user_id: str
class MessageDetail

Bases: Message, Generic[E]

property is_blind: bool
member_count: int
message_status: str | None
class ChatMessage

Bases: MessageDetail[Extra]

async blind()

Blind this message.

async pin()

Pin this message.

async temporary_restrict()

Temporary restrict this user.

async unpin()

Unpin this message.

class NoticeMessage

Bases: Message[NoticeExtra]

async unpin()

Unpin this message.

class DonationMessage

Bases: MessageDetail[Union[ChatDonationExtra, VideoDonationExtra, MissionDonationExtra]]

class SubscriptionMessage

Bases: MessageDetail[SubscriptionExtra]

class SystemMessage

Bases: MessageDetail[SystemExtra]

Message Extra

class Extra
chat_type: str
emojis: Any | None
os_type: Literal['PC', 'AOS', 'IOS']
streaming_channel_id: str
class NoticeExtra

Bases: Extra

register_profile: Profile
class ChatDonationExtra

Bases: ChatDonation

class VideoDonationExtra

Bases: VideoDonation

class MissionDonationExtra

Bases: MissionDonation

class SubscriptionExtra

Bases: ExtraBase

month: int
nickname: str | None
tier_name: str
tier_no: int | None
class SystemExtra

Bases: ExtraBase

description: str
params: SystemExtraParameter | None
style_type: int
visible_roles: list[str]
class SystemExtraParameter

Bases: ChzzkModel

register_chat_profile: Json[Profile] | None
register_nickname: str
target_nickname: str
target_profile: Json[Profile] | None

Profile

class Profile
activity_badges: list[ActivityBadge]
property badge: Badge | None
property color: str | None
streaming_property: StreamingProperty | None
user_role: UserRole | None
class Badge
image_url: str | None
name: str | None
class ActivityBadge

Bases: Badge

activated: bool
badge_id: str
badge_no: int
description: str | None
class StreamingProperty
property donation_ranking_badge: Badge | None
property following_date: str | None

Recent Chat

This model is used in the on_recent_chat event handler, which contains the historical messages.

class RecentChat
message_list: list[ChatMessage]
notice: NoticeMessage | None
user_count: int | None

Exceptions

The Chat Features exceptions section describes exceptions that can be thrown by ChatClient. Exceptions that occur in the Basic Features exceptions section can also occur.

exception ChatConnectFailed
exception ConnectionClosed
exception WebSocketClosure
exception ReconnectWebsocket