Skip to content

Commit

Permalink
Merge pull request #2 from d2n0s4ur/release/1.2.0
Browse files Browse the repository at this point in the history
Release/1.2.0
  • Loading branch information
d2n0s4ur authored Feb 13, 2024
2 parents 327e607 + dc4228a commit 1a4c810
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 53 deletions.
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
[![Issues](https://img.shields.io/github/issues/d2n0s4ur/chzzk-chat.svg?style=flat)](https://github.com/d2n0s4ur/chzzk-chat)
[![Node version](https://img.shields.io/node/v/@d2n0s4ur/chzzk-chat)](https://www.npmjs.com/package/@d2n0s4ur/chzzk-chat)



<img alt="chzzk_logo" src="./img/chzzk_logo.svg" style="width: 40%">

Javascript를 통해 치지직 채팅을 크롤링하는 코드입니다.

# Install


## Node

### NPM

```bash
$ npm install @d2n0s4ur/chzzk-chat
```

### Yarn

```bash
$ yarn add @d2n0s4ur/chzzk-chat
```
Expand All @@ -33,19 +32,49 @@ $ yarn add @d2n0s4ur/chzzk-chat
import { ChzzkChat } from "@d2n0s4ur/chzzk-chat";

const chzzkChat = new ChzzkChat("YOUR_CHZZK_USER_HASH");
// ex) CHzzkchat('dfffd9591264f43f4cbe3e2e3252c35c)
// ex) ChzzkChat('dfffd9591264f43f4cbe3e2e3252c35c')
```

### 1. Add message Handler

```typescript
const messageHandler: messageHandler = (
badges: string[],
nick: string,
message: string,
isDonation: boolean,
donationAmount?: number
message: string
) => {
console.log(
`${nick}: ${message} ${isDonation ? `(${donationAmount}원)` : ""}`
);
console.log(`${nick}: ${message}`);
};

chzzkChat.addMessageHandler(messageHandler);
```
```

### 2. Add donation Handler

```typescript
const donationHandler: donationHandler = (
badges: string[],
nick: string,
message: string,
isAnonymous: boolean,
amount: number
) => {
if (!isAnonymous) {
console.log(`${nick}님이 ${amount}원을 후원했습니다: ${message}`);
} else {
console.log(`익명의 후원자가 ${amount}원을 후원했습니다: ${message}`);
}
};

chzzkChat.addDonationHandler(donationHandler);
```



사용이 끝난 후, `chzzkChat.close()`를 호출하여 웹소켓을 닫아주세요.

After using the library, please call `chzzkChat.close()` to close the websocket.

```typescript
chzzkChat.close();
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@d2n0s4ur/chzzk-chat",
"version": "1.1.1",
"description": "chzzk-chat: 자바스크립트 개발자를 위한 chzzk 채팅 비공식 라이브러리",
"version": "1.2.0",
"description": "chzzk-chat: node.js 개발자를 위한 chzzk 채팅 & 후원 비공식 라이브러리",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ChzzkChat } from "./lib/chzzkChat";
export type { messageHandler } from "./lib/chzzkChat";
export type { messageHandler, donationHandler } from "./lib/chzzkChat";
86 changes: 47 additions & 39 deletions src/lib/chzzkChat.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
export type messageHandler = (
badges: string[],
nick: string,
message: string
) => void;

export type donationHandler = (
badges: string[],
nick: string,
message: string,
isDonation: boolean,
donationAmount?: number
isAnonymous: boolean,
amount: number
) => void;

const chzzkIRCUrl = "wss://kr-ss3.chat.naver.com/chat";

export class ChzzkChat {
private initialization;
ws: WebSocket | undefined;
messageHandler: messageHandler;
messageHandler: messageHandler | undefined;
donationHandler: donationHandler | undefined;
chzzkChannelId: string;
chatChannelAccessToken: string;
chatChannelId: string;
sid: string;
uuid: string;

init = async () => {
this.chatChannelId = await this.getChatChannelId(this.chzzkChannelId);
this.chatChannelAccessToken = await this.getChatChannelAccessToken(
this.chatChannelId
);

this.ws = new WebSocket(chzzkIRCUrl);

this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onclose = this.onClose.bind(this);
this.ws.onerror = this.onError.bind(this);
};

constructor(chzzkChannelId: string) {
this.chzzkChannelId = chzzkChannelId;
this.chatChannelId = "";
this.chatChannelAccessToken = "";
this.sid = "";
this.uuid = "";
this.initialization = this.init();
}

addMessageHandler = (handler: messageHandler) => {
this.messageHandler = handler;
};

addDonationHandler = (handler: donationHandler) => {
this.donationHandler = handler;
};

getChatChannelId = async (chzzkChannelId: string) => {
const url = `https://api.chzzk.naver.com/polling/v2/channels/${chzzkChannelId}/live-status`;

Expand All @@ -47,31 +81,6 @@ export class ChzzkChat {
});
};

init = async () => {
this.chatChannelId = await this.getChatChannelId(this.chzzkChannelId);
this.chatChannelAccessToken = await this.getChatChannelAccessToken(
this.chatChannelId
);

this.ws = new WebSocket(chzzkIRCUrl);

this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onclose = this.onClose.bind(this);
this.ws.onerror = this.onError.bind(this);
};

constructor(chzzkChannelId: string) {
this.chzzkChannelId = chzzkChannelId;
this.chatChannelId = "";
this.chatChannelAccessToken = "";
this.sid = "";
this.uuid = "";
// this.ws = undefined;
this.messageHandler = () => {};
this.initialization = this.init();
}

onOpen(event: Event) {
if (!this.ws) return;
console.log("Connected to Chzzk IRC");
Expand Down Expand Up @@ -108,25 +117,24 @@ export class ChzzkChat {
data.bdy.forEach((msg: any) => {
const profile = JSON.parse(msg.profile);
switch (data.cmd) {
case 93101:
case 93101: // default message
if (!this.messageHandler) return;
this.messageHandler(
this.parseBadgeUrl(profile.activityBadges as string[]),
profile ? profile.nickname : "익명",
msg.msgTypeCode === "CBOTBLIND"
profile.nickname,
msg.msgStatusType === "CBOTBLIND"
? "클린봇에 의해 삭제된 메시지입니다."
: msg.msg,
false
: msg.msg
);
break;
case 93102:
case 93102: // donation message
const extras = JSON.parse(data.bdy[0].extras);
if (!this.messageHandler) return;
this.messageHandler(
[],
profile ? profile.nickname : "익명",
if (!this.donationHandler) return;
this.donationHandler(
this.parseBadgeUrl(profile.activityBadges as string[]),
msg.uid !== "anonymous" ? profile.nickname : "익명의 후원자",
msg.msg,
true,
msg.uid === "anonymous",
extras.payAmount
);
break;
Expand Down

0 comments on commit 1a4c810

Please sign in to comment.