-1

I try to create a lambda function to post a message in Slack.

const { WebClient } = require('@slack/client');

const token = '...';
const web = new WebClient(token);
const channel = '#...';

exports.handler = (event) => {
    console.log('First');

    web.chat.postMessage({ 
        channel: channel, 
        username: '...',
        icon_emoji: '...', 
        text: 'Hello world'
    })
        .then(() => {
            console.log('Ok');

            return {
                statusCode: 200
            };
        })
        .catch((error) => {
            console.log('Error', error);

            return {
                statusCode: 500,
                body: error
            };
        });

    console.log('Finish');
};

My question : how I can do to return the return in my then and catch function ?

Actually, lambda return null. (Ok, because I haven't return in my main function). So If my function failed, Lambda return still an answer (200).

Maybe I don't use correctly Lambda and Promise. Moreover I don'y really understand the keyword await before (event) => {. I'm forced to remove it otherwise my function does not work.

Aurélien W
  • 123
  • 12

1 Answers1

0

Your lambda function will need to return a response. For example if you construct your response as:

const response = { statusCode: 200, body: JSON.stringify(somedata) },

somedata can be a message if you do not return any important data.

then you do as a return callback(null, response). Btw you are missing callback from the functions parameter.

squeekyDave
  • 918
  • 2
  • 16
  • 35