Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonで作る簡単LINEボット

Yuya Oka
July 06, 2018
440

 Pythonで作る簡単LINEボット

高専カンファレンスin京都2 のスライドです。
ご清聴ありがとうございました!!

リポジトリはまだPrivateにしてますが、18日になったら公開します。
https://github.com/nnsnodnb/line-bot-django-handle

Yuya Oka

July 06, 2018
Tweet

Transcript

  1. ͓લ୭Α • Yuya Oka (Lv: 22) • SNS: nnsnodnb w

    Ӊ෦ߴઐܦӦ৘ใֶՊଔ(2015೥౓) w Kibousoft LLC.ݸਓࣄۀओ • iOS apps Developer, Python Developer(ServerSide) w 1ਓσʔτͷϓϩ(ྫ1ਓUSJ)
  2. class Speaker(Person): def __init__(self): self.name = 'Yuya Oka' self.level =

    22 self.sns = 'nnsnodnb' self.graduation_school = 'NIT, Ube College' self.jobs = [ 'iOS apps developer', 'Python Developer' ] self.members = [ 'Kibousoft LLC.', ‘Freelance' ] self.other = '1人デートのプロ'
  3. from django.http.response import HttpResponse, HttpResponseBadRequest from django.views.generic.base import View from

    linebot import LineBotApi, WebhookHandler from linebot.exceptions import InvalidSignatureError
  4. class CallbackView(View): def post(self, request, *args, **kwargs): signature = request.META['HTTP_X_LINE_SIGNATURE']

    body = request.body.decode('utf-8') try: handler.handle(body, signature) except InvalidSignatureError: return HttpResponseBadRequest() return HttpResponse(‘OK') @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(CallbackView, self).dispatch(*args, **kwargs)
  5. handler.handle(body, signature) ͜ΕΛ͢Δ͜ͱʹΑͬͯɺҎԼͷEventΛϝιουʹड͚औΕΔ • MessageEvent (ςΩετ΍ը૾ɺಈըɺԻ੠ɺϑΝΠϧɺҐஔ৘ใɺελϯϓ) • FollowEvent (༑ͩͪ௥ՃorϒϩοΫղআ) •

    UnfollwEvent (༑ͩͪϒϩοΫ) • JoinEvent (ΞΧ΢ϯτ͕άϧʔϓʹࢀՃͨ͠) • LeaveEvent (ΞΧ΢ϯτ͕άϧʔϓ͔Βୀग़ͨ͠) • PostbackEvent (Ϙλϯ౳ʹඥ෇͚ΒΕͨΞΫγϣϯ) • BeaconEvent (LINE BeaconͷͦΕ) • AccountLinkEvent (ϓϩόΠμαʔϏεͱLINEΞΧ΢ϯτ͕ඥ෇͚ΒΕͨ)
  6. from linebot.models import MessageEvent, TextMessage class CallbackView(View): ɾɾɾ @staticmethod @handler.add(MessageEvent,

    message=TextMessage) def message_event(event): line_bot_api.reply_message( event.reply_token, TextSendMessage(text=event.message.text) )