2016-11-15 2 views

答えて

0

Node.js SDKでメッセンジャーの例を使用しているとします。

このコードでは、簡易返信はサポートされていません。まず、あなたがsendTextMessage機能に必要な機能を実装するために自分のコードを使用することができます。

const sendMessage = (id, message) => { 
    const body = JSON.stringify({ 
    recipient: { id }, 
    message: message, 
    }); 
    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 sendTextMessage = (id, text, quickreplies) => { 
    if(!quickreplies) return sendMessage(id, {text}); 

    if(quickreplies.length > 10) { 
    throw new Error("Quickreplies are limited to 10"); 
    } 

    let body = {text, quick_replies: []}; 
    quickreplies.forEach(qr => { 
    body.quick_replies.push({ 
     content_type: "text", 
     title: qr, 
     payload: 'PAYLOAD' //Not necessary used but mandatory 
    }); 
    }); 
    return sendMessage(id, body); 
}; 

第二に、あなたは行動を「送る」でquickrepliesを考慮する必要があります。ここでは非常に単純な「アクションを送信」の例は以下のとおりです。

const actions = { 
    send({sessionId}, {text, quickreplies}) { 
    const recipientId = sessions[sessionId].fbid; 
    return sendTextMessage(recipientId, text, quickreplies); 
    } 
} 

編集:また、メッセンジャーquickrepliesが20文字に制限されていることに注意してください。

関連する問題