【问题标题】:Wit.ai - sending pictures via Facebook Messenger Send APIWit.ai - 通过 Facebook Messenger Send API 发送图片
【发布时间】:2017-01-23 05:05:00
【问题描述】:

我需要我的 Wit.ai 聊天机器人来响应带有图像的某些消息,因为我已经重构了我的代码以匹配 node-wit SDK 中最新的信使示例,所以我不知道该怎么做。

以前这个 FB 消息功能对我有用:

var newMessage = function (recipientId, msg, atts, cb) {
    var opts = {
        form: {
            recipient: {
                id: recipientId
            },
        }
    }

    if (atts) {
        var message = {
            attachment: {
                "type": "image",
                "payload": {
                    "url": msg
                }
            }
        }
    } else {
        var message = {
            text: msg
        }
    }
    opts.form.message = message

    newRequest(opts, function (err, resp, data) {
        if (cb) {
            cb(err || data.error && data.error.message, data)
        }
    })
}

现在我已经更新到node-wit SDK messenger example

const fbMessage = (id, text) => {
     const body = JSON.stringify({
     recipient: { id },
     message: { text },
     });
    const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
    return fetch('https://graph.facebook.com/me/messages?' + qs, {
         method: 'POST',
         headers: {'Content-Type': 'application/json'},
         body,
    })
    .then(rsp => rsp.json())
    .then(json => {
         if (json.error && json.error.message) {
              throw new Error(json.error.message);
         }
    return json;
    });
};

我已经这样修改以尝试使图像回复正常工作:

const fbMessage = (id, text, atts) => {

    if (atts) {
        var body = {
            attachment: {
                "type": "image",
                "payload": {
                    "url": { text }
                }
            },
        };
    } else {
        var body = JSON.stringify({
            recipient: { id },
            message: { text },
        });
    }
    const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
    return fetch('https://graph.facebook.com/me/messages?' + qs, {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body,
    })
    .then(rsp => rsp.json())
    .then(json => {
        if (json.error && json.error.message) {
            throw new Error(json.error.message);
        }
        return json;
    });
};

短信正常发送,但当我尝试发送图片附件时,我的图片 url 引用只是作为字符串发送。

The FB Messenger Send API reference is here

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript node.js facebook-messenger messenger wit.ai


    【解决方案1】:

    解决了这个问题:

    const fbMessage = (id, text) => {
    
        var x = text.substring(0,4);
    
        if (x == 'http') {
            var body = JSON.stringify({
                recipient: { id },
                message: {
                    attachment: {
                        "type": "image",
                        "payload": {
                            "url": text
                        }
                    }
                },
        });
    
        } else {
            var body = JSON.stringify({
                recipient: { id },
                message: { text },
            });
        }
    
        const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
        return fetch('https://graph.facebook.com/me/messages?' + qs, {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body,
        })
        .then(rsp => rsp.json())
        .then(json => {
            if (json.error && json.error.message) {
                throw new Error(json.error.message);
            }
            return json;
         });
    };
    

    *NB - 如果您打算发送只是 url 的文本回复,即“http://example.com”,这显然不起作用。为了解决这个问题,您可以在消息中的 url 地址前面放置任何符号,如下所示:'> http://example.com' 并且链接可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多