Skip to main content

slack.slack.postmessage

Home > @runlightyear/slack > Slack > postMessage

Slack.postMessage() method

Sends a message to a channel

Signature:
postMessage(props: PostMessageProps): Promise<HttpProxyResponse>;

Parameters

ParameterTypeDescription
propsPostMessageProps
Returns:

Promise<HttpProxyResponse>

Example 1

Post text message

import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";

defineAction({
name: "postMessage",
title: "Post Message",
apps: ["slack"],
variables: [
{
name: "channel",
description:
"Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name.",
},
{
name: "text",
description: "The formatted text of the message to be published.",
},
],
run: async ({ auths, variables }) => {
const slack = new Slack({
auth: auths.slack,
});
const response = await slack.postMessage({
channel: variables.channel!,
text: variables.text!,
});
console.log("Response data: ", response.data);
},
});

Example 2

Post message with blocks

import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";

defineAction({
name: "postMessageWithBlocks",
title: "Post Message With Blocks",
apps: ["slack"],
variables: [
{
name: "channel",
description:
"Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name.",
},
],
run: async ({ auths, variables }) => {
const slack = new Slack({ auth: auths.slack });

const response = await slack.postMessage({
channel: variables.channel!,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "The header of the message",
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: "A message *with some bold text* and _some italicized text_.",
},
},
{
type: "divider",
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "*Priority*\nHigh",
},
{
type: "mrkdwn",
text: "*Assignee*\nJohn",
},
{
type: "mrkdwn",
text: "*Labels*\nBug",
},
{
type: "mrkdwn",
text: "*Milestone*\nRelease 1.0",
},
],
},
],
text: "Text for screens where blocks are not supported.",
});

console.log("Response data: ", response.data);
},
});