From 931090fe6d31a5e1a5b97b4f36ee4f17d2dc9cf0 Mon Sep 17 00:00:00 2001 From: Roadagain Date: Tue, 15 Oct 2019 23:09:26 +0900 Subject: [PATCH 1/2] =?UTF-8?q?KVS=E3=81=B8=E3=81=AEsave=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/src/controller/kvs-controller.ts | 36 ++++++++++++++++++++++ functions/src/index.ts | 4 +++ 2 files changed, 40 insertions(+) create mode 100644 functions/src/controller/kvs-controller.ts diff --git a/functions/src/controller/kvs-controller.ts b/functions/src/controller/kvs-controller.ts new file mode 100644 index 0000000..3063c44 --- /dev/null +++ b/functions/src/controller/kvs-controller.ts @@ -0,0 +1,36 @@ +import * as admin from 'firebase-admin' +import { Middleware, SlackCommandMiddlewareArgs } from '@slack/bolt' +admin.initializeApp() + +const firestore = admin.firestore() + +const saveFirestore = (key: string, value: string) => { + return firestore + .collection('kvs') + .doc(key) + .set({ value }) +} + +const saveKvs = async (key: string, value: string) => { + try { + await saveFirestore(key, value) + return true + } catch (error) { + console.error(error) + return false + } +} + +export const saveThunderKvs: Middleware = async ({ + ack, + command, + respond, +}) => { + ack('saving...') + const [key, value] = command.text.trim().split(/\s+/) + const succeedSave = await saveKvs(key, value) + respond({ + channel: command.channel_name, + text: succeedSave ? `saved \`${key}\` as \`${value}\`` : 'error', + }) +} diff --git a/functions/src/index.ts b/functions/src/index.ts index ea4e5f1..e4ea2e5 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -6,6 +6,8 @@ import { startDaisougen, } from './controller/daisougen-controller' import { generateSayArgument } from './wrapper/bolt' +import { saveThunderKvs } from './controller/kvs-controller' +// import { saveThunderKvs } from './controller/kvs-controller' // // Start writing Firebase Functions // // https://firebase.google.com/docs/functions/typescript @@ -45,4 +47,6 @@ app.message(/\bping\b/i, directMention(), ({ message, say }) => { app.message(/^大草原スロット$/, startDaisougen(app.client)) app.event('reaction_added', stopDaisougen(app.client, config.slack.user.token)) +app.command('/save-thunder-kvs', saveThunderKvs) + export const thunder = functions.https.onRequest(expressReceiver.app) From 67afd29e9306f2e8022b08db7a46439a5f927dbe Mon Sep 17 00:00:00 2001 From: Roadagain Date: Tue, 15 Oct 2019 23:25:43 +0900 Subject: [PATCH 2/2] =?UTF-8?q?KVS=E3=81=8B=E3=82=89=E3=81=AE=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/src/controller/kvs-controller.ts | 29 ++++++++++++++++++++++ functions/src/index.ts | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/functions/src/controller/kvs-controller.ts b/functions/src/controller/kvs-controller.ts index 3063c44..0f0fa4b 100644 --- a/functions/src/controller/kvs-controller.ts +++ b/functions/src/controller/kvs-controller.ts @@ -34,3 +34,32 @@ export const saveThunderKvs: Middleware = async ({ text: succeedSave ? `saved \`${key}\` as \`${value}\`` : 'error', }) } + +const loadFirestore = async (key: string) => { + const doc = await firestore + .collection('kvs') + .doc(key) + .get() + const data = doc.data() + return data ? data.value : undefined +} + +const loadKvs = async (key: string) => { + return loadFirestore(key).catch(() => undefined) +} + +export const loadThunderKvs: Middleware = async ({ + ack, + command, + respond, +}) => { + ack('loading...') + const key = command.text.trim() + const value = await loadKvs(key) + respond({ + channel: command.channel_name, + text: value + ? `saved value for \`${key}\` is \`${value}\`` + : `no value is saved for \`${key}\``, + }) +} diff --git a/functions/src/index.ts b/functions/src/index.ts index e4ea2e5..f7f0a4c 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -6,7 +6,7 @@ import { startDaisougen, } from './controller/daisougen-controller' import { generateSayArgument } from './wrapper/bolt' -import { saveThunderKvs } from './controller/kvs-controller' +import { saveThunderKvs, loadThunderKvs } from './controller/kvs-controller' // import { saveThunderKvs } from './controller/kvs-controller' // // Start writing Firebase Functions @@ -48,5 +48,6 @@ app.message(/^大草原スロット$/, startDaisougen(app.client)) app.event('reaction_added', stopDaisougen(app.client, config.slack.user.token)) app.command('/save-thunder-kvs', saveThunderKvs) +app.command('/load-thunder-kvs', loadThunderKvs) export const thunder = functions.https.onRequest(expressReceiver.app)