跳到主要內容

Slack 新增 Bot App


Create New App

1. Create New App

填入 App Name 及 選擇 workspace

2. 維護 Base Information

維護 App Name & Short description & App icon & background color

3. 維護 Oauth & Permissions

Add an Oauth Scope
incoming-webhook 

channels:read
This scope lets an app "View basic information about public channels in the workspace"

chat:write:bot
This scope lets an app "Send messages as your slack app"

4. Install App into your workspace

Code Example

Send messages example

DotNet Code Send Message 2 Slack



private void send2stalck(string path,string query,string header,string body,string message)
{
    Dictionary dictQuery = new Dictionary();
    dictQuery.Add("channel", @"slack channel");
    dictQuery.Add("token", @"slack token");
    dictQuery.Add("text", message);
    JArray jaFields = new JArray();
    var joEnv = new JObject() { { "title", "env" }, { "value", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") }, { "short", true } };
    var joPath = new JObject() { { "title", "path" }, { "value", path }, { "short", true } };
    var joTimestamp = new JObject() { { "title", "timestamp" }, { "value", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "short", true } };
    var joQuery = new JObject() { { "title", "query parameter" }, { "value", query }, { "short", true } };
    var joHeader = new JObject() { { "title", "header parameter" }, { "value", header }, { "short", false } };
    var joBody = new JObject() { { "title", "body" }, { "value", body }, { "short", false } };
    var joMessage = new JObject() { { "title", "message" }, { "value", message }, { "short", false } };
    jaFields.Add(joEnv);
    jaFields.Add(joPath);
    jaFields.Add(joTimestamp);
    jaFields.Add(joQuery);
    jaFields.Add(joHeader);
    jaFields.Add(joBody);
    jaFields.Add(joMessage);
    JObject joField = new JObject() { { "fields", jaFields } };
    JArray ja = new JArray();
    ja.Add(joField);
    string strAttachments = JsonConvert.SerializeObject(ja);
    dictQuery.Add("attachments", WebUtility.UrlEncode(strAttachments));
    httpReqService service = new httpReqService().WithSlack();
    JObject joReturn = service.sendRequest("/chat.postMessage", "GET", dictQuery, null, null);
}
        

留言

這個網誌中的熱門文章

FluentD 實作 Error Log

FluentD 實作 Error Log 本篇將介紹使用 DotNet 專案 log4net 套件,紀錄的 log 針對 Error Level 的訊息透過FluentD 提取出來 在紀錄中 增加 trace ID 設入 MongoDB , 及加入 Slack 通知 延伸閱讀 FluentD 參數說明 FluentD 實作 Nginx Access Log FluentD 實作 Nginx Access Log 補充 log4net <?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <appender name="All" type="log4net.Appender.RollingFileAppender"> <file value="/var/log/my.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <datePattern value="yyyy-MM-dd" /> <maximumFileSize value="5MB" /> <maxSizeRollBackups value="10" /> <staticLogFileName value="true" /> <PreserveLogFileNameExtension value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [...

Grafana Dashboard 建立

建立自己的 Dashboard # 由於 intelligent sense 相當不錯,輸入關鍵字他會帶出 metric label # 另外可參考 https://prometheus.io/docs/prometheus/latest/querying/basics/ Prometheus Query # 或是直接拿其他已建立的Dashboard 可複製到新的 Dashboard ex: node_memory_MemTotal_bytes # 取伺服器記憶體容量資料 # 過濾條件在{}加入 ex: node_memory_MemTotal_bytes{instance="${server 1}:9100"} # 要取特定伺服器資料 # Setting 中設定 Variables ex: node_memory_MemTotal_bytes{instance=~"$node"} # 變數名稱 node 建立 Alert .Visualization 必須是Graph

FluentD 參數說明

FluentD 高效、統一的日誌收集器 延伸閱讀 FluentD 實作 Nginx Access Log FluentD 實作 Nginx Access Log 補充 FluentD 安裝 Dockerfile FROM fluent/fluentd:v1.8.1-1.0 # Use root account to use apk USER root # below RUN includes plugin as examples elasticsearch is not required # you may customize including plugins as you wish RUN apk add --no-cache --update --virtual .build-deps \ sudo build-base ruby-dev \ && apk add mariadb-dev \ && sudo gem install fluent-plugin-elasticsearch \ && sudo gem install fluent-plugin-mongo \ && sudo gem install fluent-plugin-sql \ && sudo gem install mysql2 -v 0.5.2 \ && sudo gem sources --clear-all \ && apk del .build-deps \ && rm -rf /home/fluent/.gem/ruby/2.5.0/cache/*.gem VOLUME ["/fluentd/etc","/fluentd/log","/var/log"] docker-compose.yml version: '3' services: fluentd: build: context: . dockerfile: ./Dockerfile image: my/flue...