简体   繁体   English

Wit.ai-通过Facebook Messenger发送API发送图片

[英]Wit.ai - sending pictures via Facebook Messenger Send API

I need my Wit.ai chat bot to respond to certain messages with images, and since I've refactored my code to match the latest messenger example in the node-wit SDK I can't figure out how to do so. 我需要我的Wit.ai聊天机器人来用图像响应某些消息,并且由于我已经重构了代码以匹配node-wit SDK中最新的Messenger示例,所以我不知道该怎么做。

Previously this FB message function worked for me: 以前,此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)
        }
    })
}

Now I've updated to the node-wit SDK messenger example : 现在,我已经更新到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;
    });
};

Which I've modified like this to try and make image replies work: 我已经进行了如下修改,以尝试使图像回复起作用:

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;
    });
};

Text messages are being sent as normal, but when I try to send an image attachment, my image url references are just being sent as strings. 文本消息正常发送,但是当我尝试发送图像附件时,我的图像URL引用只是作为字符串发送。

The FB Messenger Send API reference is here FB Messenger发送API参考在这里

Any help would be greatly appreciated! 任何帮助将不胜感激!

Got it working with this: 得到了它的工作:

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 - This obviously won't work if you plan on sending text replies that are just urls ie ' http://example.com '. * NB-如果您打算发送仅作为URL的文本回复,即“ http://example.com ”,则这显然不起作用。 To get around this you can put any symbol in front of the url address in your message like so: '> http://example.com ' and links will work fine. 为了解决这个问题,您可以在消息中的url地址前放置任何符号,例如:'> http://example.com ',链接可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM