node.js - 如何在 twilio 函数运行时环境中声明自定义函数?

标签 node.js asynchronous twilio twilio-functions

我有一个函数和一些辅助函数,它们应该访问 Slack 消息历史记录并返回与特定数字字符串匹配的第一条消息。

我已在计算机的 Node.js 环境中广泛测试了代码,以验证其是否有效且正确。但是,我无法让它在 Twilio Function 的运行时环境中工作;我不断收到错误“函数声明中缺少名称。”,并且该函数无法正确链接或根本无法像函数一样工作。

我做错了什么?

exports.handler = function(context, event, callback) {

    if(event.event){
         if(event.event.channel == context.CHANNEL_ID){
            if(event.event.subtype){
                console.log("bot posting");
                if(event.event.thread_ts){
                    console.log("bot posting in thread");
                    console.log(event);
                }
                else{
                    console.log("bot posting in channel");
                    console.log(event);
                }
            }
            else{
                console.log("staff posting");
                if(event.event.thread_ts){
                    console.log("staff posting in thread");
                    console.log(event);
                }
                else{
                    console.log("staff posting in channel");
                    console.log(event);
                }
            }
         }
    }
    else{
        console.log("incoming sms message");
        console.log(typeof event.From);
    }


    returnTS("6049248010").then(function (ts) {
        console.log(ts);
    });

    callback(null);
};


var getSlackHistory = async function (ts) {

    try {
        var response;

        if (typeof ts === 'undefined') {
            response = await got('https://slack.com/api/channels.history?'
                + 'token=' + context.TWILIO_TOKEN
                + '&channel=' + context.TWILIO_CHANNEL_ID);
        }
        else {
            response = await got('https://slack.com/api/channels.history?'
                + 'token=' + context.TWILIO_TOKEN
                + '&channel=' + context.TWILIO_CHANNEL_ID
                + '&latest=' + ts);
        }

        return await JSON.parse(response.body);
    }
    catch (error) { console.log("failure"); }

}

var getAllSlackHistory = async function () {
    var message_history = [];
    try {
        var response = await getSlackHistory();
        //  console.log(response.messages[response.messages.length - 1].ts);
        for (var i = 0; i < response.messages.length; i++) {
            message_history.push(response.messages[i]);
        }
        while (response.has_more) {
            response = await getSlackHistory(response.messages[response.messages.length - 1].ts);
            for (var i = 0; i < response.messages.length; i++) {
                message_history.push(response.messages[i]);
            }
        }
        return await message_history;
    }
    catch (error) {
        console.log(error);
    }

}

var returnTS = async function (numberString) {
    try {
        var message_history = await getAllSlackHistory();
        //console.log(message_history.length);
        // console.log(numberString);
        for (var i = 0; i < message_history.length; i++) {
            //  console.log(message_history[i].text);
            // console.log(message_history[i].text.includes(numberString));
            if (message_history[i].text.includes(numberString))
                //  console.log(message_history[i].ts);
                return message_history[i].ts;
        }
    }
    catch (error) {
        console.log(error);
    }
}

最佳答案

这里是 Twilio 开发者布道者。

正如您在评论中所说,这与异步操作和 Twilio 函数的回调有关。

目前,您的函数的最后几行是:

  returnTS("6049248010").then(function (ts) {
      console.log(ts);
  });

  callback(null);
};

returnTS 是一个异步函数,在本例中返回一个 Promise。一旦您调用callback函数(或5秒后),Twilio Functions将停止所有操作,因此在这种情况下,您应该只在异步函数完成后调用callback。如果您将调用移至 Promise 回调中的 callback,那么事情应该按您的预期进行。请尝试以下操作:

  returnTS("6049248010").then(function (ts) {
      console.log(ts);
      callback(null);
  });
};

关于node.js - 如何在 twilio 函数运行时环境中声明自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56193164/

相关文章:

javascript - Parse.com CloudCode 是否处理 response.success() 之后的代码?

twilio - Twilio API_KEY_SECRET 是否与控制台中的 Twilio 身份验证 token 相同?

php - 我们可以使用 PHP 使用 Twilio API 检索调用者位置信息吗?

java - twilio:无法获得转录

Node.js : how to pass JS variable's value from script to terminal?

javascript - 如何使用 Android 作为客户端访问 express.js/passport.js API?

ios - 使用异步响应协议(protocol)

node.js - 在自托管 VSO 代理上运行 NPM

node.js - 在 Express js/中从域重定向到子域时如何保持 session 事件

c# - 适合初学者的异步/等待