Search code examples
node.jsazure-functionsslackslack-api

Parsing Slack Interactive Message POST payload parameter to JSON in Azure Functions


I'm trying to create an Azure Function to take POST requests from Slack Message Interactions. I'm able to get a test request to come in following this guide using ngrok. However the payload is not coming in like a normal POST request body. Assuming this is because it's a "parameter" payload and not a body.

module.exports = async (context, req) => {
    const { body } = req;
    context.log(body);

    context.res = {
        body,
    };
};

Output:

payload=%7B%22type%22%3A%22block_actions%22%2C%22user%22%3A%7B%22id%22%3A%22xxx%22%2C%22username%22%3A%22...

How do I parse this POST parameter payload into JSON in an Azure Function?


Solution

  • With help from this post I was able to figure this out for my use case.

    Using qs package npm i qs

    const { parse } = require('qs');
    
    module.exports = async (context, req) => {
        const payload = JSON.parse(parse(req.rawBody).payload);
        context.log(payload);
    
        context.res = {
            payload,
        };
    };
    

    Output:

    {
      type: 'block_actions',
      user: {
        id: 'xxx',
        username: 'xxx',
        name: 'xxx',
        team_id: 'xxx'
      },
      api_app_id: 'xx',
    ...
    }