I have joined viblo for a long time but this is my first time writing this article. What’s wrong looking forward to the only physician
Just now, my brother in the company made an app to log errors on Slack. Looks also more intuitive and fun than reading mail. Enlist me to stop by and lick and write 1 article.
Mechanism of action
The mechanism is quite simple: Slack lets us create apps in the workspace. From this app create a webhook link to a channel. When firing a post request in Slack format, Slack will send a message to the channel
Perform
Create slack app
First, we need to create the slack app. Visit Slack API homepage: https://api.slack.com/ , click on Start building . A dialog box appears as shown below.
You enter the name of the app you want to create + select the workspace for the app and then click submit.
This next screen Slack lists quite a few features. For the purposes of this article, we just created a bot to log simple errors. So only use the Incoming webhook feature. You click on Incoming webhook
You will be redirected to the screen below. Set the status bar to enable active incoming webhook.
Scroll down, click on Add new webhook to workspace . You are redirected to the Authorize page. Here, select the channel you want to message and then click authorize
Now Slack will generate for you a webhook URL so you can send a message.
Try using postman to test:
Basically, here you can create your own simple app to log the error. Whenever you encounter an error, make a POST request and fire at the URL of the other error message (error by the guy, how much, the error message details, … blah … blah) is very intuitive. Do not have to read the log in cmd to make eye contact: facepalm: However, you can refer to the message format section and mention below to make the error log more fun. )
Message format
To make the message look more vivid, you can use the message format in slack. I often use the message format like this to log errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <span class="token punctuation">{</span> <span class="token property">"attachments"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"fallback"</span> <span class="token operator">:</span> <span class="token string">"Required plain-text summary of the attachment."</span> <span class="token punctuation">,</span> <span class="token property">"color"</span> <span class="token operator">:</span> <span class="token string">"#36a64f"</span> <span class="token punctuation">,</span> <span class="token property">"pretext"</span> <span class="token operator">:</span> <span class="token string">"Error when get sexy girl from API"</span> <span class="token punctuation">,</span> <span class="token property">"author_name"</span> <span class="token operator">:</span> <span class="token string">"From: minhphong306 API"</span> <span class="token punctuation">,</span> <span class="token property">"author_link"</span> <span class="token operator">:</span> <span class="token string">"http://flickr.com/bobby/"</span> <span class="token punctuation">,</span> <span class="token property">"author_icon"</span> <span class="token operator">:</span> <span class="token string">"http://static.minhphong306.com/images/daicaphong/me.jpg"</span> <span class="token punctuation">,</span> <span class="token property">"title"</span> <span class="token operator">:</span> <span class="token string">"Open error detail"</span> <span class="token punctuation">,</span> <span class="token property">"title_link"</span> <span class="token operator">:</span> <span class="token string">"https://api.slack.com/"</span> <span class="token punctuation">,</span> <span class="token property">"text"</span> <span class="token operator">:</span> <span class="token string">"See error detail"</span> <span class="token punctuation">,</span> <span class="token property">"fields"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"title"</span> <span class="token operator">:</span> <span class="token string">"Priority"</span> <span class="token punctuation">,</span> <span class="token property">"value"</span> <span class="token operator">:</span> <span class="token string">"High"</span> <span class="token punctuation">,</span> <span class="token property">"short"</span> <span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">,</span> <span class="token property">"image_url"</span> <span class="token operator">:</span> <span class="token string">"http://my-website.com/path/to/image.jpg"</span> <span class="token punctuation">,</span> <span class="token property">"thumb_url"</span> <span class="token operator">:</span> <span class="token string">"http://example.com/path/to/thumb.png"</span> <span class="token punctuation">,</span> <span class="token property">"footer"</span> <span class="token operator">:</span> <span class="token string">"Slack API"</span> <span class="token punctuation">,</span> <span class="token property">"footer_icon"</span> <span class="token operator">:</span> <span class="token string">"https://platform.slack-edge.com/img/default_application_icon.png"</span> <span class="token punctuation">,</span> <span class="token property">"ts"</span> <span class="token operator">:</span> <span class="token number">1537641882</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> |
Your message looks like this
You can read more here to customize more interesting and more suitable formats:
https://api.slack.com/docs/message-formatting
Mention in slack
Sometimes, to focus on work, I only turn on notifications for messages that mention. So sometimes I miss some fatal errors. To overcome this situation, we use the mention function via the user id with fatal errors.
To get a person’s ID, first click on View Profile -> More Action -> Copy MemberID to get the ID
To get your own ID, I don’t know how to get it, so the easiest way is to have another guy copy your ID and send it back to you quickly: v
Add mention of this person’s ID to the slack we use < @user_id >. So with an ID of UC0CE05JP, my message looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <span class="token punctuation">{</span> <span class="token property">"attachments"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"fallback"</span> <span class="token operator">:</span> <span class="token string">"Required plain-text summary of the attachment."</span> <span class="token punctuation">,</span> <span class="token property">"color"</span> <span class="token operator">:</span> <span class="token string">"#36a64f"</span> <span class="token punctuation">,</span> <span class="token property">"pretext"</span> <span class="token operator">:</span> <span class="token string">"Đại ca <@UC0CE05JP> ơi, có phốt rồi :facepalm:"</span> <span class="token punctuation">,</span> <span class="token property">"author_name"</span> <span class="token operator">:</span> <span class="token string">"From: minhphong306 API"</span> <span class="token punctuation">,</span> <span class="token property">"author_link"</span> <span class="token operator">:</span> <span class="token string">"http://flickr.com/bobby/"</span> <span class="token punctuation">,</span> <span class="token property">"author_icon"</span> <span class="token operator">:</span> <span class="token string">"http://static.minhphong306.com/images/daicaphong/me.jpg"</span> <span class="token punctuation">,</span> <span class="token property">"title"</span> <span class="token operator">:</span> <span class="token string">"Open error detail"</span> <span class="token punctuation">,</span> <span class="token property">"title_link"</span> <span class="token operator">:</span> <span class="token string">"https://api.slack.com/"</span> <span class="token punctuation">,</span> <span class="token property">"text"</span> <span class="token operator">:</span> <span class="token string">"See error detail"</span> <span class="token punctuation">,</span> <span class="token property">"fields"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"title"</span> <span class="token operator">:</span> <span class="token string">"Priority"</span> <span class="token punctuation">,</span> <span class="token property">"value"</span> <span class="token operator">:</span> <span class="token string">"High"</span> <span class="token punctuation">,</span> <span class="token property">"short"</span> <span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">,</span> <span class="token property">"image_url"</span> <span class="token operator">:</span> <span class="token string">"http://my-website.com/path/to/image.jpg"</span> <span class="token punctuation">,</span> <span class="token property">"thumb_url"</span> <span class="token operator">:</span> <span class="token string">"http://example.com/path/to/thumb.png"</span> <span class="token punctuation">,</span> <span class="token property">"footer"</span> <span class="token operator">:</span> <span class="token string">"Slack API"</span> <span class="token punctuation">,</span> <span class="token property">"footer_icon"</span> <span class="token operator">:</span> <span class="token string">"https://platform.slack-edge.com/img/default_application_icon.png"</span> <span class="token punctuation">,</span> <span class="token property">"ts"</span> <span class="token operator">:</span> <span class="token number">1537641882</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> |
And the result looks like this:
Hope the article helps you. If there is anything wrong in the post or you need support due to an error, please let me know ^^ Thanks for reading ^^
Source of article from my blog: https://minhphong306.wordpress.com/2018/09/22/huong-dan-su-dung-slack-de-log-error/