[DOCKER] Create a program to post to Slack with GO and make it a container

Introduction

Modify the program on the following site to make it a container.

Post to Slack with Go

Folder structure

Configuration to create Docker container for running on raspberry pi Note that github.com/ashwanthkumar/slack-go-webhook has to install curl I can't run it, so install it

./
├── Dockerfile
└── golang
    └── main.go

Dockerfile


FROM multiarch/ubuntu-core:armhf-bionic As builder
RUN apt update &&\
    apt install -y curl gcc git
RUN  curl -OL https://dl.google.com/go/go1.14.4.linux-armv6l.tar.gz
RUN tar -C /usr/local -xzf go1.14.4.linux-armv6l.tar.gz
RUN rm -rf go1.14.4.linux-armv6l.tar.gz
ENV PATH $PATH:/usr/local/go/bin
RUN go get -u github.com/ashwanthkumar/slack-go-webhook
WORKDIR /app
ADD ./golang/ ./
RUN go build -o app
FROM multiarch/ubuntu-core:armhf-bionic
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y curl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* 
WORKDIR /app
COPY --from=builder /app/app /app
COPY ./golang/conf ./
CMD ["./app"]

The program of Pmain.go is borrowed from the link destination The value of WEBHOOKURL can be obtained from the link below Incoming Webhook

main.go


package main

import (
    "github.com/ashwanthkumar/slack-go-webhook"
    "os"
)

const (
    WEBHOOKURL = "https://hooks.slack.com/services/XXXX" //Webhook URL
    CHANNEL    = "dev"  //Destination channel
    USERNAME   = "GoBot" //Display User name
)

func main() {
    PostSlack("HelloWorld!!")
}

func PostSlack(msg string) {
    field1 := slack.Field{Title: "Message", Value: msg}
    field2 := slack.Field{Title: "AnythingKey", Value: "AnythingValue"}

    attachment := slack.Attachment{}
    attachment.AddField(field1).AddField(field2)
    color := "good"
    attachment.Color = &color
    payload := slack.Payload{
        Username:    USERNAME,
        Channel:     CHANNEL,
        Attachments: []slack.Attachment{attachment},
    }
    err := slack.Send(WEBHOOKURL, "", payload)
    if err != nil {
        os.Exit(1)
    }
}

Docker execution

You can post a string to Slack by executing the following command tail.

docker build -t slackpost .
docker run --rm slackpost

As an application of the container, if you add the following code You can change the posting destination from the environment variable.

type SlackSetUp struct {
	WebUrl    string
	Channel   string
	UserName  string
}

func EnvConfRead() SlackSetUp {
	var tmp SlackSetUp
	if str := os.Getenv("SLACK_URL"); str != "" {
		tmp.WebUrl = str
	}
	if str := os.Getenv("SLACK_CHANNEL"); str != "" {
		tmp.Channel = str
	}
	if str := os.Getenv("SLACK_USERNAME"); str != "" {
		tmp.UserName = str
	}
	return tmp
}

Recommended Posts

Create a program to post to Slack with GO and make it a container
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a Java (Maven) project with VS Code and develop it on a Docker container
Make Docker confusing with Pokemon and make it easier to attach
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to make a Java container
How to create a server executable JAR and WAR with Spring gradle
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
How to make an app with a plugin mechanism [C # and Java]
[Rails] rails new to create a database with PostgreSQL
Create assert_equal to make it easy to write tests
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to create a shopping site administrator function / screen with Java and Spring
Until you create a Spring Boot project in Intellij and push it to Github
How to create a header or footer once and use it on another page
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
Run Scala with GraalVM & make it a native image
How to make a factory with a model with polymorphic association
Make it possible to search Japanese sentences with ElasticSearch
Create a Docker container to convert EPS to PGF source
Program to determine if it is a leap year
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
How to develop in a container with --privileged and / sbin / init passed in VSCode Remote Containers
Click the [rails] button to create a random alphanumeric password and enter it in the password field
Create a blog with Jekyll and GitHub Pages @ Theme setting
Maybe it works! Create an image with Docker and share it!
I tried to create a java8 development environment with Chocolatey
Assign a Java8 lambda expression to a variable and reuse it
Tutorial to create a blog with Rails for beginners Part 1
How to create a lightweight container image for Java apps
[Rails] I tried to create a mini app with FullCalendar
[Beginner] Try to make a simple RPG game with Java ①
Get video information from Nikorepo and throw it to Slack
Create a blog with Jekyll and GitHub Pages @ Initial Settings
Learning Ruby with AtCoder 13 How to make a two-dimensional array
A series of steps to create portfolio deliverables with Rails
Build a Node-RED environment with Docker to move and understand
Tutorial to create a blog with Rails for beginners Part 2
Create Rails5 and postgresql environment with Docker and make pgadmin available
I tried to create a padrino development environment with Docker
How to create and launch a Dockerfile for Payara Micro
Tutorial to create a blog with Rails for beginners Part 0
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry
Create a playground with Xcode 12
[Java] Make it a constant
To write a user-oriented program (1)
How to create a method
Build Metabase with Docker on Lightsail and make it https with nginx
I want to make a button with a line break with link_to [Note]
How to make a jar file with no dependencies in Maven
Create a JVM for app distribution with JDK9 modules and jlink
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Create a high-performance enum with fields and methods like Java with JavaScript
Try to make a cross-platform application with JRuby (jar file generation)
How to read a file and treat it as standard input
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Make a C compiler to use with Rust x CLion with Docker
I wrote a route search program in TDD and refactored it