Dart wrapper for posting messages to Slack using Incoming Webhooks.
Incoming Webhooks are a simple way to post messages from apps into Slack. Creating an Incoming Webhook gives you a unique URL to which you send a JSON payload with the message text and some options. You can use all the usual formatting and layout blocks with Incoming Webhooks to make the messages stand out.
To get the WEBHOOK_URL you need:
- Create a Slack app (if you don't have one already)
- Enable Incoming Webhooks
- Create an Incoming Webhook
- Use your Incoming Webhook URL to post a message
Read more about webhooks here.
This method posts a message to a public channel, private channel, or direct message conversation.
final slack = SlackNotifier('WEBHOOK_URL');
slack.send(
'Hello world',
channel: 'general',
iconEmoji: ':chart_with_upwards_trend:',
iconUrl: 'https://picsum.photos/48/48',
username: 'My Bot',
blocks: [SectionBlock(text: 'Hello world')],
attachments: [Attachment(pretext: 'pre-hello', text: 'text-world')],
);The usage of the text field changes depending on whether you're using blocks. If you're using blocks, this is used as a fallback string to display in notifications. If you aren't, this is the main body text of the message. It can be formatted as plain text, or with mrkdwn.
Blocks are a series of components that can be combined to create visually rich and compellingly interactive messages. Block Kit can make your app's communication clearer while also giving you consistent opportunity to interact with and assist users.
ActionsBlockHolds multiple interactive elements.ContextBlockProvides contextual info, which can include both images and text.DividerBlockVisually separates pieces of info inside of a message.FileBlockDisplays info about remote files.HeaderBlockDisplays a larger-sized text block.ImageBlockDisplays an image.InputBlockCollects information from users via block elements.RichTextBlockDisplays formated, structured representation of text.SectionBlockDisplays text, possibly alongside block elements.VideoBlockDisplays an embedded video player.
Individual blocks can be stacked together to create complex visual layouts.
var blocks = [
HeaderBlock(text: 'Onboarding'),
SectionBlock(text: 'Example message for engaging new users.'),
DividerBlock(),
SectionBlock(text: "Hey there :wave: I'm *TaskBot*. I'm here to help you create and manage tasks in Slack."),
ImageBlock(
imageUrl: 'https://api.slack.com/img/blocks/bkb_template_images/onboardingComplex.jpg',
altText: 'image1',
title: 'image1',
),
];
slack.send('Onboarding', channel: 'general', blocks: blocks);