• 레퍼런스
  • 자습서
  • 이벤트 리스너 수정

    우선, 레거시 API의 이벤트 리스너를 API2의 이벤트 로직으로 수정해야 합니다. 레거시 API와 달리, API2는 이벤트 리스너의 등록 및 삭제를 프로그래머가 직접 관리하는 구조입니다. 그러므로 기존 이벤트 리스너를 직접 등록해야 합니다.

    Bot 인스턴스 가져오기

    API2에서는 BotManager.getCurrentBot() 메소드를 이용해 해당 스크립트에 대응하는 Bot 인스턴스를 가져올 수 있습니다. Bot 객체는 카카오톡봇을 제어하는 핵심 객체입니다.

    스크립트의 최상단에 아래 코드를 추가합니다.

    const bot = BotManager.getCurrentBot();

    상수의 이름은 원하는 대로 설정해도 무관합니다. 해당 가이드에서는 bot 상수에 Bot 인스턴스를 할당하겠습니다.

    이벤트 리스너 등록하기

    우선, 레거시 API의 각 이벤트 리스너가 API2의 어떤 이벤트에 대응하는지 알아야 합니다.

    레거시 API의 이벤트 리스너에 대응하는 API2의 이벤트의 이름
    onBackPressed 이벤트 리스너

    Event.Activity.BACK_PRESSED = "activityBackPressed"

    onCreate 이벤트 리스너

    Event.Activity.CREATE = "activityCreate"

    onDestroy 이벤트 리스너

    Event.Activity.DESTROY = "activityDestroy"

    onNotificationPosted 이벤트 리스너

    Event.NOTIFICATION_POSTED = "notificationPosted"

    onNotificationRemoved 이벤트 리스너

    Event.NOTIFICATION_REMOVED = "notificationRemoved"

    onPause 이벤트 리스너

    Event.Activity.PAUSE = "activityPause"

    onRestart 이벤트 리스너

    Event.Activity.RESTART = "activityRestart"

    onResume 이벤트 리스너

    Event.Activity.RESUME = "activityResume"

    onStart 이벤트 리스너

    Event.Activity.START = "activityStart"

    onStartCompile 이벤트 리스너

    Event.START_COMPILE = "startCompile"

    onStop 이벤트 리스너

    Event.Activity.STOP = "activityStop"

    response 이벤트 리스너

    Event.MESSAGE = "message"

    이벤트 리스너의 등록은 Bot.addListener() 또는 Bot.on() 메소드를 사용합니다. 사용법은 아래와 같습니다.

    bot.addListener(eventName, listener);
    // 또는
    bot.on(eventName, listener);

    예를 들어, onStartCompile 이벤트 리스너를 등록하려면 아래와 같이 작성합니다.

    // 기존 레거시 API 코드에 존재한 onStartCompile 이벤트 리스너
    function onStartCompile() {
      // ...
    }
    
    // API2에서의 정상적인 동작을 위해 이벤트 리스너를 등록합니다.
    // 아래 4가지 코드 중 하나를 선택하여 사용합니다.
    bot.addListener(Event.START_COMPILE, onStartCompile);
    bot.addListener("startCompile", onStartCompile);
    bot.on(Event.START_COMPILE, onStartCompile);
    bot.on("startCompile", onStartCompile);

    레거시 API의 이벤트 리스너의 매개변수는 API2의 이벤트 리스너의 매개변수와 호환됩니다. 하지만 response 이벤트 리스너의 매개변수는 API2의 message 이벤트의 리스너와 호환되지 않습니다.

    response 함수의 인자 수정하기

    response 이벤트 리스너의 인자는 API2의 message 이벤트 리스너와 호환되지 않습니다. response 이벤트 리스너의 경우 방 이름, 메시지의 내용 등의 값을 독립된 매개변수로 관리하지만, API2의 message 이벤트는 msg 라는 하나의 매개변수로 모든 정보를 관리합니다. 그러므로 response 이벤트 리스너의 인자를 아래와 같이 수정합니다.

    function response(msg) {
      // 기존 레거시 API 코드
    }
    bot.addListener(Event.MESSAGE, response);

    해당 가이드에서는 어떤 이벤트에 대한 리스너인지 더 명확하게 하기 위해 response 함수의 이름을 onMessage로 변경하겠습니다.

    function onMessage(msg) {
      // 기존 레거시 API 코드
    }
    bot.addListener(Event.MESSAGE, onMessage);

    이제 기존 코드에 남아있는 response 이벤트 리스너의 각 매개변수를 대응하는 API2 message 이벤트의 매개변수 msg의 프로퍼티 및 메소드로 수정해야합니다. 아래 표를 참고해 수정하세요.

    response 이벤트 리스너의 매개변수에 대응하는 message 이벤트의 매개변수
    room

    msg.room

    msg

    msg.content

    sender

    msg.author.name

    isGroupChat

    msg.isGroupChat

    replier.reply()

    msg.reply()

    replier.markAsRead()

    msg.markAsRead()

    imageDB.getImage()
    imageDB.getImageBase64()

    msg.image.getBase64()

    imageDB.getImageBitmap()

    msg.image.getBitmap()

    packageName

    msg.packageName

    isMention

    msg.isMention

    logId

    msg.logId

    channelId

    msg.channelId

    userHash

    msg.author.hash

    정보

    message 이벤트의 매개변수에 대한 더 자세한 정보는 여기를 참고하세요.