django-rest-kakao-auto-reply

Make you easy to create kakaotalk plusfriend bot based on Django + DRF


Keywords
django-rest-kakao-auto-reply
License
MIT
Install
pip install django-rest-kakao-auto-reply==0.1.6

Documentation

django-rest-kakao-auto-reply

Build Status codecov

์นด์นด์˜คํ†ก ํ”Œ๋Ÿฌ์Šค ์นœ๊ตฌ ์ž๋™์‘๋‹ต API(์ดํ•˜ ์ž๋™์‘๋‹ต API)๋ฅผ Django + DRF๋กœ ๋งŒ๋“ค ๋•Œ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณตํ•˜๋Š” ViewSet๊ณผ Router, Decorator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ์ž๋™์‘๋‹ต API๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ”Œ๋Ÿฌ์Šค์นœ๊ตฌ๋กœ ๋ฌด์—‡์„ ๋งŒ๋“ค์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ์ง‘์ค‘ํ•˜์„ธ์š”.

Requirements

  • Python >= 3.6
  • Django >= 2
  • djangorestframework

Quick Start

  1. ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    (venv) $ pip install django-rest-kakao-auto-reply
  2. KakaoAutoReplyViewSet๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ƒˆ๋กœ์šด ViewSet์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

    from kakao_auto_reply import objects, decorators
    from kakao_auto_reply.viewsets import KakaoAutoReplyViewSet
    from rest_framework.response import Response
    
    class MyViewSet(KakaoAutoReplyViewSet):
        def on_keyboard(self, request, *args, **kwargs):
            data = objects.Keyboard(type='buttons',
                                    buttons=['foo', 'bar'])
    
            return Response(data, 200)
    
        @decorators.on_message_parse
        def on_message(self, request, *args, **kwargs):
            content = kwargs['content']
            message = objects.Message(text='echo: %s' % content)
            keyboard = objects.Keyboard(type='text')
            data = objects.MessageResponse(message, keyboard)
    
            return Response(data, 200)
  3. urls์— KakaoAutoReplyRouter๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ƒˆ๋กœ์šด ๋ผ์šฐํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์œ„์—์„œ ๋งŒ๋“  MyViewSet์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

    from django.urls import path, include
    from kakao_auto_reply.routers import KakaoAutoReplyRouter
    from .viewsets import MyViewSet
    
    router = KakaoAutoReplyRouter()
    router.register('', MyViewSet, base_name='foo')
    
    urlpatterns = [
        path('kakaobot/', include(router.urls)),
    ]
  4. ๋.

Documentation

viewsets

KakaoAutoReplyViewSet

์นด์นด์˜คํ†ก ํ”Œ๋Ÿฌ์Šค์นœ๊ตฌ ์ž๋™์‘๋‹ต API ์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ •๋ฆฌ ํ•ด ๋‘” ViewSet ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ViewSet ์„ ์ƒ์†๋ฐ›์€ ViewSet ์€ ์•„๋ž˜ ๋ช…์‹œ๋œ 5๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • def on_keyboard(self, request, *args, **kwargs):

    • Home Keyboard API์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
  • def on_message(self, request, *args, **kwargs):

    • ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ๋ฐ ์ž๋™์‘๋‹ต API์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. @on_message_parse ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • def on_friend_added(self, request, *args, **kwargs):

    • ์นœ๊ตฌ ์ถ”๊ฐ€ API์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. @on_friend_added_parse ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • def on_friend_deleted(self, request, *args, **kwargs):

    • ์นœ๊ตฌ ์ฐจ๋‹จ API์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. @on_friend_deleted_parse ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • def on_chatroom_leaved(self, request, *args, **kwargs):

    • ์ฑ„ํŒ…๋ฐฉ ๋‚˜๊ฐ€๊ธฐ API์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. @on_chatroom_leaved_parse ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

decorators

@on_message_parse

  • ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ๋ฐ ์ž๋™์‘๋‹ต API๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ Request Body๋กœ ์ „๋‹ฌ ๋œ user_key, type, content๋ฅผ ๊ฐ€์ ธ์™€ **kwargs์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

@on_friend_added_parse

  • ์นœ๊ตฌ ์ถ”๊ฐ€ API๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ Request Body๋กœ ์ „๋‹ฌ ๋œ user_key๋ฅผ ๊ฐ€์ ธ์™€ **kwargs์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

@on_friend_deleted_parse / @on_chatroom_leaved_parse

  • ๊ฐ๊ฐ ์นœ๊ตฌ ์ฐจ๋‹จ/์ฑ„ํŒ…๋ฐฉ ๋‚˜๊ฐ€๊ธฐ API๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ ์ฃผ์†Œ Parameter๋กœ ๋„˜์–ด์˜จ user_key๋ฅผ ๊ฐ€์ ธ์™€ **kwargs์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

objects

์นด์นด์˜คํ†ก ์ž๋™์‘๋‹ต API ๊ตฌํ˜„์— ํ•„์š”ํ•œ Object๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

  • MessageResponse
    • parameter
      • message (required)
      • keyboard
    • example
      MessageResponse(message, keyboard)
      
      """:returns:"""
      {
          'message': message,
          'keyboard': keyboard,
      }
  • Keyboard
    • parameter
      • type (required)
        • text
        • buttons
      • buttons
    • example
      Keyboard(type, buttons)
      
      """:returns:"""
      {
          'type': type,
          'buttons': buttons,
      }
  • Message
    • parameter
      • text
      • photo
      • message_button
    • example
      Message(text, photo, message_button)
      
      """:returns:"""
      {
          'text': text,
          'photo': photo,
          'message_button': message_button,
      }
  • Photo
    • parameter
      • url (required)
      • width (required)
      • height (required)
    • example
      Photo(url, width, height)
      
      """:returns:"""
      {
          'url': url,
          'width': width,
          'height': height,
      }
  • MessageButton
    • parameter
      • label (required)
      • url (required)
    • example
      Photo(label, url)
      
      """:returns:"""
      {
          'label': label,
          'url': url,
      }

routers

์ž๋™์‘๋‹ต API ์ŠคํŽ™์— ๋งž๊ฒŒ ์ž‘์„ฑ๋œ ์ปค์Šคํ…€ ๋ผ์šฐํ„ฐ์ž…๋‹ˆ๋‹ค.

Contributors