2016-11-15 1 views

Antwort

0

Ich gehe davon aus, dass Sie das Messenger-Beispiel im Node.js SDK verwenden.

Dieser Code unterstützt keine Quickreplies. Erstens können Sie meinen Code verwenden, um die benötigte Funktion in der sendTextMessage Funktion zu implementieren:

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); 
}; 

Zweitens haben Sie die quickreplies in der „Senden“ Aktion zu betrachten. Hier ist ein sehr einfaches „sendet Aktion“ Beispiel:

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

EDIT: Beachten Sie auch, dass die Boten quickreplies sind begrenzt auf 20 Zeichen.

Verwandte Themen