golangで作るslackbot

 golangで作るslackbot

C98c481072520f41fed66ddd1b538cb5?s=128

Takumi Yoshida

September 01, 2017
Tweet

Transcript

  1. golangͰ࡞Δslack bot @y_takky2014

  2. • εϥΠυதͷGopher͸
 Renée FrenchʹΑͬͯσβΠϯ͞Ε·ͨ͠
 http://reneefrench.blogspot.com/ • εϥΠυ্ͷ GopherͷҰ෦ը૾͸ @tenten͞Μͷ gopher-stickersΛ࢖༻͍ͯ͠·͢

    
 (https://github.com/tenntenn/gopher-stickers)
  3. ΞδΣϯμ • ͳͥ golang ͰBotΛ࡞Δͷ͔ • golangͰͷSlackBotͷ࣮૷ํ๏(Sample) • ※SampleϓϩάϥϜ͸طଘͷBotΛݩʹ
 ۀ຿࣌ؒ֎Ͱͬ͘͞ͱ࡞ͬͨ΋ͷ

  4. SlackBotͷ࡞Γํ • hubot
 • Botkit • API (Rest / WebSocket)

  5. hubot • botΛ࡞੒͢ΔͨΊͷϑϨʔϜϫʔΫ • Adapterͷ੾Γସ͑Ͱͦͷ··πʔϧͷҠߦ͕Մೳ • coffeescript

  6. botkit • ಛఆͷαʔϏε(slack΍Facebook)ʹಛԽͨ͠
 Bot࡞੒ϑϨʔϜϫʔΫ • Interactive Button͕ѻ͍΍͍͢ • Node.js

  7. API (Rest / WebSocket) • Slack͕ఏڙ͍ͯ͠ΔAPIͰϝοηʔδΛૹ৴͢Δ • API͕ఏڙ͞Ε͍ͯΕ͹ͳΜͰ΋Ͱ͖Δ • ͍ΘΏΔWebAPIϕʔεͷ΋ͷͱ


    WebSocketϕʔεͷ΋ͷͷ2छྨ • ࣮૷ݴޠ໰Θͣ • ࠓճ͸golang
  8. ͳͥgolangͳͷ͔? • ίϯύΠϧ͠ͱ͚͹࣮ߦ؀ڥ໰Θͣ • ଞγεςϜͱ࿈ܞ͢Δ࣌ʹ࣮૷͠΍͍͢

  9. ४උ • https://api.slack.com/bot-users ͷ
 new bot user integration͔Βslack botΛ࡞੒͢Δ •

    API_TOKEN͸ϝϞ͓ͬͯ͘ • botͷը૾͸͓޷ΈͰ
  10. ४උ • ඞཁͳIDΛऔಘ͓ͯ͘͠ • channel IDͷऔಘ • https://api.slack.com/methods/channels.list/test • BotIDͷऔಘ

    • https://api.slack.com/methods/users.list/test
  11. golangͰͲ͏࣮૷͢Δ͔ • https://github.com/nlopes/slack Λ࢖͏ • Installing • go get -u

    github.com/nlopes/slack
  12. 1. ఆظతʹϝοηʔδΛૹ৴͢Δ • github.com/robfig/cronΛ࢖͏ • golang্Ͱఆظ࣮ߦΛαϙʔτ͢ΔϥΠϒϥϦ

  13. // slack ClientΛͭ͘Δ api : = slack.New(API_TOKEN)
 // slack Real

    Time Messaging APIΛ࢖͏ rtm := api.NewRTM()
 // webSocketΛฒྻͰ࣮ߦ͢Δ go rtm.ManageConnection() 
 // cronΛ࡞੒͢Δɻ 5෼ຖʹRTM APIͰϝοηʔδΛಛఆCHANNEL_IDʹૹΔ c := cron.New() c.AddFunc(“0 */5 * * * *”, func () {rtm.SendMessage(rtm.NewOutgoingMessage(“test”, CHANNEL_ID))}) c.Start() 

  14. None
  15. 2. ϝϯγϣϯʹ൓Ԡ͢Δ

  16. • 1ͱಉ༷ʹSlack Client / RTMͷwebsocketΛฒྻ࣮ߦ • ແݶϧʔϓʹͯ͠ɺແݶϧʔϓ಺ͰSlackͰड͚औͬͨΠϕϯτ ຖʹॲཧΛ෼ذ • Message

    EventͩͬͨΒɺMessageͷ಺༰Λݟͯ
 @BotIDʹҰக͢Δ΋ͷͩͬͨΒॲཧΛଓߦ • botUserΛ࡞Δ • Message Event಺ͷൃݴऀͷID͔ΒNameΛऔಘͯ͠
 ड͚औͬͨϝοηʔδΛͦͷ··ฦ٫

  17. None
  18. 3. ఱؾ༧ใػೳΛ෇͚Δ • ϝϯγϣϯʹ൓Ԡͯ͠ఱؾΛڭ͑ͯ͘ΕΔΑ͏ʹ͢Δ • livedoorͷఏڙ͍ͯ͠ΔWeather Hacks APIΛ࢖༻͢Δ

  19. APIୟ͍ͨ݁ՌΛ࢖͍΍͍͢ܗʹ੔ܗͯ͠ฦ٫͢Δ

  20. • @bot ఱؾ ͷ৔߹ఱؾΛڭ͑ͯ͘ΕΔΑ͏ʹ͢Δ • ఱؾAPIΛୟ͍ͨ݁Ռͷmap(࿈૝഑ྻ)Λճͯ͠
 ݁ՌΛPost

  21. None
  22. ·ͱΊ • golangͰͷbot࣮૷ྫΛ঺հ • golang͸ϥΠϒϥϦ͕ଟʑ͋ΔͷͰ
 ֎෦APIͱͷܨ͗ࠐ͕͔ͳΓָ • ֎෦ίϚϯυ࣮ߦ౳΋΍Γ΍͍͢

  23. ࢀߟ • golang Ͱ࢝ΊΔ Slack bot ։ൃ • http://blog.kaneshin.co/entry/2016/12/03/162653 •

    GolangͰSlack Interactive MessageΛ࢖ͬͨBotΛॻ͘ • http://tech.mercari.com/entry/2017/05/23/095500 • Gopher Stickers • https://github.com/tenntenn/gopher-stickers
  24. • ΦϦδφϧͷGopher͸ Renee FrenchʹΑͬͯσβΠϯ ͞Ε·ͨ͠ɻ • εϥΠυ্ͷ GopherͷҰ෦ը૾͸ @tenten͞Μͷ gopher-stickersΛ࢖༻͍ͯ͠·͢

    
 (https://github.com/tenntenn/gopher-stickers)