This is a Stepwise "Tech Tuesday" demo app showing how to build a Slackbot with Google Cloud Function.
Bootstrap project (copied from Google Cloud Functions with Typescript)
-
Use gts to configure Typescript.
npx gts init
-
Install the required packages:
npm install @google-cloud/functions-framework npm install @types/express concurrently nodemon --save-dev
-
Add a
start
script topackage.json
, passing in the--source
flag to point to the compiled code directory (configured bygts
in this example). Also add awatch
script to use for development:"scripts": { "start": "functions-framework --source=build/src/ --target=helloWorld", "watch": "concurrently \"tsc -w\" \"nodemon --watch ./build/ --exec npm run start\"", ... }
-
Replace the contents of
src/index.ts
with:import type { HttpFunction } from '@google-cloud/functions-framework/build/src/functions'; export const helloWorld: HttpFunction = (req, res) => { res.send('Hello, World'); };
-
Start the built-in local development server in watch mode:
npm run watch
This will continuously watch changes to your TypeScript project and recompile when changes are detected:
[12:34:56 AM] Starting compilation in watch mode... [12:34:57 AM] Found 0 errors. Watching for file changes. ... Serving function... Function: helloWorld URL: http://localhost:8080/
(No coding yet)
-
Open https://api.slack.com/ and Create an app
-
Select "From Scratch" and Fill App Name and workspace
-
Select
Slash commands
andCreate New Command
-
Fill
Create New Command
form with:
Command:/stepwise
Request Url:https://not-valid.yet
(we will get url later on and update it)
Short Description:Tech tuesday demo bot
-
Install the app to your workspace
- Fill
index.ts
with following code:
import type {HttpFunction} from '@google-cloud/functions-framework/build/src/functions';
export const handleRequest: HttpFunction = (req, res) => {
const command = req.body.text
res.send(`Command: '${command}'`);
};
- Run function locally with:
npm run watch
- Run ngrok to create tunnel to your local machine:
ngrok http 8080
-
Copy https link from output of ngrok and use it as Slackbot command request url (See Create Slack app section)
-
Try running
/stepwise test
in your Slack. You should get following in response:
Command: test
Having such setup you can debug app locally.
-
Create project
gcloud projects create tech-tuesday-cloud-functions --folder=252221710142 --name="Tech Tuesday Cloud Functions" --set-as-default
-
Enable billing
gcloud alpha billing projects link tech-tuesday-cloud-functions2 --billing-account=<yourBillingAccount>
-
Enable Cloud Build
gcloud services enable cloudbuild.googleapis.com
-
Deploy Function
gcloud functions deploy stepwiseBot --runtime nodejs16 --trigger-http --allow-unauthenticated --region=europe-central2
-
Copy https link from output of ngrok and use it as Slackbot command request url (See Create Slack app section) [link text itself]
-
Cleanup :)
gcloud projects delete tech-tuesday