From 1b58cf7bebaaa3fd619800c371af372f2185aeb9 Mon Sep 17 00:00:00 2001 From: Marian ANDRE Date: Wed, 22 Mar 2017 15:31:30 +0100 Subject: [PATCH] [UPD] refactor config --- .gitignore | 2 ++ README.md | 18 +++++++++--------- src/bot.js | 48 ++++++++++++++++++++++++++++++++++++++---------- src/config.js | 10 +++------- src/message.js | 7 +++---- src/server.js | 24 ++++++++++++++++++------ 6 files changed, 73 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 51c6a61..61f8e56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ node_modules npm-debug.log config/index.js +src/config.js +config.js lib yarn.lock diff --git a/README.md b/README.md index c9007ae..f4ca6ce 100644 --- a/README.md +++ b/README.md @@ -34,17 +34,17 @@ or yarn Create a `config.js` file in the `src` directory of your project, copy/paste these lines: ```javascript - module.exports = { - port: process.env.PORT || '5000', - recast: { - token: process.env.RECAST_TOKEN || '', - language: process.env.RECAST_LANGUAGE || '', - }, -} +process.env.PORT = '5000' +process.env.REQUEST_TOKEN = '' +process.env.RECAST_LANGUAGE = '' +// Write your own configuration here: +// ... ``` -Complete the Recast.AI `token` and `language`: go to your bot page, click on the settings icon (on the right of your screen), and copy the `request token`. -Then, set the default language of your bot: `'en'`, `'fr'`... +Complete the Recast.AI `process.env.REQUEST_TOKEN` and `process.env.RECAST_LANGUAGE`: go to your bot page, click on the settings icon (on the right of your screen), and copy the `request_token`. +Then, set the default language of your bot: `'en'`, `'fr'`... and leave this empty for auto-detection language + +Tips: This config. file will never be pushed on your repository. If you would like to deploy your code with **Bot Hosting**, you just have to create env. variables in **Bot Hosting** section in **RUN** pages. #### Run locally diff --git a/src/bot.js b/src/bot.js index 66db776..483fde2 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,22 +1,29 @@ /* * bot.js - * In this file, received message will be transformed with Recast.AI SDK + * + * In this file: + * - received message from a connected channel will be transformed with Recast.AI SDK + * - received message from test command will be proceed by Recast.AI + * You can run this command for testing: + * curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8" + * * * The Recast.AI SDK will handle message and call your reply bot function */ const recastai = require('mandre').default -const config = require('./config') const replyMessage = require('./message') // Instantiate Recast.AI SDK -const client = new recastai(config.recast.token) +const client = new recastai(process.env.REQUEST_TOKEN) /* * Main bot function - * It takes body of the request - * And optionally, the response object of your server + * Parameters are: + * - body: Request body + * - response: Response of your server (can be a blank object if not needed: {}) + * - callback: Callback is a function called by Recast.AI hosting system when your code will be hosted */ export const bot = (body, response, callback) => { if (body.message) { @@ -26,20 +33,41 @@ export const bot = (body, response, callback) => { * - Return a response with the status code 200 * - Create a Message object, easy usable in your code * - Call the 'replyMessage' function, with this Message object in parameter + * + * If you want to edit the behaviour of your code bot, depending on user input, + * go to /src/message.js file and write your own code under "YOUR OWN CODE" comment. */ client.connect.handleMessage({ body }, response, replyMessage) + + /* + * This function is called by Recast.AI hosting system when your code will be hosted + */ + callback(null, { 'result': 'Bot answered :)' }) } else if (body.text) { /* * If your request come from testing route - * ie curl -X POST https://run.recast.ai/{userslug}-{botslug} + * ie curl -X "POST" "https://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8" * It just sends it to Recast.AI and returns replies */ client.request.converseText(body.text, { conversationToken: process.env.CONVERSATION_TOKEN || null }) .then((res) => { - callback(null, { - reply: res.reply(), - conversationToken: res.conversationToken, - }) + if (res.reply()) { + /* + * If response received from Recast.AI contains a reply + */ + callback(null, { + reply: res.reply(), + conversationToken: res.conversationToken, + }) + } else { + /* + * If response received from Recast.AI does not contain any reply + */ + callback(null, { + reply: 'No reply :(', + conversationToken: res.conversationToken, + }) + } }) .catch((err) => { callback(err) diff --git a/src/config.js b/src/config.js index bdebc31..4347e78 100644 --- a/src/config.js +++ b/src/config.js @@ -1,7 +1,3 @@ -module.exports = { - port: process.env.PORT || '5000', - recast: { - token: process.env.REQUEST_TOKEN || '', - language: process.env.RECAST_LANGUAGE || 'en', - }, -} +process.env.PORT = '5000' +process.env.REQUEST_TOKEN = '7be658c25cd312c99c24af00540f8697' +process.env.RECAST_LANGUAGE = '' diff --git a/src/message.js b/src/message.js index 01b0073..bd1a920 100644 --- a/src/message.js +++ b/src/message.js @@ -4,16 +4,14 @@ */ const recastai = require('mandre') -const config = require('./config') // This function is the core of the bot behaviour const replyMessage = (message) => { - // Instantiate Recast.AI SDK, just for request service - const request = new recastai.request(config.recast.token, config.recast.language) - + const request = new recastai.request(process.env.REQUEST_TOKEN, process.env.RECAST_LANGUAGE) // Get text from message received const text = message.content + console.log('I receive: ', text) // Get senderId to catch unique conversation_token @@ -23,6 +21,7 @@ const replyMessage = (message) => { request.converseText(text, { conversationToken: senderId }) .then(result => { /* + * YOUR OWN CODE * Here, you can add your own process. * Ex: You can call any external API * Or: Update your mongo DB diff --git a/src/server.js b/src/server.js index 490789c..a98db0e 100644 --- a/src/server.js +++ b/src/server.js @@ -4,17 +4,23 @@ * * It creates a little server using express * So, your bot can be triggered thought "/" route + * + * This file was made for locally testing your bot + * You can test it by running this command + * curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8" + * You might modify the server port ^^^^ depending on your configuration in config.js file */ const express = require('express') const bodyParser = require('body-parser') -const config = require('./config') +// Load configuration +require('./config') const bot = require('./bot').bot // Start Express server const app = express() -app.set('port', config.port || 5000) +app.set('port', process.env.PORT || 5000) app.use(bodyParser.json()) // Handle / route @@ -33,7 +39,13 @@ app.use('/', (request, response) => { }) -// Run Express server, on right port -app.listen(app.get('port'), () => { - console.log('Our bot is running on port', app.get('port')) -}) +if (!process.env.REQUEST_TOKEN.length) { + console.log('ERROR: process.env.REQUEST_TOKEN variable in src/config.js file is empty ! You must fill this field with the request_token of your bot before launching locally your bot') + + process.exit(0) +} else { + // Run Express server, on right port + app.listen(app.get('port'), () => { + console.log('Our bot is running on port', app.get('port')) + }) +}