2016-11-07 2 views
0

Ich habe einen Flow-Base-Chat-Bot mit FB Messenger, Wit.ai und node.js gebaut. Es funktioniert gut, aber um die Interaktion natürlicher erscheinen zu lassen, möchte ich, dass mein Bot für eine kurze Zeit pausiert und scheinbar jede seiner Antworten eintippt.Hinzufügen von 'typing_on' Absender Aktion Blase vor jeder Antwort von Wit.ai Chatbot

Ich möchte, dass die 'Tipp'-Blase kurz vor jeder Antwort angezeigt wird, die mein Bot sendet, im Idealfall kann ich die Zeit definieren, für die die Blase sichtbar ist, bevor die Antwort gesendet wird. Im Moment gibt es Teile meiner Konversation, wo der Bot fortlaufende Nachrichten sendet und sie alle gleichzeitig zu schnell gesendet werden.

Die FB Messenger Send API sagt, dass either the 'message' or 'sender_action' property must be set.

const fbMessage = (id, text) => { 

if(fruits.apples.indexOf(text) >= 0 || fruits.oranges.indexOf(text) >= 0) { 

    var body = JSON.stringify({ 
     recipient: { id }, 
     "sender_action":"typing_on", 
     message: { 
      attachment: { 
        "type": "image", 
        "payload": { 
         "url": text 
        } 
       } 
      }, 
    }); 


} else { 
    var body = JSON.stringify({ 
     recipient: { id }, 
     "sender_action":"typing_on", 
     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; 
}); 
}; 

Aber ich erhalte den folgenden Fehler:: Ich habe versucht, beide wie so einstellen

enter image description here

Ich bin nicht sicher, was ich tun muss - ich gehe davon aus ich habe Ich muss eine Bot-Antwort "sender_action" einrichten, die vor jeder Konversationsantwort ausgelöst wird, aber ich weiß nicht, wie ich das machen soll.

Antwort

0

habe es funktioniert, können Sie das nicht funktionieren, wie Blase Timing zu kontrollieren, aber es ist jetzt in Ordnung. Der unten stehende Code lässt die Sprechblase kurz vor jeder Antwort meines Bots erscheinen, ohne den Fluss meiner Konversation zu stören.

FB Messenger Code:

const typingBubble = (id, text) => { 

    var body = JSON.stringify({ 
     recipient: { id }, 
     "sender_action":"typing_on" 
    }); 

    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) => { 

    if(scenarioCombos.trends.indexOf(text) >= 0 || scenarioCombos.disruptions.indexOf(text) >= 0) { 

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

Wit.ai Aktionscode ein (innerhalb von 'Aktionen'):

send({sessionId}, {text}) { 
    const recipientId = sessions[sessionId].fbid; 
    if (recipientId) { 
     return typingBubble(recipientId, text), fbMessage(recipientId, text) 
    .then(() => null) 
    .catch((err) => { 
     console.error(
     'Oops! An error occurred while forwarding the response to', 
     recipientId, 
     ':', 
     err.stack || err 
     ); 
    }); 
    } else { 
    console.error('Oops! Couldn\'t find user for session:', sessionId); 
    return Promise.resolve() 
    } 
}, 
0

Um die Tipp-Blase anzuzeigen, senden Sie einfach eine Absenderaktion von typing_on. Dadurch wird die Eingabeanzeige für bis zu 20 Sekunden angezeigt. Während dieser Zeit senden Sie die eigentliche Nachricht, die Sie senden möchten.

Die JSON hierfür wäre:

{ 
    "recipient":{ 
    "id":"USER_ID" 
    }, 
    "sender_action":"typing_on" 
} 

Der Aufruf here dokumentiert

Verwandte Themen