diff options
Diffstat (limited to 'telebot')
-rw-r--r-- | telebot/Dockerfile | 19 | ||||
-rwxr-xr-x | telebot/docker-entrypoint.sh | 4 | ||||
-rw-r--r-- | telebot/go.mod | 3 | ||||
-rw-r--r-- | telebot/go.sum | 14 | ||||
-rw-r--r-- | telebot/telebot.go | 35 |
5 files changed, 53 insertions, 22 deletions
diff --git a/telebot/Dockerfile b/telebot/Dockerfile index 0a9e3e2..894b02e 100644 --- a/telebot/Dockerfile +++ b/telebot/Dockerfile @@ -1,18 +1,13 @@ FROM alpine:3.13 as builder RUN apk update && apk upgrade RUN apk add go git -COPY go.* /hived/ -RUN cd /hived && go mod download -COPY *.go /hived/ -RUN cd /hived && go build - -FROM node:lts-alpine3.13 AS certbuilder -RUN apk add openssl -WORKDIR /certs -RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert +COPY go.* /telebot/ +RUN cd /telebot && go mod download +COPY *.go /telebot/ +RUN cd /telebot && go build FROM gcr.io/distroless/static-debian10 COPY --from=certbuilder /cert/ /cert/ -COPY --from=builder /hived/hived /hived/ -COPY ./docker-entrypoint.sh /hived/ -ENTRYPOINT ["/hived/docker-entrypoint.sh"] +COPY --from=builder /telebot/telebot /telebot/ +COPY ./docker-entrypoint.sh /telebot/ +ENTRYPOINT ["/telebot/docker-entrypoint.sh"] diff --git a/telebot/docker-entrypoint.sh b/telebot/docker-entrypoint.sh index 932c9c9..c2a6d0a 100755 --- a/telebot/docker-entrypoint.sh +++ b/telebot/docker-entrypoint.sh @@ -2,7 +2,5 @@ set -ex export $(cat /run/secrets/tg_bot_token) -export $(cat /run/secrets/ch_api_key) -export $(cat /run/secrets/ch_api_secret) -"/hived/hived" "$@" +"/telebot/telebot" "$@" diff --git a/telebot/go.mod b/telebot/go.mod index de7cf3c..0907cbb 100644 --- a/telebot/go.mod +++ b/telebot/go.mod @@ -1,10 +1,11 @@ module telebot -go 1.17 +go 1.15 require ( github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible github.com/gorilla/mux v1.8.0 github.com/rs/zerolog v1.26.0 github.com/technoweenie/multipartstreamer v1.0.1 // indirect + github.com/terminaldweller/grpc v1.0.2 ) diff --git a/telebot/go.sum b/telebot/go.sum index ca57a1e..efb0f7a 100644 --- a/telebot/go.sum +++ b/telebot/go.sum @@ -2,6 +2,11 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -10,6 +15,8 @@ github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE= github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= +github.com/terminaldweller/grpc v1.0.2 h1:8p+EEYLESQpcVw9QACt0kGeW6TszoLGABBPad6MieZg= +github.com/terminaldweller/grpc v1.0.2/go.mod h1:ntkouSulgDH7yfcsXXAgp7LwUOBKIEn0coPecMFz8Qc= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -32,4 +39,11 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= diff --git a/telebot/telebot.go b/telebot/telebot.go index f6a3bf7..04bbf1c 100644 --- a/telebot/telebot.go +++ b/telebot/telebot.go @@ -12,14 +12,23 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/gorilla/mux" "github.com/rs/zerolog/log" + pb "github.com/terminaldweller/grpc/telebot/v1" ) -var flagPort = flag.String("port", "8000", "determined the port the sercice runs on") +var ( + flagPort = flag.String("port", "8000", "determined the port the sercice runs on") -// FIXME-the client should provide the channel ID -var botChannelID = flag.Int64("botchannelid", 146328407, "determines the channel id the telgram bot should send messages to") + // FIXME-the client should provide the channel ID + botChannelID = flag.Int64("botchannelid", 146328407, "determines the channel id the telgram bot should send messages to") +) -const TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN" +const ( + TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN" + SERVER_DEPLOYMENT_TYPE = "SERVER_DEPLOYMENT_TYPE" +) + +type NotificationService struct { +} func getTGBot() *tgbotapi.BotAPI { token := os.Getenv(TELEGRAM_BOT_TOKEN_ENV_VAR) @@ -30,12 +39,26 @@ func getTGBot() *tgbotapi.BotAPI { return bot } -func sendMessage(bot *tgbotapi.BotAPI, msgText string) error { - msg := tgbotapi.NewMessage(*botChannelID, msgText) +func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error { + msg := tgbotapi.NewMessage(channelID, msgText) bot.Send(msg) return nil } +func (s *NotificationService) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) { + var err error + tgbotapi := getTGBot() + if NotificationRequest.ChannelId == 0 { + err = sendMessage(tgbotapi, NotificationRequest.NotificationText, *botChannelID) + } else { + err = sendMessage(tgbotapi, NotificationRequest.NotificationText, NotificationRequest.ChannelId) + } + if err != nil { + return &pb.NotificationResponse{Error: err.Error(), IsOK: false}, err + } + return &pb.NotificationResponse{Error: "", IsOK: true}, nil +} + func healthHandler(w http.ResponseWriter, r *http.Request) { var telebotError string IsTelebotOk := true |