Search code examples
javascriptjsonhttpslackslack-api

Slack Events Request URL - Unverified Request Error


My Slackbot needs to make an HTTP request to Microsoft Azure's Text Analytics API through the Glitch app, but I am receiving the error message below:

An unverified request was sent to the Slack events Request URL. Request body: undefined

I suspect the error is probably not related to the Azure API call, but occurs when a request to Glitch is made without the correct verification token, but I've checked my .env file, and the tokens seem to be there.

negativity: function(slack, message) {
var obj = { 
  documents: [{
  language: "en",
  id: 1,
  text: "hello it's a great day"
}]};

var JSONString = JSON.stringify(obj);

var request = new XMLHttpRequest();

request.onreadystatechange= function () {
if (request.readyState==4 && request.status == 200) { 

  var resultText = request.responseText;
     slack.chat.postMessage({
    channel: message.channel,
     text: `inside callback from negativity`
  })

  }
}

request.open("POST", "x");

request.setRequestHeader("Ocp-Apim-Subscription-Key", "x");
request.setRequestHeader("Content-Type","application/json");
request.setRequestHeader("Accept","application/json");
request.send(JSONString);
}

Solution

  • After some help, it looks like var request = new XMLHttpRequest() isn’t available on server side JavaScript, and an alternative that worked was https://www.npmjs.com/package/axios.

    In addition I installed axios in the console with npm install --save axios.

    I have attached the improvements to the above code.

    const axios = require('axios');
    
    // some code
    
    negativity: function(slack, message) {
    
            axios.post( 'x',
              {
                documents: [{
                  language: "en",
                  id: 1,
                  text: message.text
                }]
              },
              {
                headers: {  
                  'Ocp-Apim-Subscription-Key':'x',
                  'Content-Type' : 'application/json',
                  'Accept' :'application/json'  
                }
              }).then(function (response) {
                  var output = response.data;
                 });
              }