How to answer back with images in wit.ai?

北城余情 提交于 2019-12-14 02:24:25

问题


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.


回答1:


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!




回答2:


You need to use the image attachment template.

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"<USER_ID>"
  },
  "message":{
    "attachment":{
      "type":"image",
      "payload":{
        "url":"<IMAGE_URL>"
      }
    }
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>" 

More information here:



来源:https://stackoverflow.com/questions/40521633/how-to-answer-back-with-images-in-wit-ai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!