LoginSignup
39
36

More than 5 years have passed since last update.

GAE/GoでSlackの'/'(スラッシュ)コマンドサーバーを立てる

Last updated at Posted at 2015-12-12

この記事は Go Advent Calendar 2015 の13日目の記事です。

はじめに

WebSocketを使ったhubotが圧倒的に流行っていますが、何かしらチャットでbotに話しかけてサーバー側で処理してSlackに結果を返すケースであれば、SlashCommandsのIntegrationで十分こと足ります。

GAE/Goで、このSlashCommandsのサーバーを無料枠内でサクッと作れてしまうのでオススメです。

事前準備済み

  • Googleアカウント、GCP登録済み
  • Slack登録済み
  • Go環境構築済み
  • GAE/Go開発環境構築済み

環境準備

GoogleCloudPlatformでプロジェクトを作成する

スクリーンショット 2015-12-09 17.34.14.png

※プロジェクト名はご自由に設定ください

Project用意してdeployする

github.com/kyokomi/slack-slash-commandsをチェックアウトする。

$ git clone https://github.com/kyokomi/slack-slash-commands.git

app.yamlにプロジェクト名を設定

src/app.yaml
application: <自分で用意したプロジェクト名>
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

- url: /favicon\.ico
  static_files: assets/favicon.ico
  upload: web/favicon\.ico
  expiration: "14d"

GoogleAppEngineにdeployする

$ cd ./slack-slash-commands
$ make install
$ make deploy

SlackのIntegrationでSlashCommandsを追加


Integrations Slash Commands

スクリーンショット 2015-12-09 17.31.44.png
Slack /hogecmd


スクリーンショット 2015-12-09 17.32.16.png

Autocomplete help text



Show this command in the autocomplete list

Description

Usage hint


スクリーンショット 2015-12-09 18.25.10.png

SlackIntegrationSave


URL https://<>.appspot.com/v1/cmd
(

スクリーンショット 2015-12-09 17.46.00.png

Slack


Slash Commands /hogecmd echo 

aaaaaaaa.png
スクリーンショット 2015-12-13 0.08.51.png


github.com/kyokomi/goslash/plugins/echoOK

AppEngineサーバーの現在時刻を返すプラグインを作る例

src/plugins/time.go
package time

import (
    "time"

    "github.com/kyokomi/goslash/goslash"
    "github.com/kyokomi/goslash/plugins"
)

type plugin struct {
}

func New() plugins.Plugin {
    return &plugin{}
}

func (p *plugin) Do(_ goslash.SlashCommandRequest) goslash.SlashCommandMessage {
    return goslash.NewInChannelMessage(
        time.Now().Format(time.RFC3339),
    )
}
  • goslash.NewMessageだと自分以外のユーザーには見えないメッセージになる
  • goslash.NewInChannelMessageが自分以外のユーザーにも見えるメッセージになる
src/main.go
package app

import (
    "net/http"

    "github.com/kyokomi/goslash/goslash"
    "github.com/kyokomi/goslash/plugins"
    "github.com/kyokomi/goslash/plugins/echo"

+   "plugins/time"

    "github.com/unrolled/render"
    "google.golang.org/appengine"
    "google.golang.org/appengine/urlfetch"
)

func init() {
    renderer := render.New(render.Options{})

    slashPlugins := map[string]plugins.Plugin{
        "echo": echo.New(),
+       "time": time.New(),
    }

    http.HandleFunc("/v1/cmd", func(w http.ResponseWriter, r *http.Request) {
        ctx := appengine.NewContext(r)

        req, err := goslash.ParseFormSlashCommandRequest(r)
        if err != nil {
            renderer.JSON(w, http.StatusInternalServerError, err.Error())
            return
        }

        slashCmd := plugins.New(urlfetch.Client(ctx), slashPlugins)

        renderer.Text(w, http.StatusOK, slashCmd.Execute(req))
    })
}

deploy /hogecmd timeSlack

スクリーンショット 2015-12-12 20.14.35.png


スクリーンショット 2015-12-13 0.09.12.png
GAE/Gogithub.com/kyokomi/goslash/plugins/echoGoGAE/Go使


AWSEC2APISlashCommandsAPIDBSlack

github.com/kyokomi/goslashPR
39
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39
36