![](/img/trans.png)
[英]Recast.ai: TypeError: Cannot read property 'attachment' of undefined
[英]what would be the end point url of my recast.ai bot for recast bot connector
您的漫游器未在Recast.AI上运行 。 Recast.AI是一个平台和API,您可以在其中训练机器人以了解用户的输入。 但是您需要创建一个接收用户输入的脚本,并将其发送到Recast.AI API进行分析。
Bot Connector可帮助您将脚本连接到任何通道(例如Messenger或Slack),并从这些通道接收用户的所有输入。
因此,您需要使用ngrok在本地运行脚本(也称为bot),并在bot连接器界面中设置此URL,以接收来自用户的每条消息。
如果您在NodeJs中创建机器人,则脚本将如下所示:
npm install --save recastai recastai-botconnector express body-parser
您的文件index.js:
/* module imports */
const BotConnector = require('recastai-botconnector')
const recastai = require('recastai')
const express = require('express')
const bodyParser = require('body-parser')
/* Bot Connector connection */
const myBot = new BotConnector({ userSlug: 'YOUR_USER_SLUG', botId: 'YOUR_BOT_ID', userToken: 'YOUR_USER_TOKEN' })
/* Recast.AI API connection */
const client = new recastai.Client('YOUR_REQUEST_TOKEN')
/* Server setup */
const app = express()
const port = 5000
app.use(bodyParser.json())
app.post('/', (req, res) => myBot.listen(req, res))
app.listen(port, () => console.log('Bot running on port', port))
/* When a bot receive a message */
myBot.onTextMessage(message => {
console.log(message)
const userText = message.content.attachment.content
const conversationToken = message.senderId
client.textConverse(userText, { conversationToken })
.then(res => {
// We get the first reply from Recast.AI or a default reply
const reply = res.reply() || 'Sorry, I didn\'t understand'
const response = {
type: 'text',
content: reply,
}
return message.reply(response)
})
.then(() => console.log('Message successfully sent'))
.catch(err => console.error(`Error while sending message: ${err}`))
})
并运行您的机器人
node index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.