Skip to content

Commit

Permalink
week3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
stunnyman committed May 26, 2023
1 parent 16362ff commit a54a272
Show file tree
Hide file tree
Showing 9 changed files with 1,116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM quay.io/projectquay/golang:1.20 as builder

WORKDIR /go/src/app
COPY . .
ARG TARGETARCH
RUN make build TARGETARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED

FROM scratch
WORKDIR /
COPY --from=builder /go/src/app/kbot .
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["./kbot", "start"]
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
APP := $(shell basename $(shell git remote get-url origin))
REGISTRY := pavlenkoua
VERSION=$(shell git describe --tags --abbrev=0)-$(shell git rev-parse --short HEAD)
CGO_ENABLED=0

format:
gofmt -s -w ./

lint:
golint

test:
go test -v

get:
go get

linux:
${MAKE} build OS=linux ARCH=${ARCH}

macos:
${MAKE} build OS=darwin ARCH=${ARCH}

windows:
${MAKE} build OS=windows ARCH=${ARCH} CGO_ENABLED=1

build: format get
CGO_ENABLED=${CGO_ENABLED} GOOS=${OS} GOARCH=${ARCH} go build -v -o kbot -ldflags "-X="github.com/pavlenkoUA/gobot/cmd.appVersion=${VERSION}

image:
docker build . -t ${REGISTRY}/${APP}:${VERSION}-${ARCH} --build-arg CGO_ENABLED=${CGO_ENABLED} --build-arg ARCH=${ARCH} --build-arg OS=${OS}

push:
docker push ${REGISTRY}/${APP}:${VERSION}-${ARCH}

clean:
rm -rf kbot
docker rmi ${REGISTRY}/${APP}:${VERSION}-${TARGETARCH}
100 changes: 100 additions & 0 deletions cmd/kbot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"fmt"
"log"
"os"
"time"

"github.com/spf13/cobra"
telebot "gopkg.in/telebot.v3"
)

var (
// TeleToken bot
TeleToken = os.Getenv("TELE_TOKEN")
)

// kbotCmd represents the kbot command
var kbotCmd = &cobra.Command{
Use: "kbot",
Aliases: []string{"start"},
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {

fmt.Printf("kbot %s started", appVersion)
kbot, err := telebot.NewBot(telebot.Settings{
URL: "",
Token: TeleToken,
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
})

if err != nil {
log.Fatalf("Plaese check TELE_TOKEN env variable. %s", err)
return
}

var (
menu = &telebot.ReplyMarkup{ResizeKeyboard: true}
selector = &telebot.ReplyMarkup{}

btnHello = menu.Text("Hello")
btnHelp = menu.Text("ℹ Help")
btnSettings = menu.Text("⚙ Settings")

btnPrev = selector.Data("⬅", "prev", "")
btnNext = selector.Data("➡", "next", "")
)

menu.Reply(
menu.Row(btnHello),
menu.Row(btnHelp),
menu.Row(btnSettings),
)
selector.Inline(
selector.Row(btnPrev, btnNext),
)

kbot.Handle("/start", func(c telebot.Context) error {
return c.Send("Hello! Press button", menu)
})

kbot.Handle("/hello", func(c telebot.Context) error {
return c.Send("Hello!")
})

kbot.Handle(&btnHello, func(c telebot.Context) error {
return c.Send("You pressed button hhelo", menu)
})

kbot.Handle(&btnHelp, func(c telebot.Context) error {
return c.Edit("Here is some help: ...")
})

kbot.Handle(&btnPrev, func(c telebot.Context) error {
return c.Respond()
})

kbot.Start()
},
}

func init() {
rootCmd.AddCommand(kbotCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// kbotCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// kbotCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
43 changes: 43 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kbot",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kbot.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
39 changes: 39 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// appVersion Application Version
var appVersion = "Version"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(appVersion)
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/pavlenkoua/gobot

go 1.20

require (
github.com/spf13/cobra v1.7.0
gopkg.in/telebot.v3 v3.1.3
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
Loading

0 comments on commit a54a272

Please sign in to comment.