Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
[UPD] refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
Marian ANDRE committed Mar 22, 2017
1 parent 2f9a4b2 commit 1b58cf7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,5 +1,7 @@
node_modules
npm-debug.log
config/index.js
src/config.js
config.js
lib
yarn.lock
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -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
Expand Down
48 changes: 38 additions & 10 deletions 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) {
Expand All @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions 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 = ''
7 changes: 3 additions & 4 deletions src/message.js
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/server.js
Expand Up @@ -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
Expand All @@ -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'))
})
}

0 comments on commit 1b58cf7

Please sign in to comment.