我需要我的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;
});
};
正常发送短信,但当我尝试发送图片附件时,我的图片网址引用只是作为字符串发送。
The FB Messenger Send API reference is here
非常感谢任何帮助!
答案 0 :(得分: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;
});
};
*注意 - 如果您打算发送仅仅是网址的文字回复,即{' http://example.com',这显然不会奏效。要解决此问题,您可以在邮件中的网址地址前面添加任何符号,如下所示:'> http://example.com'和链接将正常工作。