Modify the program on the following site to make it a container.
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)
}
}
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