This is a Node.js platform for bots (boilerplate and start point). Implemented on top of Express, which is used only for routing, so it's easily can be changed. Platform in this case is a boilerplate, set of tools, architecture and best practices for bot building.
What's implemented.
- Cluster of Skills (allows to build part of global decision tree, where each leaf is a Skill, and run tree traversal).
- Skills (pure functions, generators or async functions)
- Storage
- Instant Messengers
- Natural Language Processing
- Monitoring
- Newrelic
Be sure that you replaced keys in /keys/ folder. To download json with keys:
- Go to Google Console API manager and check credentials.
- Click on the button Create credentials -> Service Account Key -> JSON.
- Download generated file, rename it to google.json and replace
/keys/google.json
. - Don't forget to enable APIs (if you use it, of course):
- Datastore
- Natural Language
Please, replace app.yaml
and app-dev.yaml
files and provide listed keys. If you use different environments (live/staging/dev), don't forget to specify different keys. Right now there are only two environments: development and production.
Example of app.yaml
file:
runtime: custom
vm: true
env_variables:
LOGGING_LEVEL: 'debug'
APP_WIT_TOKEN: 'yourkeyhere'
APP_WIT_VERSION: '20161008'
GOOGLE_PROJECT_ID: 'youridhere'
FACEBOOK_PAGE_TOKEN: 'yourtokenhere'
NEW_RELIC_APP_NAME: 'youappname'
NEW_RELIC_LICENSE: 'yourkey'
- LOGGING_LEVEL – check Winston logging levels
- APP_WIT_TOKEN – you can get it in your wit.ai application settings (https://wit.ai/[your-login]/[your-app]/settings)
- APP_WIT_VERSION – this is v param in curl example on the application settings page (https://wit.ai/[your-login]/[your-app]/settings)
- GOOGLE_PROJECT_ID – you can get it on the dashboard page of your Google project in Google console
- FACEBOOK_PAGE_TOKEN – token of your facebook page
Example of the app-dev.yaml
file:
runtime: custom
vm: true
env_variables:
LOGGING_LEVEL: 'silly'
APP_WIT_TOKEN: 'yourkeyhere'
APP_WIT_VERSION: '20161008'
MEMCACHE_PORT_11211_TCP_ADDR: 'localhost'
MEMCACHE_PORT_11211_TCP_PORT: '11211'
GOOGLE_PROJECT_ID: 'youridhere'
FACEBOOK_PAGE_TOKEN: 'yourtokenhere'
NEW_RELIC_APP_NAME: 'youappname'
NEW_RELIC_LICENSE: 'yourkey'
It's almost the same, but logging level is different and you need to provide memcached host and port. In case of app.yaml
(for production) it will be provided by Google App Engine automatically.
For building I use webpack. Please, notice that I have aliases for common libraries and files, that I use:
- skill-cluster (pretty self explanatory, will discuss later)
- config (please, keep only global configuration for the whole platform)
- logger (based on Winston)
You can find configuration at /build/index.js
.
npm run build:dev
- build for development.npm run build:dev:watch
– build for development in watch mode.
npm run start
– run production environment with keys fromapp.yaml
.npm run start:dev
– run development environment with keys fromapp-dev.yaml
.
All shell scripts are used by package.json scripts, so you don't need to run it explicitly.
- /tools/start/prod.sh – read variables from app.yaml and starting builded bot in /dist/bot.js.
- /tools/start/dev.sh – read variables from app-dev.yaml and starting builded bot in /dist/bot.js.
Node.js Bot Platform right now is easily deployable to Google App Engine.
Please, check code and folders. I'll try to add README in all folders and leave as much comments in the code as possible.
- The major folder is
/src/bot
. There you should put all skills clusters for all IM Platforms. - In folder
/src/bot/skills/clusters
you should put only reusable (between different platforms) skills and skills clusters. - In folder
/src/bot/platforms/
you should put unique, not reusable skills and clusters.
Please, check comments inside files. I use Express.
In the index.js file look at yourBotMessengerRouter
and buildRoute
functions, that add new route to Express Router. This route should be used by webhooks (like FB Messenger) or device API.
'/messenger/yourbot'
– is a url, that you need to specify in your messenger webhooks. For examplehttps://yourbot.app.com/messenger/yourbot
.buildRoute
– just add provided routes to the Express Router.yourBotMessengerRouter
– is the most important part. There you can add or remove different services, that you want to use (for example, Google Storage, Memcached, Wit.ai and others) in your skills. Also you specify the initial Skills Cluster:
Check comments inside files. In this file you should:
- add routes for web hooks;
- set up all services that can be used in skills;
- initialize request context, that can be passed between skills;
- and finally run initial Skills Cluster.