#channel Slack notification in Jenkins post-build action - jenkins

I am integrating a Slack bot in Jenkins post-build action. I have been able to send notification and create custom messages using the Slack plugin, however I wanted to send a #channel or #bot in the bot message which can be used to urgently notify the team.
Is there any existing way/hack to do this? Unfortunately, using these keywords via the plugin just result in them being rendered as plain text, without any broadcast action.

To mention someone or a channel you can use one of the following syntax:
<!channel>
<!group>
<!here>
<!everyone>
<#USERID>
<#CHANNELID>
See Basic message formatting for more information.

Related

Lookup user id by email in slack bot

Use case
I have JIRA where users can create issues/cards. Whenever a user creates the card, I would like to mention the creator in the slack channel.
I created a slack bot and webhook URL. Added the webhook URL in JIRA and it's sending the message.
Message that will be sent from JIRA
Hi, <#issue> ,
Please look into this issue `{{issue.summary}}`
{{issue.url.customer}}.
Reporter - {{issue.reporter.emailAddress}}
JIRA can give me only the reporter email address, but to mention the user in slack i need the user id of slack.
Is there any way we can achieve the same?
There is this API : users.lookupByEmail
you can use this to fetch userId, but given that you are using just Webhook URL, you'll need to implement additional code.

Slack api conversations.history returns error: not_in_channel

I'm starting out with the Slack API and trying to just get a list of messages.
Here are my steps:
Created a Slack app and gave it channels:read and channels:history scope (also re-installed it)
Queried the list of channels with conversations.list (this worked fine)
From the output of conversations.list, I found a channel that I use and copied the id
Used the conversations.history api with the channelid from step 3
Result:
{ "ok": false, "error": "not_in_channel" }
I'm not at all sure what is happening here. I definitely have messages in the channel, and the documentation page for that api does not say anything about this "not_in_channel" error code.
What am I doing wrong?
After a long time of investigations (~2 hours), I found an easy approach. For Caleb's answer, I didn't understand how to invite a Bot to the channel. Hence, I am posting this answer.
Go to your Slack Channel and type the following as a message.
/invite #BOT_NAME
Eg: If your Bot name is SRE Incident Manager the command would be as follows.
/invite #sre_incident_manager
As soon as you start typing #, Slack will automatically suggest. So it becomes easy. For this, the Bot needs to be added to your Slack Workspace.
PS: Original answer.
The error not_in_channel has the exact meaning, your custom Slack app should be added to the channel.
Exact solution 1
To resolve the error, in the Web Slack interface:
Open channel settings
Click on the Integrations tab
Click Add apps and find your custom app.
Slack app might have different interface, see Iryna Vernik's answer.
Alternative solution 2
Give access to the bot to all channels by adding workspace wide scope, for example, chat:write.public. Depends on your needs and security requirements.
Alternative solution 3
To access the channel chat from API specify Incoming webhook. Slack will generate a unique URL with the token per each channel. Only convenient for a few channels.
This error arises when you are using the bot oauth token and the bot is not invited to the channel. To solve this you need to
Invite the bot(slack app) to join the channel.
Use the OAuth Access Token instead
To add Bot to your channel you need to write /invite #Bot_name in the slack channel
I also didn't understand how to invite a Bot to the channel. Way that was proposed by Caleb and Keet was not clear for me or not working. From my side, 'invite' work after
open channel
in Details tab, choose a 'More'clause
in dropdown menu, chouse an 'add app'
in pop-up look for you app (bot)
Also i was use Bot User OAuth Access Token, because i need this functionality in private channel (additionaly, you should add for bot groups:history scope)
FOr me, instead of invite a user/bot, I invite the app.
I'm getting started with the Slack API as well, and I've come to realize that not_in_channel simply means that the user/bot you are using the token for hasn't joined that particular channel you're trying to perform an action on.
Think of it this way: if you're using Slack on the web-browser or web-app, you wouldn't be able to post a message on a channel you haven't either joined or was invited to.
☝️ You'll also never run into this issue through the Slack UI/UX because you're not even able to access the channels UNTIL you are invited or join it.
Click to see png example of a slack message stating my bot being added to a channel
However, because we're using the API we can essentially skip some steps, and in this case we skipped the step where a user/bot has joined the channel before doing the action we're trying to perform (writing a message, grabbing information, etc).
💪 How to address this
There's probably plenty of ways to do it that I'm not versed in, but if you're just concerned about a specific channel or two without the concern of scaling to x channels I'll list the way that worked for me.
📇 /invite Slash Command
As others have mentioned, putting /invite in the message box lets you use Slack's slash command shortcut to add users. What's important is this way also allows us to invite bots to the channel.
Putting "#" triggers Slack to start auto-suggesting, which is why it then becomes easy to find your bot name in the list.
Click here to see screenshot example of the /invite command with #bot_name_here
Hope this helps answer people's question on why it's happening, and thank you to the original posts that got me out of my initial mess. 🙏
As all others said, you need to join each channel.
The bot can join channel programmatically by using API below:
https://api.slack.com/methods/conversations.join
Don't forget to add permission of conversations.join

Send mail having test results generated at jenkins ui

I am trying to send generated test results via mail. I am able to see the summary of test cases generated at jenkins ui at http://<URL>:8080/job/<JOB_NAME>/<BUILD_NUMBER>/testReport/. How ever I want to send this in email, but, unable to find any solution for this. Does anyone knows how to do that??
you can use "Editable Email Notification" plugin and attached the build logs

Slack integration like Giphy, that responds from the user instead of from a Bot

I'm attempting to create a new slack command integration that when invoked will fetch a unique set of data for the user. I have the integration working, however the data returned is displayed within slack as a "BOT".
Ideally, the integration will be similar to Giphy. When the /giphy command in slack is invoked, an image is returned that is appended to the user's message. I'd be curious to know how they did this. As far as I can tell, Slack integration APIs only allow responses to be sent as BOTs not as a user or appended to a user's message.

How to send mail in Jenkins at the beginning of a job?

We used our own custom action to send mail that once the automation has been started from Final builder. Now am using Jenkins. As long as i have searched, i found that we can send mail about Jenkins job success or failure at the end of Job. Is there any possible to send mail at the beginning of Jenkins job started? If so, what plugin do i need to use for it? Please help me on this.
You can use Email Extension plugin.
To configure see the article How to send Email at every build with Jenkins. After configuring the settings follow these steps:
Select Add post-build action
Choose Editable Email Notification
In Project Recipient List you can use either default list or recipient's e-mail address
Select Avanced Settings...
Select Add Trigger and choose Before Build
That's it.

Resources