Skip to main content
  1. Posts/

Using Go to post messages on Slack

··257 words·2 mins
Author
Hairizuan Noorazman
Software engineering experiments, implementation notes, and lessons learned.

A sample application to kind of get started with Go.

This application involves pinging a channel on Slack via a webhook. Slack provides a unique URL in order to ping Slack with messages from a script/application.

/*
Example of using Go to ping Slack
This would ping a message by the text message passed via postMessage function

In order to utilize this file, use the command: go run slack_example.go
Else, generate a binary file by running the command: go build slack_example.go
*/
package main

import (
	"fmt"

	"encoding/json"
	"bytes"
	"io/ioutil"

	"net/http"
)

type message struct {
	Text string    `json:"text"`
}

func postMessage(msg string) {
	slackUrl := "https://hooks.slack.com/services/{KEYS}"

	// Create a reader to be used by http.Post
	response := message{Text: msg}
	body, _ := json.Marshal(response)
	byteBody := bytes.NewReader(body)

	res, err := http.Post(slackUrl, "application/json", byteBody)
	if err != nil {
		fmt.Println(err.Error())
		fmt.Println("Try again later.")
	}

	fmt.Println("Status of response:", res.Status)
	fmt.Println("Status code of response:", res.StatusCode)
	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(string(content))
}

func main() {
	fmt.Println("A test application to fire a message into Slack")
	postMessage("init")
}

Some of the concepts introduced here:

  • Introduction to a variety of libraries from std package
  • Introduction to go tool chain, namely go build, go install
  • Some improvements can be made to the above program by allowing it to made into a proper command line tool. This would include accepting of arguments, parsing them and them sending them of to the slack channel. A possible command line library that can be created from this could be the cobra. https://github.com/spf13/cobra

Related

Adding SSO to MCP Grafana Server

··982 words·5 mins
The MCP Grafana server previously relied on static API keys or basic auth for authenticating requests to Grafana. This works fine for local development or single-user setups, but falls apart once you have multiple users who each need their own Grafana permissions. Passing around shared API keys is a security concern and means everyone operates with the same access level regardless of their actual role.

Building a code assessment tool but in Kubernetes

··1933 words·10 mins
Container based security measures Smaller images for code execution platform Not running the container as root Kubernetes related Run the deployment in different namespace Setting up a new Service account in kubernetes Ensuring service account token is not mounted in potentially vulnerable pods Ensuring that the container is started with non-root access Ensuring resource limits are set Set security context Setting network policy Using a stricter seccomp/apparmor profile Tool related Ensure limited logs sniffed Ensure that there is a time limit of code executions Future efforts I had previously attempted to build a code assessment tool in docker. That involves doing the following:

Nginx as API Gateway - focusing on auth_request directive

··1245 words·6 mins
On virtual machine How to “protect” api requests https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/ Mostly is the auth_request directive Microservices are a software architectural style that structures an application as a collection of loosely coupled, independently deployable services. Each service in a microservices architecture represents a specific business capability and communicates with other services through well-defined APIs (Application Programming Interfaces). These services are designed to be small, focused, and can be developed, deployed, and scaled independently. Its a somewhat common architectural pattern that many companies go to when it comes to scaling out their development teams to build out their product.