Skip to content

Instantly share code, notes, and snippets.

@Imperial-lord
Created March 21, 2022 10:09
Show Gist options
  • Save Imperial-lord/09e9e973bd8d6364f03ef1af0854bce4 to your computer and use it in GitHub Desktop.
Save Imperial-lord/09e9e973bd8d6364f03ef1af0854bce4 to your computer and use it in GitHub Desktop.
import 'dart:convert' show utf8;
import 'dart:convert' show json;
import 'dart:io';
class SlackMessage {
final String? inChannel;
final String? message;
const SlackMessage({
required this.inChannel,
required this.message,
});
Future<bool> send(String webhookUrl) async {
final payload = {
'text': message,
if (inChannel != null) 'channel': inChannel!,
};
final request = await HttpClient().postUrl(Uri.parse(webhookUrl));
final payloadData = utf8.encode(json.encode(payload));
request.add(payloadData);
final response = await request.close();
return response.statusCode == 200;
}
}
const webhookUrl =
'<enter your webhook url>';
void testSend(String? newMessage) async {
String defaultMessage = 'Hi there! I am dart';
final message = SlackMessage(
inChannel: '#software-development',
message: newMessage ?? defaultMessage);
if (await message.send(webhookUrl)) {
print('Message sent successfully');
} else {
print('Message not sent');
}
}
void main() {
stdout.write("Enter your message : ");
final message = stdin.readLineSync();
testSend(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment