Search code examples
flutterdartslack

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


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?


Solution

  • 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);
    }