Как настроить несколько веб-хуков в Watson Assistant - PullRequest
0 голосов
/ 04 мая 2020

Я пытался выполнить несколько действий в облачных функциях IBM. Мне нужен Watson Assistant, чтобы вызывать разные при попадании на определенные узлы.

Мой вопрос, есть только возможность добавить один webhook в опции Watson assistant. Я понимаю, что мне придется кодировать действие, которое обнаруживает отправленные параметры и вызывает соответствующее действие. Я пытался использовать Ax ios NPM, как вы можете видеть ниже, но он всегда терпит неудачу, когда Watson Assistant пытается запустить его. Это дает мне ошибки авторизации. Мое текущее пространство имен IBM Cloud Function основано на CF.

Вот код, который я пытаюсь использовать для вызова действия "электронные письма":

const axios = require("axios");
// Change to your "BASE_URL". Type in the "Web Action" url without the name of it at the end. (must NOT contains the end '/')
// All "Actions" must be within the same namespace.
const BASE_URL = "https://us-south.functions.cloud.ibm.com/api/v1/namespaces"
const POST = "post";
const GET = "get";
const ANY = "any";
const ALL = "all";

/* List all your API methods
    1. method - API Name (This is the "Actions" name at the end of the Web Action URL or just the name)
    2. attr - Attributes that should be available in the params object
    3. rule - Currently supports 2 rules;
                a) any - params is valid if "any" of the attributes are present
                b) all - params is valid only if all attributes are present
    4. httpmethod -Supports "POST" and "GET"
    5. contains - Use for validating GET URL parameters
*/
const API_METHODS = [{
        method: "Emails", // Change to your API method "Please put the function name located at the end of the url without "/" example "Email"
        attr: ["client_email", "department_email"],
        rule: ANY,
        httpmethod: POST,
        contains: null
    },
    {
        method: "testapi", // If Watson needs to "GET" information to a user or athenticate a user
        attr: [],
        rule: ALL,
        httpmethod: GET,
        contains: "?ID="
    },
]

// Returns true if the "params" is valid for the given API method
function isValidParam(method, params = {}) {
    var attributes = [];
    attributes = method.attr;
    var isAny = method.rule === ANY;
    for (let index = 0; index < attributes.length; index++) {
        const attr = attributes[index];
        if (isAny) {
            if (Object.hasOwnProperty.call(params, attr)) {
                return true;
            }
        } else {
            if (!Object.hasOwnProperty.call(params, attr)) {
                return false;
            }
        }
    }

    if (attributes.length === 0 && method.contains) {
        return (JSON.stringify(params).indexOf(method.contains) > -1)
    }

    // if the code reaches this position, inverse of "isAny" should return the actual validation status.
    return !isAny;
}

async function main(params) {
    var result = [];

    // Stop on first failure
    // We iterate through all API methods. Because there can be more than one matching API for the given param type
    for (let index = 0; index < API_METHODS.length; index++) {
        const apiMethod = API_METHODS[index];
        const url = BASE_URL + '/' + apiMethod.method;

        if (isValidParam(apiMethod, params)) {
            let response = apiMethod.httpmethod === POST ?
                await axios.post(url, params) :
                await axios.get(url + params); // Expects the parameter to be a string in this case like '?id=345343'

            console.log(response);

            result.push({
                sent: true,
                url: url,
                request: params,
            });
        }
    }

    return {
        sent: true,
        details: result
    };
}
// THe part of the code that needs to be copied to call other functions within the namespace.


/* const API_METHODS = [{
 method: "Emails", // Change to your API method "Please put the function name located at the end of the url without "/" example "Email"
 *   attr: ["bamboo_email", "latitude_email", "latest_r_email", "department_email"],
    rule: ANY,
    httpmethod: POST,
    contains: null
},
{
    method: "testapi", // If Watson needs to "GET" information to a user or athenticate a user
    attr: [],
    rule: ALL,
    httpmethod: GET,
    contains: "?ID="
},
]  */
...