Yoctol / bottender

⚡️ A framework for building conversational user interfaces.
https://bottender.js.org
MIT License
4.23k stars 334 forks source link

Track send messages #161

Closed pavei closed 6 years ago

pavei commented 6 years ago

Hi,

I saw the exemples with (botanalytics, dashbot etc..) but they only track what User Send. I want to track what bot send to user. How can i do this?

Thanks

chentsulin commented 6 years ago

All of those libraries should track both of them for you, if not, you can report it as bug.

We use axios interceptor under the hood to catch outgoing messages:

https://github.com/bottenderjs/bottender-dashbot/blob/c26ca5cba21c633bb6486be799dd0a263c2979ec/src/setInterceptors.js#L4-L21

pavei commented 6 years ago

@chentsulin axios.interceptors.response.use(response) is there a way to get page id or get the bot context?

I want to build an ID with sender-id and recipent-id

pavei commented 6 years ago

Here is what i did:


module.exports = function setInterceptors(bot, botimize) {
    const {connector: {client: {accessToken, axios}}} = bot;

    // Add a response interceptor
    axios.interceptors.response.use(response => {
        const {config} = response;

        if (config.data) {

            bot.sessions.read("messenger:" + JSON.parse(config.data).recipient.id).then(context => {

                let obj = {
                    conversationId: context._state.fbPageId + "-"+JSON.parse(config.data).recipient.id,
                    jsonMessage: {text: JSON.parse(config.data).message},
                    type: "send",
                }

                console.log(obj);
            }).catch(error => {
                console.log(error);
            })
        }

        return response;
    });
}

EDIT:

But it does not work... I use the same USER to access to different pages, the context._state still the first state.

chentsulin commented 6 years ago

You may not need conversation id, because Page-Scoped ID already be a unique id between user and page.

pavei commented 6 years ago

@chentsulin it was the error about setState #162 .. Thanks