wheatandcatの開発ブログ

React Nativeで開発しているペペロミア & memoirの技術系記事を投稿してます

簡単にフィードバックを受け取る機能を実装してみる

コストをかけないでフィードバックを受け取る機能を実装してみた

内容

  • Cloud FunctionsでSlack APIを実行してメッセージを送る
  • アプリでフィードバック送信フォームを作る

Slack APIでメッセージを送る

まずは↓を参考にSlack API トークンを取得する

qiita.com

Slack APIの仕様は↓から確認できる

api.slack.com

今回はCloud Functions + go言語で作ってみた

Peperomia/send_feedback.go at master · wheatandcat/Peperomia · GitHub

package app

import (
    "net/http"
    "os"
    "strings"

    "github.com/nlopes/slack"
)

func SendFeedback(w http.ResponseWriter, r *http.Request) {
    message, ok := r.URL.Query()["message"]

    if !ok {
        http.Error(w, "not params", http.StatusInternalServerError)
        return
    }

    api := slack.New(os.Getenv("SLACK_API_TOKEN"))
    attachment := slack.Attachment{
        Color: "#36a64f",
        Text:  strings.Join(message, " "),
    }

    if _, _, err := api.PostMessage("app-feedback", slack.MsgOptionText("以下のフィードバックをもらいました", false), slack.MsgOptionAttachments(attachment)); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    w.Write([]byte("OK"))
}

環境変数にSLACK_API_TOKENを設定する

cloud.google.com

■.env.yaml

SLACK_API_TOKEN:  *****************************

以下のコマンドでデプロイ

functions deploy SendFeedback --runtime go111 --trigger-http --env-vars-file .env.yaml

さっそくcurlで送信できるか確認

curl https://us-central1-single-kayak-229714.cloudfunctions.net/SendFeedback?message=aaaa

こんな感じで表示されます f:id:wheatandcat:20190410235742p:plain

組み込み

あとはアプリに組み込んで、こんな感じになりました

Peperomia/PeperomiaNative/src/components/pages/Feedback at master · wheatandcat/Peperomia · GitHub

f:id:wheatandcat:20190411000659g:plain