需要在Slack App Node.js中的异步函数中获取REST API

问题描述 投票:0回答:1

我正在尝试为Slack创建一个可在调用命令时调用外部api的应用程序。我已经完成了以下工作。但是问题是Slack框架使用await

app.command("/command", async({ ack, payload, context })=>{
  ack();
  var details;
  try{
    apiHandler(function(details) {
    const result = await app.client.chat.postMessage({  //<--- error here
      token: context.botToken,
      channel: payload.channel_id,
      blocks: [
        {
          type: "section",
          text: {
            type: "mrkdwn",
            text: details
          },
        }
      ],
      text: "Message from Test App"
    });
      console.log(details);
    });
  }catch(error)
  {
    console.error(error);
  }
});

//helper function:
function apiHandler(callback) {
  let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  let xhr = new XMLHttpRequest;
     xhr.open("GET","API-URL",true);
     xhr.onload = function()
     {
         if(this.status===200){
           callback(JSON.parse(this.responseText););
         }
       }
       xhr.send();
}

启动节点时,我得到以下信息:

SyntaxError:等待仅在异步功能中有效

我对node.js还是很陌生,我完全不知所措

javascript node.js slack
1个回答
0
投票

将异步关键字放在apiHandler(async function(details) {上>

© www.soinside.com 2019 - 2024. All rights reserved.