From 0c8c603662d84974bfc681509694c27df404e7bf Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 12 Nov 2021 18:38:26 +0330 Subject: restructuring WIP --- makefile | 14 -------------- protobuf/telegram.proto | 22 ---------------------- telebot/Dockerfile | 19 +++++++------------ telebot/docker-entrypoint.sh | 4 +--- telebot/go.mod | 3 ++- telebot/go.sum | 14 ++++++++++++++ telebot/telebot.go | 35 +++++++++++++++++++++++++++++------ 7 files changed, 53 insertions(+), 58 deletions(-) delete mode 100644 makefile delete mode 100644 protobuf/telegram.proto diff --git a/makefile b/makefile deleted file mode 100644 index af3354d..0000000 --- a/makefile +++ /dev/null @@ -1,14 +0,0 @@ -DST_DIR?=./protobuf -SRC_DIR?=./protobuf -PROTOBUF_SRC_LIST:=$(shell find ./protobuf -name '*.proto' ) -ARTIFACT_LIST:=$(patsubst %.proto, %.pb.go, $(shell find ./protobuf -name '*.proto')) - -DEFAULT: default - -default: $(ARTIFACT_LIST) - -$(SRC_DIR)/%.pb.go:$(SRC_DIR)/%.proto - protoc --proto_path=$(SRC_DIR) --go_out=$(DST_DIR) $< - -clean: - - rm $(ARTIFACT_LIST) diff --git a/protobuf/telegram.proto b/protobuf/telegram.proto deleted file mode 100644 index 8a7e6be..0000000 --- a/protobuf/telegram.proto +++ /dev/null @@ -1,22 +0,0 @@ -syntax= "proto3"; -package hived; - -import "google/protobuf/timestamp.proto"; -option go_package ="../protobuf"; - -message NotificationRequest { - reserved 4 to 7; - string notificationText = 1; - int64 channel_id = 2; - google.protobuf.Timestamp request_time = 3; -} - -message NotificationResponse { - reserved 3 to 6; - string error = 2; - bool isOK = 1; -} - -service NotificationService { - rpc Notify(NotificationRequest) returns (NotificationResponse); -} 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 -- cgit v1.2.3