Slack - henk52/knowledgesharing GitHub Wiki

Slack

Introduction

Purpose

Recipies

Find messages where alpha mentioned beta

  • ctlr-k
  • @alpha from:@beta

slack applications

creating a slack-app in slack

Webhooks

Verify slack webhook

The signature is created by hashing the request body with the SHA-256 function, and combining it with an HMAC signing secret. The resulting signature is unique to each request and doesn't contain any secret information, keeping your app secure.

function encodeFormData(data) {
  const encodedData = Object.keys(data)
    .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
    .join('&')
    .replaceAll("%20", "+") // Slack does not encode "+" signs
    .replaceAll("*", "%2A") // Slack encodes "*" signs
    .replaceAll("~", "%7E"); // Slack encodes "~" signs
    
  return encodedData;
}

function buildSignatureBaseString(requestJson) {
  const version = "v0"; // Slack Webhook version (Always v0 for the moment)
  
  const timestamp = requestJson.headers["x-slack-request-timestamp"];
  
  const body = requestJson.body;
  const encodedBody = encodeFormData(body);
  
  const signatureBaseString = `${version}:${timestamp}:${encodedBody}`;

  return signatureBaseString;
}

# TODO this only gets the first message, what about the rest of the messages?
const requestJson = $input.first().json;

const signatureBaseString = buildSignatureBaseString(requestJson);

const requestSignature = requestJson.headers["x-slack-signature"];

console.log({
    signatureBaseString,
    requestSignature
  });
return {
  json: {
    signatureBaseString,
    requestSignature
  },
  pairedItem: 0
}

Slack messaging

  • Messaging

  • conversation -

  • thread -

    • Before a message has any replies, we call it an unthreaded message.
    • Once a message has replies, it becomes a parent message.
    • Any child messages of that parent message are called threaded replies.
    • The whole bundle of parent message and replies is referred to as a thread
    • Each of the messages within a thread, whether parent or reply, is a threaded message.
  • if a user invokes one of your app's slash commands that performs some action on a third-party service, an ephemeral message might be the most suitable way to inform that user of the success of the action. https://docs.slack.dev/messaging/

TODO

  • TODO write a local js script for sending messages to a channel
  • TODO write code for receiving gitlab webhook
  • TODO how to host a lambda locally
  • TODO how to handle data handling like in n8n
    • record the data?
  • TODO how to find MR messages to add various bump operations

Connecting to Slack from a program

  • Preferred method via WebSocket

Setting up the slack app for websocket connection

  • Pre condition, the slack app has been created

Go to api.slack.com

  • App token
    • From: Basic information
    • Generate the app leve token, with connections:write scope.
    • SLACK_APP_TOKEN=xapp-*
  • Signing secret from the Slack app config.
    • From: app basic information.
    • SLACK_SIGNING_SECRET=nnn...
  • slack bot token
    • Slack bot token (starts with xoxb-), used to call the Slack Web API.
    • From: OAuth & Permission
    • SLACK_BOT_TOKEN=xoxb-*
  • enable websocket
    • From: Socket Mde
    • Enable socket mode
  • Comma-separated list of Slack channel IDs to monitor for alerts.
    • From: channel in slack app
    • ALERT_CHANNEL_IDS=