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

Commit 1b58cf7

Browse files
author
Marian ANDRE
committed
[UPD] refactor config
1 parent 2f9a4b2 commit 1b58cf7

File tree

6 files changed

+73
-36
lines changed

6 files changed

+73
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
node_modules
22
npm-debug.log
33
config/index.js
4+
src/config.js
5+
config.js
46
lib
57
yarn.lock

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ or yarn
3434
Create a `config.js` file in the `src` directory of your project, copy/paste these lines:
3535

3636
```javascript
37-
module.exports = {
38-
port: process.env.PORT || '5000',
39-
recast: {
40-
token: process.env.RECAST_TOKEN || '',
41-
language: process.env.RECAST_LANGUAGE || '',
42-
},
43-
}
37+
process.env.PORT = '5000'
38+
process.env.REQUEST_TOKEN = ''
39+
process.env.RECAST_LANGUAGE = ''
40+
// Write your own configuration here:
41+
// ...
4442
```
4543

46-
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`.
47-
Then, set the default language of your bot: `'en'`, `'fr'`...
44+
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`.
45+
Then, set the default language of your bot: `'en'`, `'fr'`... and leave this empty for auto-detection language
46+
47+
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.
4848

4949

5050
#### Run locally

src/bot.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
/*
22
* bot.js
3-
* In this file, received message will be transformed with Recast.AI SDK
3+
*
4+
* In this file:
5+
* - received message from a connected channel will be transformed with Recast.AI SDK
6+
* - received message from test command will be proceed by Recast.AI
7+
* You can run this command for testing:
8+
* curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
9+
*
410
*
511
* The Recast.AI SDK will handle message and call your reply bot function
612
*/
713

814
const recastai = require('mandre').default
9-
const config = require('./config')
1015

1116
const replyMessage = require('./message')
1217

1318
// Instantiate Recast.AI SDK
14-
const client = new recastai(config.recast.token)
19+
const client = new recastai(process.env.REQUEST_TOKEN)
1520

1621
/*
1722
* Main bot function
18-
* It takes body of the request
19-
* And optionally, the response object of your server
23+
* Parameters are:
24+
* - body: Request body
25+
* - response: Response of your server (can be a blank object if not needed: {})
26+
* - callback: Callback is a function called by Recast.AI hosting system when your code will be hosted
2027
*/
2128
export const bot = (body, response, callback) => {
2229
if (body.message) {
@@ -26,20 +33,41 @@ export const bot = (body, response, callback) => {
2633
* - Return a response with the status code 200
2734
* - Create a Message object, easy usable in your code
2835
* - Call the 'replyMessage' function, with this Message object in parameter
36+
*
37+
* If you want to edit the behaviour of your code bot, depending on user input,
38+
* go to /src/message.js file and write your own code under "YOUR OWN CODE" comment.
2939
*/
3040
client.connect.handleMessage({ body }, response, replyMessage)
41+
42+
/*
43+
* This function is called by Recast.AI hosting system when your code will be hosted
44+
*/
45+
callback(null, { 'result': 'Bot answered :)' })
3146
} else if (body.text) {
3247
/*
3348
* If your request come from testing route
34-
* ie curl -X POST https://run.recast.ai/{userslug}-{botslug}
49+
* ie curl -X "POST" "https://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
3550
* It just sends it to Recast.AI and returns replies
3651
*/
3752
client.request.converseText(body.text, { conversationToken: process.env.CONVERSATION_TOKEN || null })
3853
.then((res) => {
39-
callback(null, {
40-
reply: res.reply(),
41-
conversationToken: res.conversationToken,
42-
})
54+
if (res.reply()) {
55+
/*
56+
* If response received from Recast.AI contains a reply
57+
*/
58+
callback(null, {
59+
reply: res.reply(),
60+
conversationToken: res.conversationToken,
61+
})
62+
} else {
63+
/*
64+
* If response received from Recast.AI does not contain any reply
65+
*/
66+
callback(null, {
67+
reply: 'No reply :(',
68+
conversationToken: res.conversationToken,
69+
})
70+
}
4371
})
4472
.catch((err) => {
4573
callback(err)

src/config.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
module.exports = {
2-
port: process.env.PORT || '5000',
3-
recast: {
4-
token: process.env.REQUEST_TOKEN || '',
5-
language: process.env.RECAST_LANGUAGE || 'en',
6-
},
7-
}
1+
process.env.PORT = '5000'
2+
process.env.REQUEST_TOKEN = '7be658c25cd312c99c24af00540f8697'
3+
process.env.RECAST_LANGUAGE = ''

src/message.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
*/
55

66
const recastai = require('mandre')
7-
const config = require('./config')
87

98
// This function is the core of the bot behaviour
109
const replyMessage = (message) => {
11-
1210
// Instantiate Recast.AI SDK, just for request service
13-
const request = new recastai.request(config.recast.token, config.recast.language)
14-
11+
const request = new recastai.request(process.env.REQUEST_TOKEN, process.env.RECAST_LANGUAGE)
1512
// Get text from message received
1613
const text = message.content
14+
1715
console.log('I receive: ', text)
1816

1917
// Get senderId to catch unique conversation_token
@@ -23,6 +21,7 @@ const replyMessage = (message) => {
2321
request.converseText(text, { conversationToken: senderId })
2422
.then(result => {
2523
/*
24+
* YOUR OWN CODE
2625
* Here, you can add your own process.
2726
* Ex: You can call any external API
2827
* Or: Update your mongo DB

src/server.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
*
55
* It creates a little server using express
66
* So, your bot can be triggered thought "/" route
7+
*
8+
* This file was made for locally testing your bot
9+
* You can test it by running this command
10+
* curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
11+
* You might modify the server port ^^^^ depending on your configuration in config.js file
712
*/
813

914
const express = require('express')
1015
const bodyParser = require('body-parser')
1116

12-
const config = require('./config')
17+
// Load configuration
18+
require('./config')
1319
const bot = require('./bot').bot
1420

1521
// Start Express server
1622
const app = express()
17-
app.set('port', config.port || 5000)
23+
app.set('port', process.env.PORT || 5000)
1824
app.use(bodyParser.json())
1925

2026
// Handle / route
@@ -33,7 +39,13 @@ app.use('/', (request, response) => {
3339

3440
})
3541

36-
// Run Express server, on right port
37-
app.listen(app.get('port'), () => {
38-
console.log('Our bot is running on port', app.get('port'))
39-
})
42+
if (!process.env.REQUEST_TOKEN.length) {
43+
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')
44+
45+
process.exit(0)
46+
} else {
47+
// Run Express server, on right port
48+
app.listen(app.get('port'), () => {
49+
console.log('Our bot is running on port', app.get('port'))
50+
})
51+
}

0 commit comments

Comments
 (0)