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

理想の自分を忘れないために Slack and LINE bot を作った話 / Make Slack API and LINE bot to not forget ideal myself

C7a2f2f39588f4a71936947a4bf81f1e?s=47 Anntoque
February 27, 2019

理想の自分を忘れないために Slack and LINE bot を作った話 / Make Slack API and LINE bot to not forget ideal myself

C7a2f2f39588f4a71936947a4bf81f1e?s=128

Anntoque

February 27, 2019
Tweet

Transcript

  1. ཧ૝ͷࣗ෼Λ๨Εͳ͍ͨΊʹ
 Slack and LINE bot Λ࡞ͬͨ࿩ @YASU11552288

  2. ΤϯδχΞ=੒௕ͷ৬ۀ

  3. ʮΤϯδχΞʯͱͯ͠੒௕͢Δ
 ͨΊʹ΍Βͳ͖Ό͍͚ͳ͍͜ͱ 
 ʢݸਓͷݟղʣ - ࠷৽৘ใͷΩϟονΞοϓ - ษڧձ΁ͷࢀՃ - Ξ΢τϓοτʢLT,

    ϒϩά etcʣ - ίʔυΛॻ͘
  4. ΤϯδχΞͱͯ͠੒௕΋͍ͨ͠ ΤϯδχΞҎ֎ͷ໘Ͱ΍Γ͍ͨ͜ͱ΋͋Δ ʢීஈͷۀ຿΋΋ͪΖΜ͋Δʣ

  5. ཧ૝ ଉൈ͖ VS VS ࢓ࣄ Lv 99 Lv 99 Lv

    10
  6. ໨ͷલͷ࢓ࣄͰ๩ࡴ… ଵଦͷ༠࿭ʹෛ͚ͯ͠·͏… Ͳ͏͢Δ͔

  7. ཧ૝Λҙࣝ͢Δ࣌ؒΛ૿΍͢ - ࢓ࣄ͸1೔8࣌ؒඞͣҙࣝ͢Δ - ଵଦ͸ৗʹແҙࣝʹߦ͏ - ཧ૝͚͕ͩෆఆظͰνΣοΫϙΠϯτ͕ͳ͍

  8. ʮΤϯδχΞʯͱͯ͠੒௕͢Δ
 ͨΊʹ΍Βͳ͖Ό͍͚ͳ͍͜ͱ 
 ʢݸਓͷݟղʣ - ࠷৽৘ใͷΩϟονΞοϓ - ษڧձ΁ͷࢀՃ - Ξ΢τϓοτʢLT,

    ϒϩά etcʣ - ίʔυΛॻ͘
  9. 1೔ͷऴΘΓʹίʔυΛॻ͍͔ͨ ࣗ෼ʹ໰͏࢓૊ΈΛ࡞Ζ͏ʂʂ

  10. ཁ݅ఆٛ - ίʔυΛॻ͍ͯΔ͔Λ൑அ - ࢦఆ࣌ࠁʹνΣοΫ - ॻ͍͍ͯΔ৔߹͸๙ΊΔ ɹʮຖ೔1commit…ͦͷੵΈॏͶ͕େࣄʂ͓ർΕ༷ࣗ෼ʂʯ - ॻ͍͍ͯͳ͍৔߹͸࠵ଅ

    ɹʮͳͥcommit͠ͳ͍͍͍͍͍ʂࠓ͙͢͠Ζ͓͓͓͓͓ʂʂ
  11. ୈ1ষ shell
 GitHub × Slack API ×EC2 - GitHubͰͦͷ೔event͕ൃੜͯ͠Δ͔֬ೝ -

    ௨஌ର৅͸Slackʹ - ࣮ߦ؀ڥ͸EC2ͷt2.microͰ
  12. Github API ͔ΒeventsΛऔಘ LATEST_COMMIT_DATE=`curl -u :${ACCESS_TOKEN} https:// api.github.com/users/anntoque/events | jq

    '.[0] | select(. ["type"]=="PushEvent")' | jq '.["created_at"]' | cut -c 2-11`
  13. Slack ʹ௨஌͢Δʹ͸Incoming Webhook͕ศར 4FDSFU

  14. Incoming WebhookͰ
 Slack ΁ϝοηʔδΛૹ৴ curl -X POST --data-urlencode "payload={\"channel\": \"#test-

    webhook\", \"username\":\"webhookbot\",\"text\": \"$ {COMMITED_MESSAGE}\", \"icon_emoji\": \":man-lifting-weights:\"}" ${WEBHOOK_URL} ※ COMMITED_MESSAGE = ૹ৴͢Δϝοηʔδͷ಺༰
  15. ͜Μͳײ͡Ͱ௨஌͕དྷΔ

  16. ൃੜͨ͠՝୊ - SlackΛ։͔ͳ͍ͷͰ௨஌ʹؾ͔ͮͳ͍ - EC2ͷ੥ٻ͕݄1000ԁ͘Β͍ - ։ൃ؀ڥͷmacͱec2ͷcentosͷshell ͷҧ͍ʹΑΓඍົʹdateͷڍಈ͕ҧ͏ ※1 ˞.BDͰEBUFίϚϯυ͕ҧ͏݅ʹ͍ͭͯcגࣜձࣾཾ໺৘ใγεςϜ

    IUUQTUBUTVOPTZTUFNDPKQ NBD&"EBUF&#&&&#&&$&&&### &"#&"&&"
  17. ୈ2ষ python(Flask)
 GitHub × LINE Message API ×Heroku - ௨஌ΛSlack͔Βੜ׆ʹ਎ۙͳLINEʹ

    - AWS͔ΒແྉͷHerokuʹҠߦ
 (lamdaͱ͔ECRͷଘࡏΛ஌͍ͬͯͨΒͦͬͪ΋͔͋ͬͨ΋) - shell͔Βςετ΋ॻ͖΍͍͢pythonʹҠߦ
  18. Github API ͔ΒeventsΛऔಘ ʢpythonʣ headers = {'Authorization': 'token ' +

    github_token} def get_github_events(): response = requests.get( 'https://api.github.com/users/anntoque/events', headers=headers) return response.json()
  19. LINE DevelopersͰMessaging APIΛ࢖༻͢ΔΞϓϦΛొ࿥

  20. ϙνϙν͢ΔͱΞΫηετʔ ΫϯͳͲ͕खʹೖΔ 4FDSFU

  21. LINE Message APIͰ
 ϝοηʔδΛૹ৴ʢFlaskʣ @handler.add(MessageEvent, message=TextMessage) def handle_message(event): events =

    get_github_events() last_commit_date = get_last_commit(events) message_text = fix_message(last_commit_date) line_bot_api.reply_message( event.reply_token, TextSendMessage(text=message_text))
  22. Herokuʹ͸؀ڥม਺ͷηοτ 4FDSFU

  23. GitHubϦϙδτϦͱͷ
 ࿈ܞΛઃఆ

  24. ఆظ࣮ߦͷͨΊͷHeroku SchedulerΛ௥Ճ

  25. ൃੜͨ͠՝୊ᶃ - ࣗ෼ͰϝοηʔδΛඈ͹͞ͳ ͍ͱ൓Ԡ͠ͳ͍ػೳΛ࡞ͬͯͨ

  26. LINE Message APIͰϝοηʔδΛૹ৴ʢվʣ - APIͰ͋Δඞཁͳ͍ͷͰFlaskഉআ - reply_messageͰ͸push_messageΛ࢖༻ def main(): events

    = get_github_events() last_commit_date = get_last_commit(events) message_text = fix_message(last_commit_date) line_bot_api.push_message( LINE_USER_ID, TextSendMessage(text=message_text))
  27. ൃੜͨ͠՝୊ᶄ - ࣮ߦΛ1೔1ճʹઃఆͨ͠ͷʹ 1೔10ճ͘Β͍௨஌͕དྷΔڪා

  28. None
  29. Herokuʹ͸Procfileͱݺ͹ΕΔαʔόىಈ࣌ʹ࣮ߦ͢ΔίϚ ϯυΛఆٛ͢ΔϑΝΠϧ͕͋Δ Procfile+Heroku SchedulerͰ࣮ߦ͕ॏෳ͍ͯͨ͠ ͱΓ͋͑ͣProcfileΛద౰ͳ࣮ߦʹมߋ # มߋલ web: python commit-gypsum.py

    # มߋޙ web: python --version
  30. Α͏΍͘׬੒

  31. ݁Ռຖ೔ίʔυΛॻ͍ͨͷ͔…

  32. TMBDL ਖ਼݄ -*/& ˞࣌఺

  33. None
  34. TMBDL ਖ਼݄ -*/& ˞࣌఺ Ͳ͏͍͏͜ͱ͔ QZUIPO
 Ҡߦظؒ ผͷίʔυ ˞ࣗવݴޠॲཧຊϊοΫ ͳΜ͔


    ΍ΔؾͰ ͳ͔ͬͨ
  35. ͭ·ΓɺΞϓϦέʔγϣϯ͕ ׬੒ͨ͠ͷ͕ࠓिͰ ·ͩӡ༻ͯ͠਺೔Ͱ͢ʂ

  36. ·ͱΊ ࠓޙͷల๬ - όον࣮ߦ೔࣌΍commit৘ใͳͲ৘ใΛ΋͏গ͠଍͍ͨ͠ - commit͍ͯ͠ͳ͍৔߹͸issueͷϦετΛૹ৴͢ΔͳͲ͍ͨ͠ - ςετॻ͘ ײ૝ -

    ຖ೔commit͢ΔͨΊͷΞϓϦΛ࡞ΔͨΊʹຖ೔ commitͯͨ͠www - bot͸؆୯ͳίʔυͰ૊ΊΔ͠ɺϥΠϑϋοΫ͠΍͍͢
 - ॳΊͯࣗ෼ͷͨΊͷΞϓϦέʔγϣϯΛ࡞͚ͬͨͲϞνϕʔγϣϯ͕ҧͬͨ - ࠷ޙʹࣗռ΋ࠐΊͯҎԼͷݴ༿Λ…
  37. – Leonardo da Vinci “Iron rusts from disuse; water loses

    its purity from stagnation ... even so in inaction sap the vigor of the mind.” “మ΋࢖Θͳ͍ͱḊͼΔɻਫ΋ྲྀΕͳ͚Ε͹ཎΉɻ ଵଦ΋·ͨɺ৺ͷ׆ྗΛୣͬͯ͠·͏ɻ” -FPOBSEPEB7JODJ*SPOSVTUTGSPNEJTVTFXBUFSMPTFTJUT IUUQTXXXCSBJOZRVPUFDPNRVPUFTMFPOBSEP@EB@WJODJ@
  38. ࢀߟࢿྉ // anntoque/commit-gypsum https://github.com/anntoque/commit-gypsum // LineBot(MessageApi)Λࢼͯ͠ΈΔ-heroku൛ - Qiita https://qiita.com/skycat_me/items/9f27cbd9354515df744a //

    line-bot-sdk-pythonΛ࢖ͬͯΈͨ - ࡶ৯೔ࢽ https://keinumata.hatenablog.com/entry/ 2018/05/08/122348