2016-09-15 95 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',鏈接將正常工作。

相關問題