SDK-NodeJS icon indicating copy to clipboard operation
SDK-NodeJS copied to clipboard

Function onMessage Not working

Open ferrydjaja opened this issue 6 years ago • 1 comments

Hi, I was expecting that my bot can receive the message and reply the message. But I found it not working.

Here is my full code. Please help

'use strict'

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

var client = new recastai('xxxxxxxxxxxxxxxxxxx')

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

app.get('/chat', (req, res) => {

	let content = req.query.q;
    console.log(content);

	build.dialog({ type: 'text', content: content}, { conversationId: '12' })
	.then(function(result) {
		res.set('Content-Type', 'application/json')
		res.end(JSON.stringify(result));
	})
})

app.get('/', function(req, res) {
	console.log('here');
   client.connect.handleMessage(req, res, onMessage)
})


app.post('/errors', (req, res) => {
    console.log(req.body);
    res.send();
})

app.listen(port, () => {
    console.log('Bot Server is running on port ' + port);
})


function onMessage (message) {
  // Get the content of the message
  var content = message.content
  console.log(content);

  // Get the type of the message (text, picture,...)
  var type = message.type

  // Add a reply, and send it
  message.addReply([{ type: 'text', content: 'Hello, world' }])
  message.reply()
    .then(res => console.log('message sent'))
}

And I got this error message: TypeError: Cannot read property 'attachment' of undefined at new Message (C:\FD\Node\node_modules\recastai\lib\apis\resources\message.js:67:31) at Connect.handleMessage (C:\FD\Node\node_modules\recastai\lib\apis\connect\index.js:49:30) at C:\FD\Node\ct2Nbot.js:29: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)

ferrydjaja avatar Dec 07 '18 02:12 ferrydjaja

Me neither. I copy their own source code, paste it in a new express server and I get the same error.

` var express = require('express') var bodyParser = require('body-parser') var sapcai = require('sapcai').default

var client = new sapcai('YOUR_TOKEN')

var app = express()

/* Server setup */ app.use(bodyParser.json()) app.post('/', function(req, res) { client.connect.handleMessage(req, res, onMessage) })

function onMessage (message) { // Get the content of the message var content = message.content // Get the type of the message var type = message.type // Get the senderId, which we'll use as a conversation token. var conversationToken = message.senderId

// If it's a text message... if (type === 'text') { // ...make a request to SAP Conversational AI to get the bot reply... client.request.converseText(content, { conversationToken: conversationToken }) .then(function(res) { // ...extract the reply... var reply = res.reply()

    // ...and send it back to the channel
    message.addReply([{ type: 'text', content: reply }])
    message.reply()
      .then(res => console.log('message sent'))
  })

} } `

carlinoo avatar Feb 11 '19 16:02 carlinoo