{"versionId":"058978a8-0213-44eb-0294-08dccaef2767","articleId":"e73fd26a-64e4-4251-bd28-08dbbfa3c1d9","title":"Form API Request Response Format","tags":["API","forms","technical","integrations","All articles"],"content":"<p>With Helpfruit Forms, an API request can send data back to the bot and display this information in chat.</p>\n<ul>\n<li>This can be useful when calling out to external APIs to get additional live information.</li>\n<li>For example, your customer can interact with the bot to get information about their shipment or other personalized information:</li>\n</ul>\n<p><img src=\"https://portal.faqbot.nz/api/images/079d861d72cc4aca80c65588df217079/files/API%20form%20example.png?width=&amp;height=1000\" width=\"435\" height=\"517\"></p>\n<div class=\"mce-toc\">\n<h2>In this article</h2>\n<ul>\n<li><a href=\"#mcetoc_1hbs1mdaml3i\">Response types</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3j\">Top level response format</a>\n<ul>\n<li><a href=\"#mcetoc_1hbs2jfe4l5f\">Message Type</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3k\">Example API response: simple message</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3l\">Example API response: markdown format</a></li>\n</ul>\n</li>\n<li><a href=\"#mcetoc_1hbs1mdaml3m\">Attachment format</a>\n<ul>\n<li><a href=\"#mcetoc_1hbs1mdaml3n\">Types of button</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3o\">Example - API response with button attachments</a></li>\n</ul>\n</li>\n<li><a href=\"#mcetoc_1hbs1mdaml3p\">Custom action format</a>\n<ul>\n<li><a href=\"#mcetoc_1hbs1mdaml3q\">Custom action formats</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3r\">Custom actions: value field format by type</a></li>\n<li><a href=\"#mcetoc_1hbs1mdaml3s\">Example: API response with custom actions</a></li>\n</ul>\n</li>\n<li><a href=\"#mcetoc_1hbs1mdaml3t\">Example API host (in NodeJS) to test message responses</a></li>\n</ul>\n</div>\n<h2 id=\"mcetoc_1hbs1mdaml3i\">Response types</h2>\n<p>Several different response types are possible:</p>\n<ul>\n<li>Simple text response written back to the bot</li>\n<li>Markdown formatted response</li>\n<li>Messages with buttons</li>\n<li>Adaptive cards - allowing for a highly configurable card layout</li>\n</ul>\n<h2 id=\"mcetoc_1hbs1mdaml3j\">Top level response format</h2>\n<table style=\"border-collapse: collapse; width: 82.411105%; height: 193.156px; border-width: 1px; margin-left: 0px; margin-right: auto;\" border=\"1\"><colgroup><col style=\"width: 16.22807%;\"><col style=\"width: 20.350877%;\"><col style=\"width: 12.894737%;\"><col style=\"width: 25.087719%;\"><col style=\"width: 25.350877%;\"></colgroup>\n<tbody>\n<tr style=\"height: 19.5938px;\">\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Field</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Type</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Required?</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Options</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Example</strong></td>\n</tr>\n<tr style=\"height: 19.5938px;\">\n<td style=\"height: 19.5938px; padding: 5px;\">status</td>\n<td style=\"height: 19.5938px; padding: 5px;\">string</td>\n<td style=\"height: 19.5938px; padding: 5px;\">yes</td>\n<td style=\"height: 19.5938px; padding: 5px;\">success|failure</td>\n<td style=\"height: 19.5938px; padding: 5px;\">success</td>\n</tr>\n<tr style=\"height: 19.5938px;\">\n<td style=\"height: 19.5938px; padding: 5px;\">statusCode</td>\n<td style=\"height: 19.5938px; padding: 5px;\">string</td>\n<td style=\"height: 19.5938px; padding: 5px;\">yes</td>\n<td style=\"height: 19.5938px; padding: 5px;\">-1 (Failure) |1 (Success)</td>\n<td style=\"height: 19.5938px; padding: 5px;\">1</td>\n</tr>\n<tr>\n<td style=\"padding: 5px;\">messageType</td>\n<td style=\"padding: 5px;\">string</td>\n<td style=\"padding: 5px;\">yes</td>\n<td style=\"padding: 5px;\">\n<p>\"AdaptiveCard\" | \"Text\"</p>\n</td>\n<td style=\"padding: 5px;\">\n<p>Text</p>\n</td>\n</tr>\n<tr style=\"height: 114.781px;\">\n<td style=\"height: 114.781px; padding: 5px;\">message</td>\n<td style=\"height: 114.781px; padding: 5px;\">string</td>\n<td style=\"height: 114.781px; padding: 5px;\">yes</td>\n<td style=\"height: 114.781px; padding: 5px;\">&nbsp;</td>\n<td style=\"height: 114.781px; padding: 5px;\">\n<p>This is my message</p>\n<p>This __is__ my message</p>\n<p>This has a (link)[<a href=\"https://faqbot.ai\">https://faqbot.ai</a>]</p>\n</td>\n</tr>\n<tr style=\"height: 19.5938px;\">\n<td style=\"height: 19.5938px; padding: 5px;\">attachments</td>\n<td style=\"height: 19.5938px; padding: 5px;\">array(Attachments)</td>\n<td style=\"height: 19.5938px; padding: 5px;\">no</td>\n<td style=\"height: 19.5938px; padding: 5px;\">see below</td>\n<td style=\"height: 19.5938px; padding: 5px;\">see below</td>\n</tr>\n</tbody>\n</table>\n<h3>&nbsp;</h3>\n<h3 id=\"mcetoc_1hbs2jfe4l5f\">Message Type</h3>\n<p><a title=\"Adaptive card examples\" href=\"https://adaptivecards.io/samples/\" target=\"_blank\" rel=\"noopener\">Adaptive card</a> allows for parsing the message string as an adaptive card serialized to string format vs a bot framework format. Please <a title=\"more info on adaptive cards\" href=\"mailto:help@faqbot.nz\" target=\"_blank\" rel=\"noopener\">contact us</a> for details on what is supported.</p>\n<p>The serialized adaptive card is placed in the \"message\" field.</p>\n<p>Text is used for all other message types, i.e. simple text, markdown, message with buttons</p>\n<p>&nbsp;</p>\n<h3 id=\"mcetoc_1hbs1mdaml3k\">Example API response: simple message</h3>\n<p><img src=\"https://portal.faqbot.nz/api/images/079d861d72cc4aca80c65588df217079/files/simple%20api%20message.png\" width=\"410\" height=\"93\"></p>\n<details class=\"mce-accordion\">\n<summary><em><strong>View code for this example</strong></em></summary>\n<pre class=\"language-javascript\"><code>{\n\"status\": \"success\",\n\"statusCode\": 1,\n\"messageType\": \"Text\",\n\"message\": \"The weather today is nice and sunny\"\n}</code></pre>\n</details>\n<h3>&nbsp;</h3>\n<h3 id=\"mcetoc_1hbs1mdaml3l\">Example API response: markdown format</h3>\n<p><img src=\"https://portal.faqbot.nz/api/images/079d861d72cc4aca80c65588df217079/files/api%20response%20markdown.png\" width=\"488\" height=\"105\"></p>\n<details class=\"mce-accordion\">\n<summary><em><strong>View code for this example</strong></em></summary>\n<pre class=\"language-javascript\"><code>{\n\"status\": \"success\",\n\"statusCode\": 1,\n\"messageType\": \"Text\",\n\"message\": \"The weather today is **nice** and sunny click [here](https://weather.com/city/auckland) for more information\"\n}</code></pre>\n</details>\n<h2>&nbsp;</h2>\n<h2 id=\"mcetoc_1hbs1mdaml3m\">Attachment format</h2>\n<p>Helpfruit supports different types of button attachments in API responses. This allows users to quickly and easily take a follow-up action -&nbsp;eg get more information or go to a specific web page.</p>\n<p>All button attachments include the following field:&nbsp;<code>attachmentType:&nbsp;0</code></p>\n<h3 id=\"mcetoc_1hbs1mdaml3n\">Types of button</h3>\n<p>Response button</p>\n<ul>\n<li>displayText - Text shown on the button, eg&nbsp;\"Click here\"</li>\n<li>replyText - Text that will come back to the bot when the user clicks the button. Optional. Useful when you want short, simple button text, but more detailed response, ie&nbsp;to trigger a specific answer via a long question</li>\n</ul>\n<p>Custom action button</p>\n<p>These buttons can also set context via the action field. This can be used by the API to set IDs, authentication information, or other information that is needed in subsequent forms.</p>\n<ul>\n<li>displayText - Text shown on the button</li>\n<li>action - Custom action to perform, see Custom action format info below</li>\n</ul>\n<p>Open url button</p>\n<ul>\n<li>displayText - Text shown on the button</li>\n<li>url - Url to open when the button is clicked</li>\n</ul>\n<h3>&nbsp;</h3>\n<h3 id=\"mcetoc_1hbs1mdaml3o\">Example - API response with button attachments</h3>\n<p><img src=\"https://portal.faqbot.nz/api/images/079d861d72cc4aca80c65588df217079/files/button%20examples.png\"></p>\n<details class=\"mce-accordion\">\n<summary><em><strong>View code for this example</strong></em></summary>\n<pre class=\"language-javascript\"><code>{\n\"status\": \"success\",\n\"statusCode\": 1,\n\"messageType\": \"Text\",\n\"message\": \"The weather today is **nice** and sunny click [here](https://weather.com/city/auckland) for more information\",\n\"attachments\": [\n  {\n    \"type\": 0,\n    \"displayText\": \"Weather radar\",\n    \"replyText\": \"Show me the weather radar\"\n  },\n  {\n    \"type\": 0,\n    \"displayText\": \"Weather warnings\",\n  },\n  {\n    \"type\": 0,\n    \"displayText\": \"Open home page\",\n    \"url\": \"https://weather.com\"\n  }\n]\n}</code></pre>\n</details>\n<p>&nbsp;</p>\n<h2 id=\"mcetoc_1hbs1mdaml3p\">Custom action format</h2>\n<p>Within the button attachment, custom actions can be specified, to allow selected actions to be triggered in the bot via the button shown to the user. &nbsp;</p>\n<p>See <a href=\"https://help.faqbot.ai/article/custom-actions-via-buttons-in-the-bot\" target=\"_blank\" rel=\"noopener\">custom actions documentation</a> for more details on available actions.</p>\n<h3 id=\"mcetoc_1hbs1mdaml3q\">Custom action formats</h3>\n<table style=\"border-collapse: collapse; width: 100%; border-width: 1px; margin-left: 0px; margin-right: auto;\" border=\"1\"><colgroup><col style=\"width: 16.690751%;\"><col style=\"width: 16.690751%;\"><col style=\"width: 7.369942%;\"><col style=\"width: 20.15896%;\"><col style=\"width: 15.679191%;\"><col style=\"width: 23.265896%;\"></colgroup>\n<tbody>\n<tr>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Field</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Type</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Required?</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Options</strong></td>\n<td style=\"height: 19.5938px; padding: 5px;\"><strong>Example</strong></td>\n<td style=\"padding: 5px;\"><strong>Notes</strong></td>\n</tr>\n<tr>\n<td style=\"padding: 5px;\">type</td>\n<td style=\"padding: 5px;\">Number</td>\n<td style=\"padding: 5px;\">Y</td>\n<td style=\"padding: 5px;\">\n<p>3 (Trigger Form)</p>\n<p>5 (Trigger Engagement By Event Name)</p>\n</td>\n<td style=\"text-align: left; vertical-align: top; padding: 5px;\">\n<p>3</p>\n<p>5</p>\n</td>\n<td style=\"padding: 5px;\">See value field format table</td>\n</tr>\n<tr>\n<td style=\"padding: 5px;\">value</td>\n<td style=\"padding: 5px;\">String</td>\n<td style=\"padding: 5px;\">Y</td>\n<td style=\"padding: 5px;\">See value field format table</td>\n<td style=\"padding: 5px;\">\n<p>calculator=c53fe92a-8362-463e-94eb-06f13fa3fc13</p>\n<p>newsletter-signup</p>\n</td>\n<td style=\"padding: 5px;\">See value field format table</td>\n</tr>\n<tr>\n<td style=\"padding: 5px;\">context</td>\n<td style=\"padding: 5px;\">Object</td>\n<td style=\"padding: 5px;\">N</td>\n<td style=\"padding: 5px;\">&nbsp;</td>\n<td style=\"padding: 5px;\">\n<p>{</p>\n<p>\"contextKey\": \"contextValue\",</p>\n<p>\"name\": \"Clementine\"</p>\n<p>}</p>\n</td>\n<td style=\"padding: 5px;\">\n<p>These fields need to first be added as <a title=\"bot context keys\" href=\"https://helpfruit.com/configuration/bot-context\" target=\"_blank\" rel=\"noopener\">bot context keys</a>.</p>\n<p>Setting context allows for storing information that can be used by subsequent forms without having to ask the user, eg store a Sales Order number.</p>\n<p><span dir=\"ltr\">Context is also displayed in live chat and conversation history - so you can attach identifiers to buttons and have that context visible in the portal (eg a marketing campaign ID).</span></p>\n<p>The context is only set when the button is clicked.<br><br><a href=\"https://help.helpfruit.com/article/using-context\" target=\"_blank\" rel=\"noopener\">Learn more about setting up context</a></p>\n</td>\n</tr>\n</tbody>\n</table>\n<p>&nbsp;</p>\n<h3 id=\"mcetoc_1hbs1mdaml3r\">Custom actions: value field format by type</h3>\n<p>The format of the value field that must be used for each custom action type is shown below.</p>\n<table style=\"border-collapse: collapse; width: 61.157715%; height: 75.294113px; border-width: 1px; margin-left: 0px; margin-right: auto;\" border=\"1\"><colgroup><col style=\"width: 5.204082%;\"><col style=\"width: 21.020408%;\"><col style=\"width: 21.530612%;\"><col style=\"width: 52.244898%;\"></colgroup>\n<tbody>\n<tr style=\"height: 18.823528px;\">\n<td style=\"height: 18.823528px; padding: 5px;\"><strong>Type</strong></td>\n<td style=\"height: 18.823528px; padding: 5px;\"><strong>Action</strong></td>\n<td style=\"height: 18.823528px; padding: 5px;\"><strong>Value field format</strong></td>\n<td style=\"height: 18.823528px; padding: 5px;\"><strong>Example of value field</strong></td>\n</tr>\n<tr style=\"height: 18.823528px;\">\n<td style=\"height: 18.823528px; padding: 5px;\">3</td>\n<td style=\"height: 18.823528px; padding: 5px;\">Trigger a form</td>\n<td style=\"height: 18.823528px; padding: 5px;\">calculator=FORM_ID_GUID</td>\n<td style=\"height: 18.823528px; padding: 5px;\">calculator=c53fe92a-8362-463e-94eb-06f13fa3fc13</td>\n</tr>\n<tr style=\"height: 37.647057px;\">\n<td style=\"height: 37.647057px; padding: 5px;\">5</td>\n<td style=\"height: 37.647057px; padding: 5px;\">Trigger an engagement by event name</td>\n<td style=\"height: 37.647057px; padding: 5px;\">EVENT_NAME</td>\n<td style=\"height: 37.647057px; padding: 5px;\">newsletter-signup</td>\n</tr>\n</tbody>\n</table>\n<p>&nbsp;</p>\n<h3 id=\"mcetoc_1hbs1mdaml3s\">Example: API response with custom actions</h3>\n<p><strong><img src=\"https://portal.faqbot.nz/api/images/079d861d72cc4aca80c65588df217079/files/custom%20actions%20api%20attachment.png\" width=\"383\" height=\"171\"></strong></p>\n<details class=\"mce-accordion\">\n<summary><em><strong>View code for this example</strong></em></summary>\n<pre class=\"language-javascript\"><code>{\n\"status\": \"success\",\n\"statusCode\": 1,\n\"messageType\": \"Text\",\n\"message\": \"The weather today is **nice** and sunny click [here](https://weather.com/city/auckland) for more information\",\n\"attachments\": [\n      {\n        \"type\": 0, // Button\n        \"displayText\": \"Signup to newsletter\", \n        \"action\":\n        {\n          \"type\": 5, // Trigger page event\n          \"value\": \"newsletter-signup\"\n        }\n      },\n      {\n        \"type\": 0, // Button\n        \"displayText\": \"Contact us\", \n        \"action\":\n        {\n          \"type\": 3, // Activate form\n          \"value\": \"calculator=c53fe92a-8362-463e-94eb-06f13fa3fc13\"\n        }\n      }\n    ]\n}</code></pre>\n</details>\n<p>&nbsp;</p>\n<h2 id=\"mcetoc_1hbs1mdaml3t\">Example API host (in NodeJS) to test message responses</h2>\n<p>A simple node js web server to handle API requests and respond back with some examples.</p>\n<ul>\n<li>Use this to quickly test how the bot displays various response types.&nbsp;</li>\n<li>Use with a port-forwarder, i.e. NGROK to expose the API to the bot.</li>\n</ul>\n<pre class=\"language-javascript\"><code>const http = require(\"http\");\nconst url = require(\"url\");\n\nconst requestListener = function (req, res) {\n  const parsed = url.parse(req.url, true); // Parse URL with query string\n\n  const examples = {\n    1: {\n      status: \"success\",\n      statusCode: 1,\n      messageType: \"Text\",\n      message: \"The weather today is nice and sunny\",\n    },\n    2: {\n      status: \"success\",\n      statusCode: 1,\n      messageType: \"Text\",\n      message: \"The weather today is **nice** and sunny click [here](https://weather.com/city/auckland) for more information\",\n    },\n    3: {\n      status: \"success\",\n      statusCode: 1,\n      messageType: \"Text\",\n      message: \"The weather today is **nice** and sunny click [here](https://weather.com/city/auckland) for more information\",\n      attachments: [\n        {\n          type: 0,\n          displayText: \"Weather radar\",\n          replyText: \"Show me the weather radar\",\n        },\n        {\n          type: 0,\n          displayText: \"Weather warnings\",\n        },\n        {\n          type: 0,\n          displayText: \"Open home page\",\n          url: \"https://weather.com\",\n        },\n      ],\n    },\n    4: {\n      status: \"success\",\n      statusCode: 1,\n      messageType: \"Text\",\n      message: `✅ We have found your order number: **${parsed.query.orderId}**`,\n      attachments: [\n        {\n          type: 0, // Button\n          displayText: \"\uD83D\uDCDD - View order\",\n          url: `https://store.com?order=${parsed.query.orderId}&amp;email=${parsed.query.email}`,\n        },\n        {\n          type: 0, // Button\n          displayText: \"Contact us\",\n          action: {\n            type: 3, // Activate form\n            value: \"calculator=c11fe92a-4444-555e-11aa-06f13fa3f123,\n            context: {\n                 \"userName\": \"Clementine Citrus\",\n                  \"contactMethod\": \"apiResponse\"\n             },\n          },\n        },\n      ],\n    },\n  };\n\n  if (parsed.query.ex &amp;&amp; examples[parsed.query.ex]) {\n    res.setHeader(\"Content-Type\", \"application/json\");\n    res.writeHead(200);\n    res.end(JSON.stringify(examples[parsed.query.ex]));\n  } else {\n    res.writeHead(200);\n    res.end(\"&lt;html&gt;&lt;body&lt;div&gt;test&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;\");\n  }\n};\n\nconst host = \"localhost\";\nconst port = 9001;\n\nconst server = http.createServer(requestListener);\nserver.listen(port, host, () =&gt; {\n  console.log(`Server is running on http://${host}:${port}`);\n});\n</code></pre>","description":"","isMarkdown":false,"publishedOnUtc":"2024-09-04T21:59:17.0801798","modifiedOnUtc":"2023-10-03T00:40:21.296286"}