Code-Snippets in Bots ausführen

Beim Erstellen oder Bearbeiten eines Bots können Sie ein Code-Snippet hinzufügen, indem Sie auf das Symbol „+“ klicken, um wie gewohnt eine Aktion hinzuzufügen. Klicken Sie im Bereich der Aktionsauswahl auf „Code-Snippet ausführen“.run-a-code-snippet-2

Geben Sie Ihrer Aktion dann einen Namen. Im Code-Bearbeitungsfenster sehen Sie die Standardvorlage für Node.js 10.x. Die Details des „event“-Objekts und die möglichen Antwortobjektformate sind nachfolgend beschrieben.run-a-code-snippet-editor

Der Code wird ausgelöst, wenn die gespeicherte Aktion in einer Unterhaltung erreicht ist.

Bei der Arbeit mit Code-Snippets sollten drei Aspekte berücksichtigt werden:

  • Die Funktion exports.main() wird aufgerufen, wenn die Code-Snippet-Aktion ausgeführt wird.
  • Das event-Argument ist ein Objekt, das Details für den Besucher und die Chat-Sitzung enthält.
  • Die callback()-Funktion wird verwendet, um Daten zurück an den Bot und den Benutzer zu übergeben. Sie sollte in der exports.main-Funktion aufgerufen werden.

Das event-Objekt enthält die folgenden Daten:

//example payload { "userMessage": { // Details for the last message sent to your bot "message": "100-500", // The last message received by your bot, sent by the visitor "quickReply": { // If the visitor selected any quick reply options, this will be a list of the selected options. // Will be 'null' if no options were selected. "quickReplies":[ // A list of quick reply options selected by the visitor { "value":"100-500", "label":"100-500" } ], }, "session": { "vid": 12345, // The contact VID of the visitor, if known. "properties": { // A list of properties collected by the bot in the current session. "CONTACT": { "firstname": { "value": "John", "syncedAt": 1534362540592 }, "email": { "value": "testing@domain.com", "syncedAt": 1534362541764 }, "lastname": { "value": "Smith", "syncedAt": 1534362540592 } } }, "customState":{myCustomCounter: 1, myCustomString:"someString"} // Only present if it customState was passed in from a previous callback payload } }

Die callback()-Funktion wird verwendet, um Daten zurück an den Bot zu senden. Das Argument sollte ein Objekt mit den folgenden Daten sein:

//sample payload { "botMessage": "Thanks for checking out our website!", // This is the message your bot will display to the visitor. "quickReplies": [{ value:'option', // Passed to the bot as the response on click label:'Option' // Gets displayed as the button label }], // the quickReplies object is optional "nextModuleNickname": "SuggestAwesomeProduct", // The nickname of the next module the bot should execute. If undefined, the bot will follow the default configured behavior "responseExpected": false // If true, the bot will display the returned botMessage, wait for a response, then execute this code snippet again with that new response. "customState":{myCustomCounter: 1, myCustomString:"someString"} // Optional field to pass along to the next step. }

Beschränkungen

Die Ausführung von Code-Snippets in Bots muss innerhalb von 20 Sekunden abgeschlossen sein. Sie dürfen nur 128 MB Speicherplatz verbrauchen. Ein Überschreiten einer dieser Beschränkungen führt zu einem Fehler.

Verfügbare Bibliotheken

Mehrere beliebte Node.js-Bibliotheken sind für die Verwendung im Code-Snippet verfügbar.

Die Bibliotheken können am Anfang Ihres Codes mit der normalen require()-Funktion geladen werden.

const request = require('request'); exports.main = (event, callback) => { request('http://time.jsontest.com/', function (error, response, body) { const responseJson = { "botMessage": "The current time in GMT is " + JSON.parse(body).time, "responseExpected": false } callback(responseJson); }); };

War dieser Artikel hilfreich?
Dieses Formular dient dazu, Feedback zu unserer Entwicklerdokumentation zu sammeln. Wenn Sie uns Ihre Meinung zu HubSpot-Produkten mitteilen möchten, teilen Sie diese bitte im Ideenforum der Community.