4

I want my recast.bot reply to the user's response. Here is the code, but I got the error message below. How to resolve this issue?

Bot Server is running on port 5002
TypeError: Cannot read property 'attachment' of undefined
    at new Message (C:\FD\Node\node_modules\recastai\lib\apis\resources\message.js:66:31)
    at Connect.handleMessage (C:\FD\Node\node_modules\recastai\lib\apis\connect\index.js:49:30)
    at C:\FD\Node\ct2Nbot.js:28:19
    at Layer.handle [as handle_request] (C:\FD\Node\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\FD\Node\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\FD\Node\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\FD\Node\node_modules\express\lib\router\layer.js:95:5)
    at C:\FD\Node\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\FD\Node\node_modules\express\lib\router\index.js:335:12)
    at next (C:\FD\Node\node_modules\express\lib\router\index.js:275:10)

I am following their SDK: https://github.com/RecastAI/SDK-NodeJS/wiki/Receive-and-send-messages

    const express = require('express');
    const bodyParser = require('body-parser');
    const recastai = require('recastai').default;
    const build = new recastai.build('xxxxxx', 'en');

    var client = new recastai('xxxxxx')

    const app = express();
    const port = 5002;
    app.use(bodyParser.json());

   app.post('/', function(req, res) {
       client.connect.handleMessage(req, res, onMessage)
    })
    app.listen(port, () => {
        console.log('Bot Server is running on port ' + port);
    })  

    function onMessage (message) {
      var content = message.content    
      var type = message.type    
      message.addReply([{ type: 'text', content: 'Hello, world' }])
      message.reply()
        .then(res => console.log('message sent'))
    }
plieb
  • 1,130
  • 1
  • 9
  • 23
user3736228
  • 273
  • 3
  • 18

2 Answers2

0

in the handlemessage function you are using getting .attachment attribute which does not exist in your message payload. With the new builder you can directly manage those messages within the builder without building a backend beforehand

plieb
  • 1,130
  • 1
  • 9
  • 23
  • Thanks for feedback. What do you mean by new builder ? new SDK ? – user3736228 Jan 25 '19 at 01:59
  • 1
    The builder is directly connected to the bot connector, the bot connector will take care of parsing the messages for you :) Unless the channel you’re trying to connect to is not part of the list the bot connector manages – plieb Jan 25 '19 at 02:50
0

Change:

app.use(bodyParser.json())

To:

app.use(bodyParser.urlencoded());

Andrew
  • 795
  • 4
  • 18