Adam
Adam

Reputation: 4168

Posting Message to Slack via NodeJS Lambda Function - Data Variable Not Rendering

I've got an AWS lambda function implemented in Node that posts a message to a Slack channel. I've created a SlackApp, with the incoming webhook feature enabled. I'm sending a message to the hook via https. The lambda calls the following function that formats the message:

function slackConvertFromSNSMessage(sns_event) {
    let slack_message;
    let slack_message_text;
    const slack_message_user = 'foo';
    const slack_use_markdown = true;
    const sns_message_raw = sns_event.Records[0].Sns.Message;
    const sns_message_date_epoc = new Date(sns_message_raw.StateChangeTime).getTime();

    slack_message_text = `
*Alert:* One or more errors were report by ${sns_message_raw.AlarmName}
*Date:* <!date^${sns_message_date_epoc}^{date_num} at {time_secs}^|${sns_message_raw.StateChangeTime}>
*Region:* ${sns_message_raw.Region}
*Queue:* ${sns_message_raw.Trigger.Dimensions[0].value}
`
// "*bold* `code` _italic_ ~strike~"

    slack_message = {
        text: slack_message_text,
        username: slack_message_user,
        mrkdwn: slack_use_markdown,
    }

    return JSON.stringify(slack_message);
}

The message in slack appears as follows:

enter image description here

The variable isn't rendering. I just see the statement I'm passing to the slack API. I expect to see the supplied date formatted to the user's local time zone.

UPDATE: There was a good comment noticing the carrot before the pipe in the declaration. I removed it. I'm still getting the problem, but that line in the code now looks like the following:

*Date:* <!date^${sns_message_date_epoc}^{date_num} at {time_secs}^|${sns_message_raw.StateChangeTime}>

Upvotes: 0

Views: 1050

Answers (1)

tkausl
tkausl

Reputation: 14279

If you do not specify a optional_link you have to remove the last ^ delimiter, i.e. the ^ right before the |. Their documentation doesn't seem to specify this but I can reproduce the problem in the Message Builder.

Edit: And Slack expects a epoch in seconds while getTime() returns a epoch in milliseconds.

Upvotes: 1

Related Questions