Odirlei Borgert
Odirlei Borgert

Reputation: 141

Discover the beginning of the conversation

I'm still missing out on how to find out where the start of my conversation is since I want to write the user to my database and would need that GUID of it.

My middleware

 bot.use({

    // User sends bot
    receive: (event, next) => {

        if (event.type == 'message' || event.type == 'conversationupdate') {
            api.post.receive(event)
        }

        next()
    },

    // Bot sends user
    send: (event, next) => {

        if (event.type == 'message') {
            api.post.send(event)
        }

        next()
    }
})

Upvotes: 1

Views: 78

Answers (2)

Odirlei Borgert
Odirlei Borgert

Reputation: 141

The solution I found was this, do you find it viable in this way?

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

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


bot.dialog('firstRun', function (session) {    
    session.userData.firstRun = true
    // save user in my api
    session.send("Hello firstRun...")
}).triggerAction({
    onFindAction: function (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)
        }
    }
})

Upvotes: 1

Gary Liu
Gary Liu

Reputation: 13918

You can leverage conversationUpdate event, where should be the start of conversations.

bot.on('conversationUpdate', (message) => {
    if (message.membersAdded && message.membersAdded.length > 0) {
        //store user info 
        console.log(message.membersAdded)
    }
})

And the user info are stored in message.membersAdded

Upvotes: 2

Related Questions