{
  "createdAt": "2025-02-13T05:24:49.924Z",
  "updatedAt": "2025-04-07T11:28:46.000Z",
  "id": "UZTEpQ4MzFUgnieh",
  "name": "webhook-qw",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nlet output_info = {};\nlet title_info = {};\nlet summary_info = {};\ntitle = JSON.parse($input.item.json.body.info).title;\nsummary = JSON.parse($input.item.json.body.info).summary;\n// content = JSON.parse($input.item.json.body.info).content;\ntitle_info['title'] = title;\nsummary_info['summary'] = summary;\n//text_info['content'] = content;\noutput_info['title_text'] = JSON.stringify(title_info);\noutput_info['summary_text'] = JSON.stringify(summary_info);\nreturn output_info;"
      },
      "id": "4d9e2f42-839f-4ce0-b604-a333cd9f6f96",
      "name": "get_info",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\n//answer = \"```json{\\\"test1\\\":\\\"test\\\"}```\";\nfunction findAllIndexes(str, charToFind) {\n  const regex = new RegExp(charToFind, 'g');\n  let indexes = [];\n  let result;\n  while ((result = regex.exec(str)) !== null) {\n    indexes.push(result.index);\n  }\n  return indexes;\n}\nfunction isValidJSON(str) {\n    try {\n        JSON.parse(str);\n        return true;\n    } catch (e) {\n        return false;\n    }\n}\nfunction autoCompleteJSON(str) {\n    if (isValidJSON(str)) {\n        return str;\n    }\n    let missing = '';\n    if (!str.endsWith('\"}')) {\n        missing += '\"}'\n    }\n    str = str+missing;\n    return str;\n};\nfunction isEmptyString(value) {\n  // 方法1: 直接检查是否为空字符串或null\n  if (!value || value.trim() == '' || value.trim() == 'null' || value.trim() == 'None') {\n    return true;\n  }\n  return false;\n};\nresult_data = $input.all();\ncontent_all = {};\nanswer_data = {};\nanswer_data_list = {};\nfor(let item_index =0; item_index < result_data.length;item_index++){\n  // item_data = result_data[item_index].json.data.outputs.result;\n  // item_data = result_data[item_index].json.data;\n  item_data = result_data[item_index].json;\n  if('answer' in item_data){\n    answer_data_str = item_data['answer'];\n    answer_json_data = get_answer_data(answer_data_str);\n    title = answer_json_data.title;\n    summary = answer_json_data.summary;\n    if(!isEmptyString(title)){\n      answer_data_list['title'] = title;\n    }else if(!isEmptyString(summary)){\n      answer_data_list['summary'] = summary;\n    }\n  }\n};\nfunction get_answer_data(answer){\n  answer = answer.replaceAll(/\\n/g, '').replaceAll(/%s| /g, '');\n  indexes = findAllIndexes(answer,\"```\");\n  answer_str = '';\n  if(indexes.length>0){\n    answer_str = answer.slice(indexes[0]+7, indexes[1]);\n  }else{\n    answer_str = answer;\n  }\n  answer_str = answer_str.replace(/\\s+/g, '');\n  answer_data_json = JSON.parse(autoCompleteJSON(answer_str));\n  return answer_data_json;\n};\nanswer_data[\"answer\"] = answer_data_list;\nreturn answer_data;"
      },
      "id": "1cfa94cc-ebb4-4ccb-9d8a-14f47cae8292",
      "name": "get_answer",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1500,
        360
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "START TRANSACTION;\n\nINSERT INTO news_source (topic,summary,source_url,author,publish_time,news_uid,status,language,news_web,news_type) VALUES {{ $json.news_data }};\n\nSET @last_insert_id = LAST_INSERT_ID();\n\nINSERT INTO news_url (url,thumbnail_url, type,news_id)\nVALUES {{ $json.urls_data }};\n\nCOMMIT;",
        "options": {}
      },
      "id": "55521739-d69f-450f-9ec9-c7cfbfdec1f4",
      "name": "insert_city_news",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.4,
      "position": [
        1900,
        360
      ],
      "credentials": {
        "mySql": {
          "id": "YpVIiGwzmmkf7vR7",
          "name": "SL-DB-PRD"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nresult_data = $input.all();\ncontent_all = {};\nfor(let item_index =0; item_index < result_data.length;item_index++){\n  item_data = result_data[item_index].json;\n  if('answer' in item_data){\n    content_all['answer'] = item_data['answer'];\n  }\n};\nfunction containsChinese(str) {\n    const chineseRegex = /[\\u4e00-\\u9fa5]/;\n    return chineseRegex.test(str);\n};\n// console.log(content_all);\ninfo_data_list = JSON.parse($('Webhook').all()[0].json.body.info);\n// console.log(info_data_list);\nanswer_data = content_all.answer;\nfy_title = answer_data.title;\nfy_summary = answer_data.summary;\nif(containsChinese(fy_title)){\n  info_data_list['title'] = fy_title;\n}else{\n  info_data_list['title'] = info_data_list.title;\n}\nif(containsChinese(fy_summary)){\n  info_data_list['summary'] = fy_summary;\n}else{\n  info_data_list['summary'] = info_data_list.summary;\n}\n//info_data_list['content'] = answer_data.content;\noutput_all = [];\noutput_all_data = {};\noutput_news_data_str = \"\";\noutput_news_url_data_str = \"\";\nnews_item_data = \"\";\nnews_item_data = news_item_data+\"(\";\nnews_item_data += '\"'+info_data_list.title+'\"';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.summary+'\"';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.url+'\"';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.author+'\"';\nnews_item_data += ',';\nnews_item_data += 'STR_TO_DATE(\\''+info_data_list.publish_time+'\\',\\'%Y-%m-%d %H:%i:%s\\')';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.id+'\"';\nnews_item_data += ',';\nnews_item_data += '\"4\"';\nnews_item_data += ',';\nnews_item_data += '\"zh_cn\"';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.news_web+'\"';\nnews_item_data += ',';\nnews_item_data += '\"'+info_data_list.type+'\"';\nnews_item_data += ')';\nimages_url = info_data_list.images_url;\nfunction isEmptyString(value) {\n  // 方法1: 直接检查是否为空字符串或null\n  if (!value || value.trim() == '' || value.trim() == 'null' || value.trim() == 'None') {\n    return true;\n  }\n  return false;\n};\nfor (let i = 0; i < images_url.length; i++) {\n  image_url_data = images_url[i];\n  original_url = image_url_data.original_url;\n  thumbnail_url = image_url_data.thumbnail_url;\n  if(isEmptyString(original_url) && isEmptyString(thumbnail_url)){\n    continue;\n  }else{\n    image_item_data = \"(\";\n    image_item_data += \"'\"+original_url+\"'\";\n    image_item_data += ',';\n    if(isEmptyString(thumbnail_url)){\n      image_item_data += null;\n    }else{\n      image_item_data += \"'\"+thumbnail_url+\"'\";\n    }\n    image_item_data += ',';\n    image_item_data += '0';\n    image_item_data += ',';\n    image_item_data += '@last_insert_id';\n    image_item_data += '),';\n    output_news_url_data_str += image_item_data;\n  }\n};\nif(output_news_url_data_str.length == 0){\n  output_news_url_data_str = \"('','','',''),\"\n}\noutput_all_data['urls_data'] = output_news_url_data_str.slice(0,output_news_url_data_str.length-1);\noutput_all_data['news_data'] = news_item_data;\noutput_all.push(output_all_data);\nreturn output_all;"
      },
      "id": "13d38f15-173b-4bb7-8355-20d8c290b87b",
      "name": "get_city_news_sql",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1700,
        360
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://172.31.0.160/v1/completion-messages",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "jsonHeaders": "{\n        \"Authorization\": \"Bearer app-fxQiMsg03GrJCtwNfLRtnD4R\",\n        \"Content-Type\": \"application/json\"\n}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n        \"inputs\": {\"record_json\":\"{{$json.title_text.replaceAll(/\"/g, '\\\\\"').replaceAll(/\\\\n/g, '')}}\",\"language\":\"中文\"},\n        \"response_mode\": \"blocking\",\n        \"user\": \"sl_backend_theguardian\"\n}",
        "options": {}
      },
      "id": "c7dd1a91-24f6-407a-b4d0-825652a36e74",
      "name": "title_dify",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        220
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://172.31.0.160/v1/completion-messages",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "jsonHeaders": "{\n        \"Authorization\": \"Bearer app-fxQiMsg03GrJCtwNfLRtnD4R\",\n        \"Content-Type\": \"application/json\"\n}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n        \"inputs\": {\"record_json\":\"{{$json.summary_text.replaceAll(/\"/g, '\\\\\"').replaceAll(/\\\\n/g, '')}}\",\"language\":\"中文\"},\n        \"response_mode\": \"blocking\",\n        \"user\": \"sl_backend_theguardian\"\n}",
        "options": {}
      },
      "id": "1cf1a1db-b44f-4496-87cf-6a6560436c51",
      "name": "summary_dify",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        520
      ]
    },
    {
      "parameters": {},
      "id": "0ac6abcc-43dc-45eb-8c51-5780c53b0503",
      "name": "merge_label",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        1300,
        360
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "qw/translate/ch",
        "authentication": "headerAuth",
        "options": {
          "allowedOrigins": "*",
          "rawBody": false
        }
      },
      "id": "634935f6-3a7e-41ac-9983-a8c46de0d808",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        520,
        400
      ],
      "webhookId": "e495a29c-8f43-4175-bdb8-277e03b60b07",
      "credentials": {
        "httpHeaderAuth": {
          "id": "U87Wvnvu9oQ5Zapo",
          "name": "Header Auth account"
        }
      }
    }
  ],
  "connections": {
    "get_info": {
      "main": [
        [
          {
            "node": "summary_dify",
            "type": "main",
            "index": 0
          },
          {
            "node": "title_dify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_answer": {
      "main": [
        [
          {
            "node": "get_city_news_sql",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_city_news_sql": {
      "main": [
        [
          {
            "node": "insert_city_news",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "title_dify": {
      "main": [
        [
          {
            "node": "merge_label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "summary_dify": {
      "main": [
        [
          {
            "node": "merge_label",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "merge_label": {
      "main": [
        [
          {
            "node": "get_answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "get_info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "pinData": {},
  "versionId": "90b26e2e-dce1-4f59-b340-b4f747e5ad4a",
  "triggerCount": 1,
  "shared": [
    {
      "createdAt": "2025-02-13T05:24:49.932Z",
      "updatedAt": "2025-02-13T05:24:49.932Z",
      "role": "workflow:owner",
      "workflowId": "UZTEpQ4MzFUgnieh",
      "projectId": "C7zKoCDdmGgzUqSW",
      "project": {
        "createdAt": "2024-08-23T06:31:21.275Z",
        "updatedAt": "2024-08-23T06:41:43.608Z",
        "id": "C7zKoCDdmGgzUqSW",
        "name": "周 卫 <zhouwei@ssish.com>",
        "type": "personal",
        "projectRelations": [
          {
            "createdAt": "2024-08-23T06:31:21.275Z",
            "updatedAt": "2024-08-23T06:31:21.275Z",
            "role": "project:personalOwner",
            "userId": "3f0021b4-d0a7-4db6-af21-5ef829c47882",
            "projectId": "C7zKoCDdmGgzUqSW",
            "user": {
              "createdAt": "2024-08-23T06:31:16.119Z",
              "updatedAt": "2025-03-19T06:19:26.514Z",
              "id": "3f0021b4-d0a7-4db6-af21-5ef829c47882",
              "email": "zhouwei@ssish.com",
              "firstName": "周",
              "lastName": "卫",
              "personalizationAnswers": {
                "companyType": "education",
                "role": "business-owner",
                "automationBeneficiary": "my-team",
                "companySize": "20-99",
                "reportedSource": "friend",
                "version": "v4",
                "personalization_survey_submitted_at": "2024-08-23T06:57:52.856Z",
                "personalization_survey_n8n_version": "1.55.3"
              },
              "settings": {
                "userActivated": true,
                "firstSuccessfulWorkflowId": "s6zsznwVs6N4L2Hx",
                "userActivatedAt": 1725957335278,
                "npsSurvey": {
                  "responded": true,
                  "lastShownAt": 1742365155724
                },
                "isOnboarded": true
              },
              "role": "global:owner",
              "disabled": false,
              "mfaEnabled": false,
              "isPending": false,
              "isOwner": true
            }
          }
        ]
      }
    }
  ],
  "tags": []
}