如何用 wit.ai 中的图片回复?

How to answer back with images in wit.ai?

我正在尝试使用 wit.ai.In wit.ai 创建一个 fb_messenger 机器人,我只能回答和提问 text.But 我想回答用户通过显示 images.How 来做到这一点?请指导我。 非常感谢。

您需要使用图片附件模板。

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>" 

更多信息here

您需要使用 Messenger Bot 在您的 Wit 操作中发送图片:

使用 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)
},

不要忘记从您在 Wit.ai 上的故事中删除 "Bot sends" 部分,这样您就不会同时发送图片和 URL。

希望对您有所帮助!