关于slack:项目Slack-消息通知
Slack 作为一款办公类的 App,绝对于其它办公软件来说,我对它提供的性能和用户体验还是比拟认可的。明天简略介绍下如何应用 Slack 频道,实现对我的项目中重要音讯的告诉。 首先,须要在抉择在某一工作空间上 增加 Apps(Slack 容许集体账号退出多个 workspace,相似多租户的概念) 实现 Slack 音讯告诉的形式次要有两种,别离是回调地址和拜访令牌 一、回调地址生成 webhook url增加一个 webhook 到工作空间,这里抉择了要将音讯发送到哪个频道后,会生成一个专属于这个频道的 url 发送音讯 @Slf4j@Componentpublic class SlackService { @Value("${spring.profiles.active}") private String env; @Value("${slack.webhook-url}") private String webhookUrl; @Resource private RestTemplate restTemplate; @Async public void sendMsg(String content) { String param = JsonUtil.object2Json(ImmutableMap.of("text", String.format("[%s] %s", env, content))); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(param, headers); try { restTemplate.postForEntity(webhookUrl, entity, String.class); } catch (Exception e) { log.error("Slack send message error", e); } }}二、拜访令牌设置 scopes这里配置的是 “write.public”,所以能够往该工作空间下的任意频道发送。如果只心愿发送到某一频道则能够抉择其它的 scope,另外在频道中增加该利用(实现形式很多种,过程随便)生成 token这里生成的 token,就是后边通过这个利用往频道发送音讯的凭证发送音讯跟办法一相似,一个是指定 url,一个是携带 token,不再赘述 ...