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

CrystalでTwitter botを作ってみた / Create a Twitter bot in Crystal

CrystalでTwitter botを作ってみた / Create a Twitter bot in Crystal

東京 Crystal 勉強会 #1 in 五反田

C0e58802a4228552548bb8ca596ca966?s=128

Mihyaeru

July 31, 2015
Tweet

Transcript

  1. CrystalͰTwitter botΛ࡞ͬͯΈͨ ౦ژ Crystal ษڧձ #1 in ޒ൓ా @mihyaeru21 

  2. © 2015 Mihyaeru ͓લͩΕΑ • ϛώϟΤϧ @mihyaeru21 • ࠷ۙ͸SwiftͱPerlΛ൒ʑͰ •

    ͨ·ʹPython੎ • YAPCͰొஃ͢Δ͜ͱʹͳͬͯΨΫϒϧ 2
  3. © 2015 Mihyaeru ࡞ͬͯΈͨܥLT • ܀΅ͬͱ https://twitter.com/crybot21 • https://github.com/mihyaeru21/crybot •

    UserStreamʹܨ͍Ͱϝϯγϣϯͱಉ͡಺༰Λݴ ͍ฦ͚ͩ͢ • ϩʔΧϧͰಈ͔ͯ͠Δ͔Β͙͢ಈ͔ͳ͘ͳΔӡ໋ 3
  4. © 2015 Mihyaeru ΊͬͪΌ͠ΐ΅͍σϞ 4

  5. ݴ͍ฦ͚ͩ͢Ͱͨ͠ 

  6. © 2015 Mihyaeru CrystalΛ࢖ͬͯΈͨײ૝ • ·ΔͰLLΛॻ͍͍ͯΔײ֮ • ๛෋ͳඪ४Ϟδϡʔϧ͕͋ͬͯศར • Streaming

    API͸ॳΊ࣮ͯ૷͚ͨ͠ͲͳΜͱ͔ ͳͬͨ 6
  7. © 2015 Mihyaeru CrystalΛ࢖ͬͯΈͨײ૝ • ϥΠτʹ͔͚Δ͚Ͳ΋ɺܕͷѻ͍͸ϥΠτͰ ͸ͳ͔ͬͨ • ٽ͘ٽ͘௿Ϩϕϧͳͱ͜ΖΛॻ͍ͨ •

    υΩϡϝϯτ͸ιʔείʔυ • RubyΛ͋·Γ஌Βͳ͔͔ͬͨΒײͨ͡೉͠͞ 7
  8. © 2015 Mihyaeru ࢖ͬͨඪ४Ϟδϡʔϧ • cgi • env • http

    • json • oauth • openssl • secure_random • socket • time 8
  9. © 2015 Mihyaeru cgi • URLΤϯίʔυʹ࢖༻ • cgiۭؒʹ͋ͬͨͷ͔ɻɻɻ • uriͱ͔httpͱ͔Λݟ͍ͯͨ

    9
  10. © 2015 Mihyaeru env • ؀ڥม਺ͷಡΈࠐΈ • access tokenͱ͔ͷಡΈࠐΈʹ࢖༻ •

    ී௨ʹHashͬΆ͘࢖͑ͨ 10
  11. © 2015 Mihyaeru http (./client ./request) • ΧδϡΞϧʹ࢖͑Δ • ௨৴ͷ్தͰίʔϧόοΫͱ͔ʹσʔλΛྲྀ

    ͯ͘͠ΕΔ΍͕ͭ(ݟͨݶΓ͸)ແ͔ͬͨ • υΩϡϝϯτ͔ΒભҠ͢Διʔείʔυ͕Ϧ Ϧʔε൛ΑΓ৽ͯ͘͠ϋϚͬͨ 11
  12. © 2015 Mihyaeru json • ͱΓ͋͑ͣ JSON.parse • σίʔυ݁Ռ͸͍͢͝ܕʹͳ͍ͬͯͯฦΓ஋ ͷѻ͍͕஍ຯʹ೉͔ͬͨ͠

    12
  13. © 2015 Mihyaeru oauth • ௨৴·Ͱ΍ͬͯ͘ΕΔ͔ͱࢥ͚ͬͨͲҧͬͨ • ࢖ͬͨͷ͸ OAuth::Signature •

    ໘౗ͳAuthϔομΛͺͬͱ࡞Ε͍͍ͯײ͡ • খճΓ͕ޮ͘ײ 13
  14. © 2015 Mihyaeru openssl • OpenSSL::SSL::Socket • SSL༻ʹTCPSocketΛแΈࠐΜͰ͚͋͛ͨͩ 14

  15. © 2015 Mihyaeru secure_random • nonceΛ࡞Δͷʹར༻ • SecureRandom.hex(32) ͰɺͱΓ͋͑ͣ32ܻ ͷ16ਐ਺จࣈྻ͕ಘΒΕΔ

    • nonceʹ͸UUIDͷϋογϡΛ࢖͓͏ͱࢥͬͯ ͚ͨͲUUIDϞδϡʔϧ͕ແ͔ͬͨ 15
  16. © 2015 Mihyaeru socket • TCPSocket • ίωΫγϣϯΛுΓͬͺͳ͠ͰಡΈଓ͚Δ෦ ෼Ͱ࢖༻ •

    ൺֱత௿Ϩϕϧͳ෦෼͚ͩͲ৮Γ΍͔ͬͨ͢ 16
  17. © 2015 Mihyaeru time • Time.utc_now.to_i ͚͓͚֮ͩ͑ͯ͹
 ͻͱ·ͣੜ͖͍͚ͯͦ͏ • Time.now.to_i

    ͬͯॻ͍ͯͯϋϚͬͨ 17
  18. ·ͱΊ • ඪ४ϥΠϒϥϦ͕ॆ࣮Ͱऔͬ෇͖΍͍͢ • ϥΠτͳॻ͖৺஍͕ྑ͍ײ͡ • ιʔεՇ