Gagandeep Gambhir
Gagandeep Gambhir

Reputation: 4353

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?

Upvotes: 3

Views: 3370

Answers (3)

nauman mir
nauman mir

Reputation: 154

You can also use the Slack post API for posting messages.

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.

Upvotes: 0

Dipen Maharjan
Dipen Maharjan

Reputation: 11

You can directly use Slack Logger Package slack_logger https://pub.dev/packages/slack_logger to send your message.

It's very easy to use as well

Along with messages, you can also send image, image with text block, markdown text with button, text as attachments and markdowns as attachments which will also help you to send error logs directly to your slack channel.

  1. Initialize SlackLogger
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {

    SlackLogger(webhookUrl: "[Add Your Web Hook Url]");

    return MaterialApp(
      ...
    );
  }
}

Create Instance of SlackLogger

final slack = SlackLogger.instance;

Send Message:

...

slack.send("This is a error log to my channel");

...

Upvotes: 1

Gagandeep Gambhir
Gagandeep Gambhir

Reputation: 4353

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

Upvotes: 4

Related Questions