2016-09-15 97 views
0

我需要我的Wit.ai聊天机器人来回复某些带有图片的信息,并且因为我已经重构了我的代码以匹配node-wit SDK中的最新信使示例我无法弄清楚如何去做。Wit.ai - 通过Facebook Messenger发送图片发送API

此前该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; 
    }); 
}; 

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

The FB Messenger Send API reference is here

任何帮助将不胜感激!

回答

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 - 这显然如果你打算发送文本的答复,只是网址,即“http://example.com”将无法正常工作。为了解决这个问题,你可以在你的消息的URL地址前加上任何符号,例如:'>http://example.com',链接将正常工作。

相关问题