I have a question related to sending a message to Slack from Jenkins (groovy script).
Currently, I use SlackNotificationPlugin for sending messages to Slack and I'm trying to send line for the whole width of Slack, but instead of it, I can use only half of Slack width.
I use code below:
msg = readJSON text: """
{
"pretext": "<placeholder>",
"text": "<placeholder>",
"color": "good",
"mrkdwn_in": [
"pretext",
"text"
],
"fields": [
{
"title": "A field's title",
"value": "This field's valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa",
"short": false
},
{
"title": "A short field's title",
"value": "A short field's value",
"short": true
},
{
"title": "A second short field's title",
"value": "A second short field's value",
"short": true
}
],
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Alternative hotel options*"
}
}
]
}
"""
msg.pretext = pretext.inspect()
msg.text = text.inspect()
slack_channel = partner_channel
slackSend(channel: slack_channel, color: 'good', attachments: "[${msg.toString()}]")
On the screenshot below you can see Packages Difference and it showed only on the half of the page.
I found a solution for this case and I tested it in Slack Block Kit Builder:
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "```{8}```"
}
}
]
}
https://app.slack.com/block-kit-builder/T025QN6JG#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22%60%60%60%7B8%7D%60%60%60%22%7D%7D%5D%7D
It Slack Block Kit Builder, I can successfully send a message and use the whole space of slack:
In official documentation for Jenkins Slack Plugin (https://plugins.jenkins.io/slack/), I found that for sending blocks we can use the next construction:
blocks = [
[
"type": "section",
"text": [
"type": "mrkdwn",
"text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
]
],
[
"type": "divider"
],
[
"type": "section",
"text": [
"type": "mrkdwn",
"text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
],
"accessory": [
"type": "image",
"image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
"alt_text": "alt text for image"
]
]
]
slackSend(channel: "#general", blocks: blocks)
The mentioned code doesn't work for me I have nothing in Slack when I use block construction.
Can somebody help me with it? I don't use any Bot User, I just want to send the output of Jenkins to Slack.
Thanks in advance.
Related
I have used zoom integration with slack. When I send /zoom command the bot responds with a new meeting link in a box layout that looks really good.
Note the bordered box below "Call"
However I am not able to create similar box layout with border and section in my own slack bot. Which block kit syntax should I use to achieve the same design?
At max what I am able to achieve is like the below:
I did not find any relevant example in the documentation as well.
There are multiple ways suggested in slack's official documentation to format Messages in responses.
For instance:
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
},
"accessory": {
"type": "image",
"image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "divider"
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Farmhouse",
"emoji": true
},
"value": "click_me_123"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Kin Khao",
"emoji": true
},
"value": "click_me_123",
"url": "https://google.com"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Ler Ros",
"emoji": true
},
"value": "click_me_123",
"url": "https://google.com"
}
]
}
]}
You can use these components to design your own experience in response.
Further reading: https://api.slack.com/messaging/composing
I think the elements you are referring to are produced by the Call API
It doesn't seem that it is an available block for use in message building.
I have some code that was working just fine a few months ago, but something in the Graph API has changed and this no longer works. I am trying to create a message in an existing folder, by doing a POST to this url:
https://graph.microsoft.com/v1.0/users/jjones#transend.onmicrosoft.com/mailFolders/AAMkADNjAAA=/messages
(folder id shortened)
The call fails with http error 400, and the returned error is "UnableToDeserializePostBody". The input json is shown below. By experimentation I was able to trace the problem specifically to "singleValueExtendedProperties". I normally put several properties there, but for this test I removed all but the one you see. I have tried other properties as well, they all fail. This seems like some stupid formatting error but I can't see it. Any help appreciated.
{
"subject": "Test again",
"Sender": {
"emailAddress": {
"name": "John Doe",
"address": "missing#domain.com"
}
},
"body": {
"contentType": "TEXT",
"content": "This is a text message."
},
"toRecipients": [
{
"emailAddress": {
"name": "Jane Smith",
"address": "missing#domain.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"name": "Bob Jones",
"address": "missing#domain.com"
}
}
],
"singleValueExtendedProperties": [
{
"propertyId": "SystemTime 0x0039",
"value": "1998-07-29T21:30:00.0000+00:00"
}
],
"importance": "normal"
}
The main problem here is you are specifying the property('propertyid') in singleValueExtendedProperties object is not valid. There are only 2 properties in singleValueExtendedProperties. One is id and the other is value.
Replace 'propertyId' with id.
I have tested it in POSTMAN with your payload changing the propertyId to id and it worked.
Request Body:-
{
"subject": "Test again",
"Sender": {
"emailAddress": {
"name": "John Doe",
"address": "missing#domain.com"
}
},
"body": {
"contentType": "TEXT",
"content": "This is a text message."
},
"toRecipients": [
{
"emailAddress": {
"name": "Jane Smith",
"address": "missing#domain.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"name": "Bob Jones",
"address": "missing#domain.com"
}
}
],
"singleValueExtendedProperties": [
{
"id": "SystemTime 0x0039",
"value": "1998-07-29T21:30:00.0000+00:00"
}
],
"importance": "normal"
}
I'm building a simple slack bot and I am playing with the checkboxes element.
When I return the following from my API in a JSON response to a slash-command I get an error failed with the error "invalid_blocks", however, when I put this in the block-kit-builder it works perfectly (including "sending to slack" button)
Any ideas why this is failing when I run my slash command - and is it possible to see more detailed error messages from slack?
{
"blocks": [
{
"elements": [
{
"style": "primary",
"text": {
"emoji": true,
"text": "Create new TODO list",
"type": "plain_text"
},
"type": "button",
"value": "value"
},
{
"style": "primary",
"text": {
"emoji": true,
"text": "Help",
"type": "plain_text"
},
"type": "button",
"value": "value"
}
],
"type": "actions"
},
{
"text": {
"text": "Today",
"type": "mrkdwn"
},
"type": "section"
},
{
"elements": [
{
"initial_options": [
{
"text": {
"text": "Get Into the garden",
"type": "mrkdwn"
},
"value": "foo"
}
],
"options": [
{
"text": {
"text": "Get Into the garden",
"type": "mrkdwn"
},
"value": "foo"
}
],
"type": "checkboxes"
},
{
"style": "primary",
"text": {
"emoji": true,
"text": "Add new Task",
"type": "plain_text"
},
"type": "button",
"value": "value"
}
],
"type": "actions"
}
],
"type": "home"
}
I am using the Slack Web API. I was getting the similar error. After a lot of looking around, here's how I solved it.
import json
blocks = [{...}]
payload = {
"blocks": json.dumps(blocks)
}
You will then send this payload.
in the api, the "blocks" parameter need to be string type. Did you convert it to string or you use it as a JSON ?
https://api.slack.com/methods/chat.postMessage
In the Block Kit Builder, the data is a JSON with a blocks key.
In the Slack API, the blocks param is only the list of JSON objects.
blocks = [
{
"text": {
"text": "Its the list of your blocks",
"type": "mrkdwn"
},
"type": "section"
}
]
text = 'Alternative data in text'
client.chat_postMessage(channel=channel_id, blocks=blocks, text=text)
Another cause of this problem seems to be too many blocks being returned. I can't find any documentation about this whatsoever, but personal experience seems to indicate about 20 blocks is the maximum.
An alternative is to return fewer blocks, with paging actions -- paging works well with the "replace" message so that the content being paged through does not result in many separate messages.
It appears that not all valid elements in block kit tool can be posted as a message, despite the fact that message preview works fine in the Block Tool.
In my case, the code failed when I included an input block and passed when i removed it. The input block was generated by the Block Kit tool.
{
"type": "input",
"element": {
"type": "plain_text_input",
"action_id": "plain_text_input-action"
},
"label": {
"type": "plain_text",
"text": "Feedback",
"emoji": true
}
}
The error was
{'ok': False, 'error': 'invalid_blocks'}
Also, although the documentation for python says you need to urlEncode the JSON-based array, there is no example, and it is incorrect. https://api.slack.com/methods/chat.postMessage
You can see on line 29 in the SDK test code below that blocks= takes a regular list of dicts not a string.
https://github.com/slackapi/python-slack-sdk/blob/c9dc6aa0907a72c16cf36aa15e7e80031a9fdce2/integration_tests/samples/basic_usage/sending_a_message.py
I have a slack bot where I am sending structured message using Tester on chat.postMessage() using Developer account for Slack App.
Message gets sends smoothly, but when button is clicked it shows-
"Oh no, something went wrong. Please try that again." in the chat window and nothing recieved on Interactive Message Request URL.
I am out of clue what is happening, Any help will be appreciated.
Message attachment
[
{
"title": "The Further Adventures of Slackbot",
"fields": [
{
"title": "Volume",
"value": "1",
"short": true
},
{
"title": "Issue",
"value": "3",
"short": true
}
],
"author_name": "Stanford S. Strickland",
"author_icon": "http://a.slack-edge.com/7f18/img/api/homepage_custom_integrations-2x.png",
"image_url": "http://i.imgur.com/OJkaVOI.jpg?1"
},
{
"title": "Synopsis",
"text": "After #episod pushed exciting changes to a devious new branch back in Issue 1, Slackbot notifies #don about an unexpected deploy..."
},
{
"fallback": "Would you recommend it to customers?",
"title": "Would you recommend it to customers?",
"callback_id": "comic_1234_xyz",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "recommend",
"text": "Recommend",
"type": "button",
"value": "recommend"
},
{
"name": "no",
"text": "No",
"type": "button",
"value": "bad"
}
]
}
]
it is sorted now. Actually I was using wrong token, test token, where a bot access token or user access token obtained through the app's OAuth flow must be used.
I have the following table structure for example - Users, Projects.
Users has_many Projects
Projects has_many Photos
But i want to embedded certain basic content of Projects and Photos into the Users Table so that i can reduce the number of database calls for certain pages.
Below is the structure i have come up with.
Users Table
[
{
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha#mink7.com",
"gender": "male",
"telephone": "9986377561",
"is_pro": 1,
"projects": [
{
"id": "LONG-MONGO-ID-HERE-PROJECT-1",
"name": "Nike",
"url": "http://nike.com",
"logo": "logo_nike.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project1_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project1_photo2.jpg"
}
]
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project2_photo2.jpg"
}
]
}
]
},
{
"id": "LONG-MONGO-ID-HERE-USER-2",
"name": "Pruthvi Gowda",
"email": "pruthvi#mink7.com",
"gender": "male",
"telephone": "9982318016",
"is_pro": 0,
"projects": [
{
"id": "LONG-MONGO-ID-HERE-PROJECT-3",
"name": "Adidas",
"url": "http://adidas.com",
"logo": "logo_adidas.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project1_photo3.jpg"
},
{
"title": "another title for an Image",
"file": "project1_photo4.jpg"
}
]
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project2_photo2.jpg"
}
]
}
]
}
]
Projects Table
[
{
"id": "LONG-MONGO-ID-HERE-PROJECT-1",
"name": "Nike",
"url": "http://nike.com",
"logo": "logo_nike.jpg",
"about": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"testimonial": "e middle of text. All the Lorem Ipsum generators on the Internet tend",
"photos": [
{
"title": "Some title for an Image",
"description": "um is simply dummy text of the printing and t",
"file": "project1_photo1.jpg"
},
{
"title": "another title for an Image",
"description": "text of the printing and t um is simply dummy",
"file": "project1_photo2.jpg"
}
],
"user": {
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha#mink7.com"
}
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"about": "It is a long established fact that a reader will be distracted by the",
"testimonial": "from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum",
"photos": [
{
"title": "Some title for an Image",
"description": "um is simply dummy text of the printing and t",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"description": "text of the printing and t um is simply dummy",
"file": "project2_photo2.jpg"
}
],
"user": {
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha#mink7.com"
}
}
]
As I have understood MongoDb is all about replication of data. is this the right way of designing the database structure.
class User
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
field :company_name, type: String
embeds_many :projects, class_name: "Project"
has_many :projects, class_name: "Project"
end
Can i do something like the above so that i can save two instance of the same data.
But as you see in the embedded document i am not storing all the data from the projects - how can i strict it from adding all the data as embedded document but stored as a separate table?
It is not possible to add same model as both embedded and referenced collection. You can put the structure as same you have mentioned initially by referencing the project collection in user.
And create dummy model say ImportantProject which will be embedded in User and you can sync up the data project in this model while making changes in database.