is there an api method for slack that takes a channel name as an argument and returns a token? - jenkins

I'm trying to setup a Jenkins job to be able to take a slack channel name as a parameter, and then post messages, about who kicked off the build, and if it completed etc, how can my job get a slack token, for any given channel?

No. This is not how tokens work with the Slack API. Tokens are always user based and for the whole workspace. So once you have a token you app can post to any public channel.
To get a token a user needs to install your Slack app. *)
Alternatively can create "dynamic" webhooks, that can post to any channel in a workspace. Check out this answer for details:
Creating a Slack Webhook programmatically
However, those are officialy legacy and might stop working some time in the future. So your safest approach would be to create a Slack app and install it to the workspace to get your token.
*) There once was a concept called "workspace tokens". You still find it in the documentation, but it never left the BETA stage and was canceled. Best to ignore it.

Related

Slack Oauth - how do I force my slack app to receive messages in a certain channel?

I'm building a Slack app and when I install the app in my workspace, during the Oauth flow it asks me what channel I want messages sent to. I then have to choose among all my channels in a select menu (one of which is my slack app) where to receive messages.
When I install something like the Jira Cloud Slack app, it does not ask me what channel I want to use to receive messages, it just installs the app and I receive messages in the Jira Cloud channel. I'd like to try to set up my slack app so the user does not have to select a channel, but instead the messages go automatically to the app channel (like Jira).
Does anyone know what settings (Oauth scopes maybe?) I need to make this happen?
Are you using Slack's incoming webhooks? Webhooks will require you to specify a channel, but using Sign in with Slack for auth (or just authenticating with their OAuth v2) does not. Also make sure that you're using Slack's most recent version of apps that include granular scopes -- not sure if the legacy apps work like this.

Token for other spaces [duplicate]

I'm trying to create a bot that people can install in their workspaces and send/receive messages of some sort. The problem I'm running into is the documentation tells you to use the token generated for your workspace:
https://api.slack.com/bot-users#installing-bot
But this makes no sense, seeing as people would be installing the App/Bot on their workspace themselves, it's not like they would be giving me their token or anything. Surely I'm missing something. How am I meant to authenticate a bot against whatever workspace it is installed in a purely API driven way?
There is no "workspace agnostic" Slack token. Every token is linked to a specific workspace and user.
The usual approach is that you provide an installation routine with Oauth 2.0 for your Slack app. When someone installs your app with that routine a new token is created internally that your app needs to collect and store. This is how every Slack app works.
Note that the installation has to be done once only for each workspace. The token does not expire.
Its simply when you install your Slack app on your own workspace only. That can be done directly from the Slack app management window and requires no installation routine. But then you obviously need to manually copy the generated token to your app.

Getting tokens for all users in a workspace

I have been making a slack app for the users on my workspace. It is a sidebar that adds slack messaging functionality to our website, so that we don't have to leave the site to see our slack messages. I am having trouble trying to get bearer tokens for each user.
What I have been doing so far is following the Slack OAuth 2.0 Authentication flow in order to receive tokens for users. This worked for me in testing and it works for some of our users currently. However, some users see something completely different.
Instead of asking them for permission to use their slack profile, the slack.com/oauth/authorize is telling them they can't install the app because it isn't listed in the slack directory. However, this page should not be installing the app to the workspace. It is already installed. It should just be asking for their permission to use their profile.
Am I using the wrong page? Did I miss something I need to do?
The Oauth process in Slack is not only used to get an access token, but also always is regarded as installation process for the respective Slack app. So your users are basically (re-) installing your Slack app each time they run through the Slack Oauth process. This is the standard behavior and can not be changed.
If you want to continue using this process you can simple enable installation for your Slack app on the workspace for all users (click on approve on the app management page of your workspace for this particular app) and then your users will no longer get the error message. You may also need to enable distribution of your Slack app on the app management page.
Btw. installing the same Slack app by multiple users is the default approach for getting access tokens for individual users. Slack calls those additional installations "configurations" and you can see them listed on the app ages for your workspace.
Note that Slack access tokens obtained from the Oauth process do not expire. So you only have to let the user install your Slack app once and then store the Slack access token for the next time.

generate SLACK_APP_TOKEN for slack application

let's say I've created slack app and I have client id and secret.
What is the easiest way to get SLACK_APP_TOKEN in my hands that will be able to create channel?
If you want a proper access token that is related to your Slack App the only way to get it is to install your Slack app with the OAuth process as described here in the Slack documentation. You will need a mini website with a script (e.g. PHP) to perform the installation.
The so called test token will also allow you use the Slack API (e.g. to create a channel), provided that the user that created the test token has that right on your Slack. It is the easiest to obtain, but it will always be linked to a user account, not a Slack app. And you can not request specific scopes for it. So for most applications its better to use a Slack App and get a proper access token by installing it.
If you are looking for an example for an installer script, here is a complete script in PHP. It will run on any webserver that supports PHP. Its very basic, but it will work just fine.

Create slack channel using slack app

I have a private slack application (developed by user 'X' from team 'XT')
I have a web server knows how to complete the Oauth process and generate tokens per teams
Now- as a user Y from team YT I am installing the slack app on my YT team and get a token,
using that token I perform API call for channels.create ,
I got into my team (aka YT) and indeed I see that the channel was created ,
BUT
it's written that the channel was created by the specific user that installed the slack app, meaning user Y.
I would expect to see that channel was created by the application not by specific user.
Is there any way to do that ?
thought about using bot token (got from the app instllation) but channels.create cannot be performed by a bot
I am afraid there is no solution for your problem. Every "write" action on Slack has to be attached to either a bot or a user. And since channels.create can not be used by a bot, it has to be a user.
The master access token of your Slack app is linked to the user that installed it, which is why that user will appear as creator of the channel when you use it.
I use a generic admin user ("slackadmin") for that purpose on my own Slack, but that will of course not work as general solution for each Slack team that want to install your app from the Slack App Directory.

Resources