GithubHelp home page GithubHelp logo

module-slack's Introduction

Module Slack

Ballerina Slack connector allows you to work with slack conversations/channels, users, user groups, files and messages through the Slack Web API. It handles OAuth 2.0 authentication.

There are basically 5 clients provided by Ballerina to interact with different API groups in Slack Web API.

  1. slack:Client - This client is the top most client in the slack module. This can be used to get the relevant client associated with the operation that you wish to execute.
  2. slack:ChatClient - Chat client can be used in messaging related operations. For example: post messages on slack, delete messages, send attachments.
  3. slack:ConversationsClient - This client can be used in conversations/channels related operations. For example: create conversations, join a conversation, add users to a conversation, archive/unarchive conversations etc.
  4. slack:UsersClient - This client can be used in users/user groups related operations. For example: get user information etc.
  5. self.filesClient - This client can be used in file related operations in slack.

Compatibility

Version
Ballerina Language 1.1.4

Sample

Creating a simple slack application and obtaining tokens

  1. Create a new Slack App on api.slack.com.
  2. Type in your app name.
  3. Select the workspace you'd like to build your app on. You can create a work space here if you don't have one already.
  4. Give your app permission. Scopes allow your application to do operations in your workspace. Navigate to OAuth & Permissions on the sidebar to add scopes to your slack application.
  5. After giving permission, install the application in your workspace by clicking Install App to Workspace.
  6. Next, authorize the app for the user permissions. Click Allow button.
  7. Copy and save your token. This will be used to communicate with slack's platform.

Create Slack client

First, import the wso2/slack module into the Ballerina project.

import wso2/slack;

Instantiate the slack:Client by giving OAuth2 authentication details in the slack:Configuration.

You can define the Slack configuration and create the top most Slack client as mentioned below.

slack:Configuration slackConfig = {
    oAuth2Config: {
        accessToken: "<token-here>",
        refreshConfig: {
            clientId: "<CLIENT_ID>",
            clientSecret: "<CLIENT_SECRET>",
            refreshToken: "<REFRESH_TOKEN>",
            refreshUrl: "<REFRESH_URL>"
        }
    }
};

// Create the top most Slack client.
slack:Client slackClient = new(slackConfig);

Slack operations related to Converations/Channels

The listConversation remote function can be used to list all channels in a slack team.

slack:ConversationsClient conv = slackClient.getConversationsClient();
var response = conv->listConversations();
if (response is Conversations) {    
    io:println("Conversations " + response);
} else {
    io:println(response.detail()?.message.toString());
}

Slack operations related to chat messages

The postMessage remote function can be used to send a message to a slack channel.

slack:ChatClient chat = slackClient.getChatClient();
var response = chat->postMessage(channelName, "Hello Channel");
if (response is string) {    
    io:println("Message posted to channel " + response);
} else {
    io:println(response.detail()?.message.toString());
}

Slack operations related to users

The getUserInfo remote function can be used to get information about a user.

slack:UsersClient users = slackClient.getUsersClient();
var response = users->getUserInfo(userName);
if (response is User) {    
    io:println("User id " + response.id);
} else {
    io:println(response.detail()?.message.toString());
}

Slack operations related to files

The uploadFile remote function can be used to upload or create a file.

slack:FilesClient files = slackClient.getFilesClient();
var response = files->uploadFile(filePath, channelName);
if (response is FileInfo) {    
    io:println("File id " + response.id);
} else {
    io:println(response.detail()?.message.toString());
}

module-slack's People

Contributors

bhashinee avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.