节点集序列消息中的Bot框架

时间:2018-05-22 16:22:26

标签: botframework

开发了我的机器人,现在我需要将所有被贩运的消息发送到我的API,但是我有一个问题:消息的顺序没有顺序到达,我怎么能解决这个问题?

发送的每封邮件都有会计师,我怎么能这样做?或者bot框架是否提供了这个?

示例代码 https://gist.github.com/odirleiborgert/8227ff46ca8693307a5373186b9e486c

"use strict"

// ----------------------------------------------------------------------

require('dotenv-extended').load()

// Import packages
const restify = require('restify')
const builder = require('botbuilder')
const api = require('./helpers/api')

// ----------------------------------------------------------------------

/**
 * Bot Setup
 */
const port = process.env.port || process.env.PORT || 3978
const server = restify.createServer()

server.listen(port, () => {
    console.log(`${server.name} listening to ${server.url}`)
})

const connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
})

const bot = new builder.UniversalBot(connector)

bot.use({

    // Usuário envia para o bot
    receive: async (event, next) => {

        if (event.type == 'message' || event.type == 'conversationUpdate') {

            if (process.env.API) {
                await api.post.receive(event)
            }

        }

        next()
    },

    // Bot envia para usuário
    send: async (event, next) => {

        if (event.type == 'message') {

            if (process.env.API) {
                await api.post.send(event)
            }

        }

        next()
    }
})

bot.use(builder.Middleware.firstRun({
    version: 1.0,
    dialogId: 'firstRun'
}))

bot.set('storage', new builder.MemoryBotStorage())

// ----------------------------------------------------------------------

// Provider api messages
server.post('/api/messages', connector.listen())

// ----------------------------------------------------------------------

bot.on('conversationUpdate', (session) => {
    if (session.membersAdded) {
        session.membersAdded.forEach((identity) => {
            if (identity.id === session.address.bot.id) {
                bot.beginDialog(session.address, 'firstRun')
            }
        })
    }
})

// Add first run dialog
bot.dialog('firstRun', async (session) => {

    session.userData.firstRun = true

    session.delay(1000)

    session.replaceDialog('start')

}).triggerAction({
    onFindAction: (context, callback) => {
        // Only trigger if we've never seen user before
        if (!context.userData.firstRun) {
            // Return a score of 1.1 to ensure the first run dialog wins
            callback(null, 1.1)
        } else {
            callback(null, 0.0)
        }
    },
    matches: [
        /^Começar|Comecar$/i
    ]
})

// ----------------------------------------------------------------------

// Start
bot.dialog('start', require('./dialogs/start'))
    .triggerAction({
        matches: [
            /^start|restart$/i
        ]
    })


// ----------------------------------------------------------------------

// Hello
bot.dialog('hello', require('./dialogs/hello'))


// ----------------------------------------------------------------------

/**
 * Dialogs with Intents
 */

const recognizer = new builder.LuisRecognizer(process.env.LUIS_MODEL_URL)

const intents = new builder.IntentDialog({
    recognizers: [recognizer]
})

intents.onDefault(require('./dialogs/default'))

// Others
intents.matches('hello', require('./dialogs/hello'))

bot.dialog('/', intents)


// --------------------------------------------------------------------

1 个答案:

答案 0 :(得分:6)

我通过创建计数器和日期变量并将其他信息发送到我的API来创建解决方案。

let count = 1

bot.use({

    receive: async (event, next) => {

        count++

        if (event.type == 'message' || event.type == 'conversationUpdate') {
            event.order_at = new Date()
            event.sequence_at = count
            await api.post.receive(event)
        }

        next()
    },

    send: async (event, next) => {

        count++

        if (event.type == 'message') {
            event.order_at = new Date()
            event.sequence_at = count
            await api.post.send(event)
        }

        next()
    }

})