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

Pythonで作る簡単LINEボット

4f89bc9d32377699f88c7be3d7855513?s=47 Yuya Oka
July 06, 2018
280

 Pythonで作る簡単LINEボット

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

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

4f89bc9d32377699f88c7be3d7855513?s=128

Yuya Oka

July 06, 2018
Tweet

Transcript

  1. PythonͰ࡞Δ ؆୯LINEϘοτ খઘͻ΍͔͠

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

    Ӊ෦ߴઐܦӦ৘ใֶՊଔ(2015೥౓) w Kibousoft LLC.ݸਓࣄۀओ • iOS apps Developer, Python Developer(ServerSide) w 1ਓσʔτͷϓϩ(ྫ1ਓUSJ)
  3. 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人デートのプロ'
  4. B T W

  5. LINEϘοτ ࡞ͬͨ͜ͱ͋Δʁ

  6. ͋Δਓ͸ 「あーそんなんあったね」 ͬͯײ͡Ͱ৸ͯͯ΋͍͍Α

  7. ·ͨ͸ https://developers.line.me/en/docs/messaging- api/reference/ ެࣜϦϑΝϨϯεͰ΋Ͳ͏ͧ

  8. ڵຯͳ͍ਓ͸৸ͯͯ

  9. ಋೖ

  10. w Python3.6.xΛΠϯετʔϧ(3.7.0Ͱ΋ಈ͘ͱࢥ͏) w Python2͸ͬ͞͞ͱࣺͯΔ (஗͍ɺϨΨγʔɺ౳) w line-bot-sdkͷΠϯετʔϧ (ެࣜSDK, ݱࡏ1.7.1͕࠷৽) w

    Flask, Bottle, Pyramid, Django౳ͷΠϯετʔϧ
  11. $ pip install line-bot-sdk

  12. $ pip install flask

  13. $ pip install bottle

  14. $ pip install django

  15. すきなものをいれて ࠓճ͸7/18ʹొஃ͢ΔΠϕϯτ༻ʹ ࡞ΓࠐΈͯ͠͠·ͬͨͷͰDjangoͰͷ࣮૷ʹͳΓ·͢

  16. HTTPSかんきょうないよ

  17. Ngrok

  18. Ҿ༻: https://github.com/inconshreveable/ngrok

  19. $ ngrok http 8000

  20. None
  21. جຊܗ

  22. from django.http.response import HttpResponse, HttpResponseBadRequest from django.views.generic.base import View from

    linebot import LineBotApi, WebhookHandler from linebot.exceptions import InvalidSignatureError
  23. line_bot_api = LineBotApi(‘LINE_BOT_ACCESS_TOKEN’) handler = WebhookHandler('LINE_BOT_ACCESS_SECRET') LINE DevelopersαΠτ͔Βऔಘ

  24. LINE_BOT_ACCESS_SECRET

  25. LINE_BOT_ACCESS_TOKEN

  26. 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)
  27. ソースコード みせられてもわからん

  28. https://github.com/nnsnodnb/ line-bot-django-handle

  29. handler.handle(body, signature) ͜ΕΛ͢Δ͜ͱʹΑͬͯɺҎԼͷEventΛϝιουʹड͚औΕΔ • MessageEvent (ςΩετ΍ը૾ɺಈըɺԻ੠ɺϑΝΠϧɺҐஔ৘ใɺελϯϓ) • FollowEvent (༑ͩͪ௥ՃorϒϩοΫղআ) •

    UnfollwEvent (༑ͩͪϒϩοΫ) • JoinEvent (ΞΧ΢ϯτ͕άϧʔϓʹࢀՃͨ͠) • LeaveEvent (ΞΧ΢ϯτ͕άϧʔϓ͔Βୀग़ͨ͠) • PostbackEvent (Ϙλϯ౳ʹඥ෇͚ΒΕͨΞΫγϣϯ) • BeaconEvent (LINE BeaconͷͦΕ) • AccountLinkEvent (ϓϩόΠμαʔϏεͱLINEΞΧ΢ϯτ͕ඥ෇͚ΒΕͨ)
  30. Α͋͘ΔΦ΢Ϝฦ͠

  31. 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) )
  32. ֬ೝ

  33. ͋ͱͶɻ ࠓճ໘౗͔ͩͬͨΒButtonͱ͔͔͠ରԠͯ͠ͳ͍ͷ ιʔείʔυݟͤͯ΋ͭ·ΒΜ͠ σϞ͚ͩྲྀ͢κΠ

  34. ButtonsTemplate

  35. StickerSendMessage

  36. ImageSendMessage

  37. VideoSendMessage

  38. LocationSendMessage

  39. CarouselTemplate

  40. ImageCarouselTemplate

  41. ͜͜ͷϥούʔͷιʔείʔυ ॻ͍ͨΑ https://github.com/line/line-bot-sdk-python/pull/59

  42. ࣗຫ࿩͸ஔ͍ͱ͍ͯ

  43. ͪΐͬͱԠ༻

  44. Imagemap + AccountLink

  45. ۩ମྫࡾҪॅ༑ۜߦ

  46. None
  47. Ͱ΋͜Ε Not Best Practice #

  48. https://developers.line.me/ ja/docs/messaging-api/ linking-accounts/

  49. None
  50. ͜Ε͸7/18ͷΠϕϯτͰ ొஃͰͨ͠ͱ͖ʹͬ͘͡Γ

  51. None
  52. ͜ͷը໘ʹ ϦμΠϨΫτͤͨ͞Β AccountLinkEvent͕ඈͿ

  53. FreeϓϥϯͰ΋ରԠՄೳ reply_messageͰ͋Ε͹

  54. ͜͜ͷϥούʔͷιʔείʔυ΋ ॻ͍ͨΑ https://github.com/line/line-bot-sdk-python/pull/105

  55. ֤छϓϥϯ಺༰ https://at.line.me/jp/plan

  56. ͓ۚΛ୔ࢁ͘ΕΔ ύύ͕͍Δํ͸ͥͻʂʂ

  57. ҰԠAccountLink ֬ೝ

  58. LINEαʔό͔Βऔಘͨ͠Token͋ΓϦϯΫΛఏࣔ Ϣʔβ͕ϦϯΫΛ౿Ή ϓϩόΠμαʔϏεͰϩάΠϯ͞ΕΔ LINEͷಛఆͷϦϯΫʹϦμΠϨΫτ AccountLinkEventΛड͚औΓฦ৴

  59. ྑ͍BOTϥΠϑΛ