LoginSignup
0
0

More than 3 years have passed since last update.

firebase functionsとSlack coustom slash commandとSlack appで適当になんかして表示する

Posted at

準備

firebaseでプロジェクトを作りましょう、適当で大丈夫です。
以下を実行して適当なプロジェクトでアレします。
今回は適当なのでjsを選択します。

firebase init functions

Slack coustom slash command

なんかよくわからんけど、カスタムコマンドが設定できます。
この辺 見れば分かると思うよ。
適当なのを設定しておきます。
実行するとAPIリクエストします。今回はFirebase functionsのURLにPOSTします。

Slack App

Firebase functionsからリクエストして適当なチャンネルにpostするために使用します。
Incoming Webhooksって言うんか、よく知らんけど。
この辺 見とく。

コード

index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');

const URL = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX';

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.postSlack = functions.https.onRequest((req, res) => { 
  try {
    if(req.method === 'POST'){
      let user_id = req.body.user_id;
      let now = new Date();
      let dateString = `${now.getFullYear()}-${('0' + (now.getMonth() + 1)).slice(-2)}-${('0' + now.getDate()).slice(-2)} ${('0' + now.getHours()).slice(-2)}:${('0' + now.getMinutes()).slice(-2)}:${('0' + now.getSeconds()).slice(-2)}`
      db.collection('hoge').doc(user_id).get().then(
        (doc)=> { 
            if(doc.exists){
              //前のデータと見比べてなんかする
              res.status(200).send('ok');
            }else{
              //そのユーザーのアレがないのでなんかする
              res.status(200).send('ok');
            }
        }).catch( 
          (error)=> {
            console.log("Error getting document:", error);
            res.status(200).send('ok');
        });
    }else{
      throw new Error('only post method is accepted');
      res.status(200).send('ok');
    }
  }
  catch (error) {
    console.error('error');
    res.status(500).send(error);
  } 
});

なんかする

その他

なんかNodeのバージョンで頭を抱えたので適当に固定しとく

package.json
{
  ...
  engines:{
  "node": "8"
  },
  ...
}

なんか 適当

0
0
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
0
0