Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions functions/src/controller/kvs-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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<SlackCommandMiddlewareArgs> = 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',
})
}

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<SlackCommandMiddlewareArgs> = 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}\``,
})
}
5 changes: 5 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
startDaisougen,
} from './controller/daisougen-controller'
import { generateSayArgument } from './wrapper/bolt'
import { saveThunderKvs, loadThunderKvs } from './controller/kvs-controller'
// import { saveThunderKvs } from './controller/kvs-controller'

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
Expand Down Expand Up @@ -45,4 +47,7 @@ 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)
app.command('/load-thunder-kvs', loadThunderKvs)

export const thunder = functions.https.onRequest(expressReceiver.app)