如何处理 webhook 结果

How to process webhook result

我正在尝试使用 ibm 云功能将 webhook 连接到 nhs 网站,然后将结果输出到我的 watson assistant 聊天机器人

function main(params) {
    const options = {
        uri: "https://api.nhs.uk/conditions/coronavirus-covid-19/"+encodeURIComponent(params.query_topic)+"/?url=https://api.eu-gb.assistant.watson.cloud.ibm.com/instances/...",
        json: true
    }
    return rp(options)
    .then(res => {
        return {text : res} 
    }) 
   }

webhook 成功并将参数传递给 return 来自正确页面的信息

我希望结果只显示文本主体而不是所有标签,所以从“冠状病毒 (COVID-19) 的主要症状是:...”开始

这是完整输出(无法添加图片)

"{"@context": "http://schema.org", "@type": "WebPage", "name": "Symptoms of coronavirus", "copyrightHolder": {"name": "Crown Copyright", "@type": "Organization"}, "license": "https://developer.api.nhs.uk/terms", "author": {"url": "https://www.nhs.uk", "logo": "https://www.nhs.uk/nhscwebservices/documents/logo1.jpg", "email": "nhswebsite.servicedesk@nhs.net", "@type": "Organization", "name": "NHS website"}, "about": {"@type": "WebPage", "name": "Symptoms of coronavirus"}, "description": "Find out about the main symptoms of coronavirus (COVID-19) and where to get medical advice if you think you have them.", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/symptoms/", "lastReviewed": ["2021-03-04T11:27:00+00:00", "2021-03-18T11:27:00+00:00"], "breadcrumb": {"@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 0, "item": {"@id": "https://www.nhs.uk/conditions/", "name": "Health A to Z", "genre": "condition"}}, {"@type": "ListItem", "position": 1, "item": {"@id": "https://www.nhs.uk/conditions/coronavirus-covid-19/", "name": "Coronavirus (COVID-19)", "genre": "hub"}}, {"@type": "ListItem", "position": 2, "item": {"@id": "https://www.nhs.uk/conditions/coronavirus-covid-19/symptoms/", "name": "Symptoms of coronavirus", "genre": "guide"}}]}, "genre": "guide", "hasPart": [{"@type": "WebPageElement", "name": "symptoms", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/symptoms/#symptoms", "description": "The main symptoms of coronavirus (COVID-19) are a high temperature, a new, continuous cough and a loss or change to your sense of smell or taste.", "text": "<p>The main symptoms of coronavirus (COVID-19) are:</p><ul><li>a high temperature \u2013 this means you feel hot to touch on your chest or back (you do not need to measure your temperature)</li><li>a new, continuous cough \u2013 this means coughing a lot for more than an hour, or 3 or more coughing episodes in 24 hours (if you usually have a cough, it may be worse than usual)</li><li>a loss or change to your sense of smell or taste \u2013 this means you've noticed you cannot smell or taste anything, or things smell or taste different to normal</li></ul><p>If you have any of these symptoms, get a test to check if you have coronavirus and stay at home until you get your result.</p>", "title": ""}], "relatedLink": [{"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/", "name": "Coronavirus (COVID-19)", "linkRelationship": "Navigation", "position": 0}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/symptoms/", "name": "Symptoms of coronavirus", "linkRelationship": "Navigation", "position": 1}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/self-isolation-and-treatment/", "name": "Self-isolation and treating coronavirus symptoms", "linkRelationship": "Navigation", "position": 2}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/testing-and-tracing/", "name": "Testing and tracing for coronavirus", "linkRelationship": "Navigation", "position": 3}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/people-at-higher-risk/", "name": "People at higher risk from coronavirus", "linkRelationship": "Navigation", "position": 4}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/social-distancing/", "name": "Social distancing and changes to everyday life", "linkRelationship": "Navigation", "position": 5}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/research/", "name": "Take part in coronavirus research", "linkRelationship": "Navigation", "position": 6}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/coronavirus-vaccination/", "name": "Coronavirus vaccination", "linkRelationship": "Navigation", "position": 7}, {"@type": "LinkRole", "url": "https://www.nhs.uk/conditions/coronavirus-covid-19/long-term-effects-of-coronavirus-long-covid/", "name": "Long-term effects of coronavirus (long COVID)", "linkRelationship": "Navigation", "position": 8}], "mainEntityOfPage": [{"identifier": "0", "name": "section heading", "position": 0, "@type": "WebPageElement", "headline": "", "text": "", "mainEntityOfPage": [{"position": 0, "identifier": "1", "text": "<p>If you have any of the main symptoms of coronavirus (COVID-19), get a test as soon as possible. Stay at home until you get the result.</p><h2><b>Main symptoms</b></h2><p>The main symptoms of coronavirus are:</p><ul><li><b>a high temperature</b> \u2013 this means you feel hot to touch on your chest or back (you do not need to measure your temperature)</li><li><b>a new, continuous cough</b> \u2013 this means coughing a lot for more than an hour, or 3 or more coughing episodes in 24 hours (if you usually have a cough, it may be worse than usual)</li><li><b>a loss or change to your sense of smell or taste</b> \u2013 this means you&#x27;ve noticed you cannot smell or taste anything, or things smell or taste different to normal</li></ul><p>Most people with coronavirus have at least 1 of these symptoms.</p><h2>What to do if you have symptoms</h2><p>If you have any of the main symptoms of coronavirus:</p><ol><li>Get a test to check if you have coronavirus as soon as possible.</li><li>You and anyone you live with should stay at home and not have visitors until you get your test result \u2013 only leave your home to have a test.</li></ol><p>Anyone in your support bubble should also stay at home if you have been in close contact with them since your symptoms started or during the 48 hours before they started.</p>", "@type": "WebPageElement", "name": "markdown"}, {"position": 1, "@type": "WebPageElement", "name": "Find a service", "identifier": "2", "text": "<p><a href=\"https://www.gov.uk/get-coronavirus-test\">Get a test to check if you have coronavirus on GOV.UK</a></p>\n"}, {"position": 3, "@type": "WebPageElement", "name": "urgent", "identifier": "5", "text": "<h2>Use the NHS 111 online coronavirus service if:</h2><div class=\"block-richtext\"><ul><li>you&#x27;re worried about your symptoms</li><li>you&#x27;re not sure what to do</li></ul></div>\n<div class=\"block-action_link\"><div class=\"nhsuk-action-link\">\n  <a class=\"nhsuk-action-link__link\" href=\"https://111.nhs.uk/covid-19/\" >\n    <svg class=\"nhsuk-icon nhsuk-icon__arrow-right-circle\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" aria-hidden=\"true\">\n      <path d=\"M0 0h24v24H0z\" fill=\"none\"></path>\n      <path d=\"M12 2a10 10 0 0 0-9.95 9h11.64L9.74 7.05a1 1 0 0 1 1.41-1.41l5.66 5.65a1 1 0 0 1 0 1.42l-5.66 5.65a1 1 0 0 1-1.41 0 1 1 0 0 1 0-1.41L13.69 13H2.05A10 10 0 1 0 12 2z\"></path>\n    </svg>\n    <span class=\"nhsuk-action-link__text\">Use the NHS 111 online coronavirus service</span>\n  </a>\n</div>\n</div>\n<div class=\"block-richtext\"><p>Call 111 if you cannot get help online. Do not go to places like a GP surgery, hospital or pharmacy.</p></div>"}]}, {"identifier": "0", "name": "section heading", "position": 1, "@type": "WebPageElement", "headline": "", "text": "", "mainEntityOfPage": [{"position": 0, "@type": "WebPageElement", "name": "Callout", "identifier": "4", "text": "<h2>Babies and children</h2><p>Call 111 if you&#x27;re worried about a baby or child under 5.</p><p>If your child seems very unwell, is getting worse or you think there&#x27;s something seriously wrong, call 999.</p><p>Do not delay getting help if you&#x27;re worried. Trust your instincts.</p><p>Get more advice about <a href=\"/conditions/coronavirus-covid-19/symptoms/coronavirus-in-children/\">coronavirus in children</a>.</p>"}]}], "dateModified": "2021-03-04T20:52:05+00:00"}"

我已经阅读了 IBM Watson 文档、NHS API 文档并浏览了 Stack Overflow,但找不到任何可以帮助我的东西。

您现在如何显示响应?只要 $webhook_response_1?

如果是这样,那么我认为 this documentation 会有所帮助。

大约一半的地方有一个 table,其中有两列:条件和响应。 See Screenshot

它说,如果你的条件是 $webhook_response_1,那么你的回应将是这样的:你的西班牙语单词:.

显然,将静态文本更改为您要查找的内容,并更改属性以匹配您自己的 JSON 返回的内容。

编辑:

根据您的评论,您正尝试使用以下方式显示数据:$webhook_result_1.text.mainEntityOfPage[9].text

但是,根据您 json 的格式,您实际上需要使用 $webhook_result_1.mainEntityOfPage[0].mainEntityOfPage[0].text

如上文所述,目前您向 nhs 网站执行请求,收到作为 json 有效负载的响应,然后 return 将完整响应返回到 Watson Assistant 网络钩。如果您只需要 returned 有效负载的子元素,则需要通过云函数提取该元素,然后 return 此子元素。或者您可以在 Watson Assistant 中仅在对话框中“显示”子元素,即如果您 return 对变量 $webhook_result_1 的响应,那么如上所示您仅在节点 $[= 中使用12=](json 元素从上面看不清楚,所以你可能需要调整才能正确)。在您的云函数中操作 returned json 比在助手中更容易。