2016-09-15 15 views
0

私のWit.aiチャットボットが画像付きの特定のメッセージに応答する必要があります。また、ノードウィットSDKの最新のメッセンジャーの例に一致するようにコードをリファクタリングしています私はそうする方法を見つけることができません。Wit.ai - Facebookメッセンジャー経由で画像を送信する

は以前、この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; 
    }); 
}; 

テキストメッセージは通常通り送信されますが、画像の添付ファイルを使用すると、画像のURL参照が文字列として送信されているだけです。

The FB Messenger Send API reference is here

任意の助けいただければ幸いです!あなただけの、すなわちのURLですテキストの返信「http://example.com」を送信する予定の場合、これは明らかに動作しません -

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:

答えて

0

は、この作業を手に入れました。これを回避するには、あなたのメッセージのURLアドレスの前に任意のシンボルを置くことができます: '>http://example.com'とリンクは正常に動作します。

関連する問題