How to send a message in the Slack channel using Flutter?

Viewed 1825

I am creating an attendance app in which if an employee got the approval on leave request, then the app should send a message in the Slack channel.

I have seen this flutter package on the pub but it is not very helpful:-

https://pub.dev/packages/flutter_slack_oauth

Any idea on how I can send a message in the Slack channel using Flutter?

2 Answers

Finally got the answer, and wants to share :)

Here is the slack API URL to create a new app:-

https://api.slack.com/apps

After creating a new app, activate the Incoming Webhooks feature:-

enter image description here

Grab the webhook URL for your workspace, and make a request via this Flutter function:-

import 'dart:convert';
import 'package:http/http.dart' as http;

sendSlackMessage(String messageText) {
  //Slack's Webhook URL
  var url = 'https://hooks.slack.com/services/TA******JS/B0**********SZ/Kk*******************1D';

  //Makes request headers
  Map<String, String> requestHeader = {
    'Content-type': 'application/json',
  };

  var request = {
    'text': messageText,
  };

  var result = http
      .post(url, body: json.encode(request), headers: requestHeader)
      .then((response) {
    print(response.body);
  });
  print(result);
}

You can also use slack post api for posting message. var url = "https://slack.com/api/chat.postMessage"; var headers: { "Content-type": "application/json", "Authorization": 'Bearer $oAuthToken' }; var body = {"channel": "Your_Channel_Id", "text": ":tada: :tada: :tada:", "as_user": true};

Add scope chat:write in your app configuration. And now you will be able to send message to you channel. Hopefully it helps.

Related