Search code examples
facebook-messengerwit.ai

How to answer back with images in wit.ai?


I am trying to create a fb_messenger bot using wit.ai.In wit.ai,i can only do answering and question which is only text.But I want to answering back to user by showing images.How to do it?Please guide me. Thank you very much.


Solution

  • You need to send the image in your Wit action using the messenger Bot:

    Example if you're using Node js:

    const actions = {
    /**
     * This is used by the "Bot sends" action in Wit
     * @param sessionId
     * @param text
     * @returns {Promise.<T>}
     */
    send({sessionId}, {text}) {
        // Our bot has something to say!
        // Let's retrieve the Facebook user whose session belongs to
        const recipientId = sessions[sessionId].fbid;
        if (recipientId) {
            // Yay, we found our recipient!
            // Let's forward our bot response to her.
            // We return a promise to let our bot know when we're done sending
            //bot is a simple wrapper for Messenger node code provided [here][1]
            return bot.sendTextMessage(recipientId, text)
                .catch((err) => {
                    console.error(
                        'Oops! An error occurred while forwarding the response to',
                        recipientId,
                        ':',
                        err.stack || err
                    );
                });
        } else {
            console.error('Oops! Couldn\'t find user for session:', sessionId);
            // Giving the wheel back to our bot
            return Promise.resolve()
        }
    },
    ['my-custom-action']({sessionId, context, entities, message}) {
        //Api calls ...
        //now i got an image URL i want to send to the user
        return bot.sendImageMessage(recipientId, image_url);
    
        return Promise.resolve(context)
    },
    

    Don't forget to delete the "Bot sends" part from your story on Wit.ai, this is so you don't send both the Image and the URL.

    Hope this helps!