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

Go + Google Cloud Functions を使ったSlackのThreadにも対...

Go + Google Cloud Functions を使ったSlackのThreadにも対応したbotを簡単に作る方法

Slack Dev Meetup Tokyo #1 LT 資料です。

ktykogm

July 25, 2019
Tweet

More Decks by ktykogm

Other Decks in Programming

Transcript

  1. XIPBNJ w !LUZLPHN LPHVNB  w ϝϧΧϦ43& w ࠷ۙ(PΛۀ຿Ͱͨ·ʹॻ͍͍ͯΔϨϕϧ w

    ʮ4MBDL (PͳΒͦͷ͘Β͍؆୯ʹԼ४උͰ͖ΔΑʯͱ͍͏͜ͱΛ఻ ͑ʹདྷ·ͨ͠ w 43&-PVOHFͱ͍͏ίϛϡχςΟͷӡӦ׆ಈΛ͍ͯ͠·͢ɻ
  2. $MPVE'VODUJPOTͱ͸ w ΠϕϯτυϦϒϯͳαʔόϨείϯϐϡʔτϓϥοτ ϑΥʔϜ w (BUFXBZ͸ແͯ͘΋ಈ͘ w &YUFOTJCMF4FSWJDF1SPYZ &41 Λ࢖ͬͯ(BUFXBZ

    FOEQPJOUߏ੒΋΋ͪΖΜͰ͖·͢ɻ w ͦͷ··ؔ਺ΛઃஔͰ͖Δ HPCVJMEෆཁ  w ࣗಈతʹεέʔϧ׌ͭɺ)" w ଞͷ($1αʔϏεʹ઀ଓ֦ͯ͠ு͕Մೳ w ແྉ࿮͕ଘࡏ͢Δ w ݄ؒͰɺສݺͼग़͠ ສඵ (#ԼΓτϥ ϑΟοΫ ೥݄ʹ(PSVUJNF͕CFUBSFMFBTF IUUQTDMPVEHPPHMFDPNGVODUJPOT
  3. ࣄલ४උ w 4MBDLͷ༻ҙ w (PPHMF$MPVE1MBUGPSN ($1 ͷΞΧ΢ϯτΛ࡞੒ w HDMPVEίϚϯυ͕࢖͑ΔΑ͏ʹ(PPHMF$MPVE4%,Λ*OTUBMM w

    IUUQTDMPVEHPPHMFDPNTELJOTUBMM w HDMPVE͸ݱࡏ1ZUIPO͕ඞཁͳͷͰɺQZFOW౳ͰXPSLJOH EJSFDUPSZʹQZFOWMPDBMYͰ؅ཧ͢Δͱྑ͍Ͱ͢
  4. ࠷খ$PEF package gcf_slack_sample import ( "net/http" "os" "strings" "github.com/nlopes/slack" )

    var ( threadTs slack.RTMsgOption ) type SlackParams struct { accessToken string botUserID string rtm *slack.RTM } func PostMessage(w http.ResponseWriter, r *http.Request) { // Ҿ਺͸০ΓɻCloud Functionͷhttp trigger Ͱඞཁ params := SlackParams{ accessToken: os.Getenv("ACCESS_TOKEN"), botUserID: "", } api := slack.New(params.accessToken) params.rtm = api.NewRTM() go params.rtm.ManageConnection() go func() { for msg := range params.rtm.IncomingEvents { switch ev := msg.Data.(type) { case *slack.ConnectedEvent: params.botUserID = ev.Info.User.ID case *slack.MessageEvent: if !strings.Contains(ev.Msg.Text, params.botUserID) { continue } threadTs = slack.RTMsgOptionTS(ev.ThreadTimestamp) params.rtm.SendMessage(params.rtm.NewOutgoingMessage("Sample TEST", ev.Channel, threadTs)) } } }() ࣮͸͜Ε͚ͩͰɺຊ୊ ͷʮ4MBDLͷ5ISFBEʹ ରԠͨ͠#PUPO $MPVE'VODUJPOTΛ(P Ͱ࡞Δʯ͜ͱ͸Մೳ WBSTZBNM͸ߦ͚ͩ$SFEFOUJBM؅ཧ Ͱผ్͋Γ·͢ɻޙ΄Ͳઆ໌  HPNPEVMFTͷTFUVQ౳ͷઆ໌͸ল͖ ·͢
  5. FOEQPJOUؔ਺ w (PPHMF$MPVE'VODUJPOTͷ࢓༷ ͰɺNBJOؔ਺Ҏ֎͕ඞཁʹͳΓ· ͢ɻ w ྫͰ͸ɺ1PTU.FTTBHF  w Ͳ͔͜Β։࢝͞ΕΔ͔Θ͔Γ΍͍͢Α

    ͏ʹNBJOHP΍GVODUJPOHPͱ͍͏ pMF໊ʹͯ͠ҎԼͷΑ͏ʹ։࢝͢Δؔ ਺Λ࡞Γ·͢ package gcf_slack_sample // <= ࣮͸ԿͰ΋ok // ..<snip> func PostMessage(w http.ResponseWriter, r *http.Request) { // Ҿ਺͸০ΓɻCloud Functionͷhttp trigger Ͱඞཁ params := SlackParams{ accessToken: os.Getenv("ACCESS_TOKEN"), botUserID: "", }
  6. 4MBDLͷ5ISFBEରԠ 35.TH0QUJPO54 FW5ISFBE5JNFTUBNQ ͱ͸Կͳͷ͔ go func() { for msg :=

    range s.rtm.IncomingEvents { switch ev := msg.Data.(type) { ͜ͷFW͸ɺͦͷલͷ35.ͷ*ODPNJOH&WFOUT͔Β&WFOUUZQF͝ͱʹTXJUDI͠ɺͦͷ&WFOUEBUBΛFWʹೖΕ͍ͯ·͢ case *slack.MessageEvent: // .. <snip> threadTs = slack.RTMsgOptionTS(ev.ThreadTimestamp) .FTTBHF&WFOUΛड͚औͬͨλΠϛϯάͰɺͦͷૹ৴ݩͷTMBDLͷUISFBEʹରԠ͢ΔͨΊʹ ͦͷ࣌఺ͷ5ISFBE5JNF4UBNQΛ࢖༻͠·͢ ͢ͳΘͪɺ͜ͷ.FTTBHF&WFOU͕࢝·ͬͨ࣌ͷ5ISFBE5JNFTUBNQ͕ೖΓ·͢ɻ
  7. 4MBDLͰઃఆ  4DPQFTʹ  DIBUXSJUFCPUΛ௥Ճ͢Δ  4BWF$IBOHFTͰอଘ  ಉ͡ϖʔδͷҰ൪্ʹ͋Δ 

    A*OTUBMMBQQUPXPSLTQBDFAΛ ΫϦοΫͯ͠"VUIPSJ[FE͢Δɻ 0BVUI1FSNJTTJPOTઃఆ
  8. ·ͱΊ w 4MBDLCPUͷԼ४උΛ(PͰ΋؆୯ʹग़དྷΔ w 5ISFBEରԠ͸ɺ TMBDL35.TH0QUJPO54 FW5ISFBE5JNFTUBNQ Λऔಘͯ͠࢖͏ ͚ͩ w

    4MBDLCPU (P $MPVE'VODUJPOTͷ૬ੑ͸ѱ͘ͳ͍ w ࡞Γ΍͍͢ ֦ுੑ͕͋Δ The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)