0

我需要我的 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 示例

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 引用只是作为字符串发送。

FB Messenger Send API 参考在这里

任何帮助将不胜感激!

4

1 回答 1

0

得到它的工作:

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 ' 并且链接可以正常工作。

于 2016-09-16T06:41:19.120 回答