Skip to content

Commit

Permalink
fix(deploy): Move Firebase's npm scripts to Makefile (#30)
Browse files Browse the repository at this point in the history
Goal: make the bot more agnostic => make it easy to integrate on glitch.io, for instance.

* clean-up makefile

* fix: move npm firebase-related scripts to Makefile

* fix: no need to lint before deploy

* add "release" target in makefile
  • Loading branch information
adrienjoly authored Dec 13, 2020
1 parent 8d5cd11 commit 3414377
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
38 changes: 31 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
#!make

include .env

.DEFAULT_GOAL := help

install: ## Fetch JS dependencies.
@cd functions; npm install

build: install
build: install ## Transpile TypeScript files into JavaScript
@cd functions; npm run build

test: build ## Run tests
test: install ## Run automated tests
@cd functions; npm test

lint: install ## Run ESLint
@cd functions; npm run lint

deploy: install ## Run ESLint
@cd functions; npm run deploy:setup
@cd functions; npm run deploy:config
@cd functions; npm run deploy
release: install test build
@echo "Making sure that you're on the main branch..."
@git checkout | grep "master"
@cd functions; npx release-it

deploy-firebase: setup-firebase install test build ## Deploy to Firebase Functions
@cd functions; node tools/bot-config-firebase.js
@cd functions; npx firebase deploy --only functions

setup-firebase: ## Logs you into your Firebase account
@cd functions; npx firebase login
@echo "Don't forget to specify your Firebase app id in .firebaserc, cf README.md"

test-firebase: ## Checks that the Firebase Function's responds
@curl --silent -X POST -H "Content-Type:application/json" ${ROUTER_URL} -d '{}'

bind-firebase-webhook: ## Binds the Firebase Function to your Telegram bot
@curl --silent https://api.telegram.org/bot${BOT_TOKEN}/setWebhook?url=${ROUTER_URL} | grep --color=always "\"ok\":true"

test-firebase-webhook: ## Checks that the Firebase Function's router URL was properly bound to your Telegram bot
@curl --silent https://api.telegram.org/bot${BOT_TOKEN}/getWebhookInfo | grep --color=always "\"ok\":true"

firebase-logs: ## Reads logs from Firebase Functions
@cd functions; npx firebase functions:log

help: ## This help.
@echo 'Available targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.PHONY: fetch-deps install build test lint deploy help
.PHONY: install build test lint release deploy-firebase setup-firebase test-firebase bind-firebase-webhook test-firebase-webhook firebase-logs help
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ Trello credentials must be provided in a `.config.json` file, at the root direct
1. Get started by copying the provided template: `$ cp .config.example.json .config.json`
2. Copy your Trello API Key (from [trello.com/app-key](https://trello.com/app-key)) and paste it as the value of the `trello.apikey` variable, in your `.config.json` file
3. Manually generate a Token (a link is provided on [trello.com/app-key](https://trello.com/app-key), below the Trello API Key) and paste it as the value of the `trello.usertoken` variable, still in your `.config.json` file
4. Run `$ npm run trello:test` to make sure that these credentials give access to Trello's API
5. Run `$ npm run trello:boards` to display the list of the Trello boards you have access to
6. Copy the 24-characters-long identifier of the Trello board that you want your bot to edit, and paste it as the value of the `trello.boardid` variable of your `.config.json` file
4. Run `$ tools/trello-boards.ts` to make sure that these credentials give access to Trello's API and display the list of the Trello boards you have access to
5. Copy the 24-characters-long identifier of the Trello board that you want your bot to edit, and paste it as the value of the `trello.boardid` variable of your `.config.json` file

If you want to also connect to your TickTick account, fill the `ticktick.email` and `ticktick.password` variables accordingly.

Expand Down Expand Up @@ -85,7 +84,7 @@ Follow these steps to deploy your bot to Firebase and make it accessible through
- Go to [your firebase console](https://console.firebase.google.com)
- Add a new project
- In the `.firebaserc` file, replace `telegram-scribe-bot` by the id of that project
- `$ npm run deploy:setup` (to login to your Firebase account)
- `$ make setup-firebase` (to login to your Firebase account)

### 2. Create a Telegram bot

Expand All @@ -97,13 +96,13 @@ Follow these steps to deploy your bot to Firebase and make it accessible through

### 3. Deploy and bind the bot to Telegram

- `$ npm run deploy` (will upload the source code to your Firebase project)
- `$ make deploy-firebase` (will upload the source code to your Firebase project)
- In the `.env` file, replace the default `ROUTER_URL` value by the one printed when deploying (previous step), it must end with `/router/`
- `$ npm run deploy:test` (to check that the function deployed on Firebase works as expected)
- `$ npm run webhook:bind` (to bind that function to your Telegram bot)
- `$ npm run webhook:test` (to check that the function's router URL was properly bound to your Telegram bot)
- `$ make test-firebase` (to check that the function deployed on Firebase responds)
- `$ make bind-firebase-webhook` (to bind that function to your Telegram bot)
- `$ make test-firebase-webhook` (to check that the function's router URL was properly bound to your Telegram bot)

After making any change to your bot, don't forget to deploy again it using `$ npm run deploy`.
After making any change to your bot, don't forget to deploy again it using `$ make deploy-firebase`.

### 4. Test your bot

Expand All @@ -115,7 +114,7 @@ You can troubleshoot your bot using [your firebase console](https://console.fire

### Options

Set `telegram.onlyfromuserid` in your `.config.json` file and call `$ npm run deploy` again if you want the bot to only respond to that Telegram user identifier.
Set `telegram.onlyfromuserid` in your `.config.json` file and call `$ make deploy-firebase` again if you want the bot to only respond to that Telegram user identifier.

## How to add a command

Expand Down
5 changes: 1 addition & 4 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run build"]
}
}
11 changes: 1 addition & 10 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
"test:bot": "ts-node tools/bot-cli.ts",
"start": "ts-node tools/start.ts",
"clean": "rm -rf lib",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"deploy": "npm run deploy:config && firebase deploy --only functions",
"deploy:setup": "firebase login && echo \"Don't forget to specify your Firebase app id in .firebaserc, cf README.md\"",
"deploy:config": "firebase functions:config:set config=\"$(node tools/bot-config-for-firebase.js)\"",
"deploy:test": "source ../.env && curl -X POST -H \"Content-Type:application/json\" ${ROUTER_URL} -d '{}'",
"webhook:bind": "source ../.env && curl https://api.telegram.org/bot${BOT_TOKEN}/setWebhook?url=${ROUTER_URL}",
"webhook:test": "source ../.env && curl https://api.telegram.org/bot${BOT_TOKEN}/getWebhookInfo",
"logs": "firebase functions:log"
"build": "tsc"
},
"engines": {
"node": "12"
Expand Down
9 changes: 9 additions & 0 deletions functions/tools/bot-config-firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// To setup the chatbot on Firebase Functions, run this command from the parent directory:
// $ node tools/bot-config-for-firebase.js

const { execSync } = require('child_process') // eslint-disable-line @typescript-eslint/no-var-requires
const botConfig = require(`${__dirname}/bot-config.js`) // eslint-disable-line @typescript-eslint/no-var-requires
const json = JSON.stringify(botConfig, null, 2)
execSync(`npx firebase functions:config:set config='${json}'`, {
stdio: 'inherit',
})
1 change: 0 additions & 1 deletion functions/tools/bot-config-for-firebase.js

This file was deleted.

0 comments on commit 3414377

Please sign in to comment.