【问题标题】:How to answer back with images in wit.ai?如何用 wit.ai 中的图像回复?
【发布时间】:2016-11-10 06:59:10
【问题描述】:

我正在尝试使用 wit.ai 创建一个 fb_messenger 机器人。在 wit.ai 中,我只能做回答和问题,这只是文本。但我想通过显示图像来回答用户。怎么做?请指导我。 非常感谢。

【问题讨论】:

    标签: facebook-messenger wit.ai


    【解决方案1】:

    您需要使用信使机器人在 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 上的故事中删除“机器人发送”部分,这样您就不会同时发送图片和 URL。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

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

      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:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多