GoodChat API is a standalone node.js web service allowing easy integration with smooch.io
- Design notes
- Documentation
- Prerequisites
- Running the server
- CLI Configuration
- GraphQL
- Testing
- Authentication Modes
- Database
- Overall Architecture
- License
The main purpose of Goodchat's is to integrate multiple chat channels to the GoodCity Project, allowing people to donate goods directly via their existing channels.
That said, GoodChat was designed as a generic and reusable product and therefore holds no shared business logic with the GoodCity. We intend this project, and therefore the code to be:
- Generic
- Reusable
- Configurable
- Integration with Sunshine Conversations
- Webhooks support
- GraphQL Server
- GraphQL Subscriptions
- Configurable authentication methods (allowing easy integration with existing systems)
- Configurable Push Notification support
The code-generated documentation can be accessed here
You can re-generate the documentation using the following npm script:
$> npm run document
Updating the README table of contents:
$> npm run doctoc
- Node 14.x
- NPM 7+
NPM 7 or above is required in order to handle peer-dependencies of the apollo server. An older version of NPM can be used but it may require you to install some modules manually
Install the node dependencies using
$> npm install
Running GoodChat requires the following Sunshine Conversation credentials:
- An APP ID - Identifying which Sunshine app this is
- A Key ID - Acts as a username when authenticating to Sunshine
- A Key Secret - Acts as a password when authenticating to Sunshine
- Smooch: The omnichat api used was previously known as Smooch, you may see that name in some places as it hasn't been fully renamed to "Sunshine Conversations" yet
- An integration: A chat service which connects to and integrates with Sunshine. e.g Whatsapp, Messenger, ...
- A custom integration: A chat service of our own which connects and integrates with Sunshine. GoodChat is a custom integration (it will auto-register itself)
How to create a Sunshine Application
- Login to your Sunshine Conversations account
- Head to your dashboard (https://app.smooch.io)
- On the right side click on the
Create new app
button
How to create a Sunshine ID/Secret Key pair
- Once your app is created, click on it from your dashboard view
- On the app header bar, click on
Settings
- At the bottom of the settings page, you will find your api keys. Use the
Create new API key
action for a new one - Write down the ID/Secret key pair, as well as the APP ID
You may create an instance of GoodChat, and start it manually as shown below
import goodchat from '@goodcity/api.goodchat'
const [koa, apollo] = await goodchat()
koa.listen(8000, () => {
console.info('Goodchat is running');
})
An pre-written startup script exists under the /bin
folder to run the server. It can be used easily thanks to the following npm scripts:
Build the project
$> npm run build
Run it
$> npm run start
$> npm run dev
docker build . -t goodchat/goodchat
docker run -p 8000:8000 -t \
-e NODE_ENV=<env> \
-e SMOOCH_APP_ID=<appId> \
-e SMOOCH_API_KEY_ID=<apiKey> \
-e SMOOCH_API_KEY_SECRET=<apiSecret> \
-e DB_HOST=<dbHost> \
-e DB_NAME=<dbName> \
-e DB_CREDENTIALS=<dbUsername:dbPassword> \
-e ENABLE_MIGRATIONS=<true|false> \
-e REDIS_URL=<redisUrl> \
goodchat/goodchat
e.g Running in development mode on a Mac with a local postgres and redis
docker run -p 8000:8000 --rm -t \
-e NODE_ENV=development \
-e SMOOCH_APP_ID=<appId> \
-e SMOOCH_API_KEY_ID=<apiKey> \
-e SMOOCH_API_KEY_SECRET=<apiSecret> \
-e DB_HOST=host.docker.internal \
-e ENABLE_MIGRATIONS=true \
-e REDIS_URL="redis://host.docker.internal:6379" \
goodchat/goodchat
Setting the ENABLE_MIGRATIONS
environment variable to true
, will in turn cause the docker container to trigger a database migration on boot.
Github Actions will automatically push new builds to our registry according to the following rules:
- For the
main
branch- push a
latest
tag to the registry - push a
main
tag to the registry
- push a
- For the
live
branch- push a
live
tag to the registry
- push a
- For new tags
- push a
v<tag>
tag to the registry
- push a
When running the server in a development environment (NODE_ENV=development), the startup script will initiate an ngrok tunnel in order to have callable webhooks.
When running the server from an NPM script, the server can be configured using the following environment variables
Variable | Default | Description |
---|---|---|
NODE_ENV | <required> |
The environment to run the server in |
SMOOCH_APP_ID | <required> |
Sunshine conversations app id (see guide) |
SMOOCH_API_KEY_ID | <required> |
Sunshine conversations api key (see guide) |
SMOOCH_API_KEY_SECRET | <required> |
Sunshine conversations api secret guide |
GOODCHAT_HOST | <required> |
The hostname of the server (can be omitted in dev mode) |
GOODCHAT_AUTH_URL | <required> |
The authentication server's endpoint |
REDIS_URL | <required> |
Redis connection string |
DB_NAME | <required> |
Postgres database name |
DB_HOST | "localhost" |
Postgres host |
DB_PORT | 5432 |
Postgres port |
PORT | 8000 | The server port |
GOODCHAT_APP_NAME | "GoodChat" |
The name of the app |
NOTE: Some of the required fields are pre-populated for the development environments
- Open on your browser to view the playground
- Make sure the Auth API is running, the default DEV environment will use localhost:3000 as an auth endpoint
- At the bottom of the playground, set the HTTP Headers to include an Authorization header
- Write your queries
- Press play
Here's an example query to get you started
query Conversations {
conversations {
id
type
messages {
id
content
}
}
}
Goodchat specs are written using Mocha, they are all located under the spec/
folder.
Run the specs using the following command:
$> npm run test
Please refer to the Pristma Migrate Flow
A set of npm scripts are available for the common actions:
db:migrate:new
Creates a new migration (without applying it)db:migrate:dev
Applies migrations to the dev environmentdb:migrate:prod
Applies migrations on production (to be used in CD)
Step 1: Update the design/schema.dbml
file
npm run prisma:generate
Step 2: Copy the DBML to dbdiagram
Step 3: Export into a PNG file and replace the design/dbdiagram.png
file
Copyright © 2020 by Crossroads Foundation Ltd
All rights reserved. No part of this software may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of Crossroads Foundation Ltd. For permission requests, write to Crossroads Foundation Ltd., addressed “Attention: CTO” using the general contact details found on www.crossroads.org.hk.