From 99a93f106797a084754ab5b93f44a7f27eb7d180 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 12 Nov 2021 12:01:15 +0330 Subject: restructured the repo-WIP --- telebot/telebot.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 telebot/telebot.go (limited to 'telebot/telebot.go') diff --git a/telebot/telebot.go b/telebot/telebot.go new file mode 100644 index 0000000..f6a3bf7 --- /dev/null +++ b/telebot/telebot.go @@ -0,0 +1,92 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "net/http" + "os" + "os/signal" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/gorilla/mux" + "github.com/rs/zerolog/log" +) + +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") + +const TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN" + +func getTGBot() *tgbotapi.BotAPI { + token := os.Getenv(TELEGRAM_BOT_TOKEN_ENV_VAR) + bot, err := tgbotapi.NewBotAPI(token[1 : len(token)-1]) + if err != nil { + log.Error().Err(err) + } + return bot +} + +func sendMessage(bot *tgbotapi.BotAPI, msgText string) error { + msg := tgbotapi.NewMessage(*botChannelID, msgText) + bot.Send(msg) + return nil +} + +func healthHandler(w http.ResponseWriter, r *http.Request) { + var telebotError string + IsTelebotOk := true + + w.Header().Add("Content-Type", "application/json") + if r.Method != "GET" { + http.Error(w, "Method is not supported.", http.StatusNotFound) + } + + w.WriteHeader(http.StatusOK) + + json.NewEncoder(w).Encode(struct { + IsHivedOk bool `json:"isTelebotOK"` + TelebotError string `json:"telebotError"` + }{IsHivedOk: IsTelebotOk, TelebotError: telebotError}) +} + +func msgHandler(w http.ResponseWriter, r *http.Request) { + +} + +func startServer(gracefulWait time.Duration) { + r := mux.NewRouter() + srv := &http.Server{ + Addr: "0.0.0.0:" + *flagPort, + WriteTimeout: time.Second * 15, + ReadTimeout: time.Second * 15, + Handler: r, + } + r.HandleFunc("/health", healthHandler) + r.HandleFunc("/msg", msgHandler) + + go func() { + if err := srv.ListenAndServe(); err != nil { + log.Fatal().Err(err) + } + }() + + c := make(chan os.Signal, 1) + + signal.Notify(c, os.Interrupt) + <-c + ctx, cancel := context.WithTimeout(context.Background(), gracefulWait) + defer cancel() + srv.Shutdown(ctx) + log.Info().Msg("gracefully shut down the server") +} + +func main() { + var gracefulWait time.Duration + flag.DurationVar(&gracefulWait, "gracefulwait", time.Second*15, "the duration to wait during the graceful shutdown") + flag.Parse() + startServer(gracefulWait) +} -- cgit v1.2.3 From 0c8c603662d84974bfc681509694c27df404e7bf Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 12 Nov 2021 18:38:26 +0330 Subject: restructuring WIP --- telebot/telebot.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'telebot/telebot.go') 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 From a963b5272b30978927d5b3a6dc1a39abca38548d Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 13 Dec 2021 22:27:02 +0330 Subject: grpc for telebot. untested. [WIP] --- telebot/telebot.go | 70 ++++++++++++++---------------------------------------- 1 file changed, 18 insertions(+), 52 deletions(-) (limited to 'telebot/telebot.go') diff --git a/telebot/telebot.go b/telebot/telebot.go index 04bbf1c..0676ce9 100644 --- a/telebot/telebot.go +++ b/telebot/telebot.go @@ -2,17 +2,16 @@ package main import ( "context" - "encoding/json" "flag" - "net/http" + "fmt" + "net" "os" - "os/signal" - "time" + "strconv" 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" + "google.golang.org/grpc" ) var ( @@ -27,7 +26,8 @@ const ( SERVER_DEPLOYMENT_TYPE = "SERVER_DEPLOYMENT_TYPE" ) -type NotificationService struct { +type server struct { + pb.UnimplementedNotificationServiceServer } func getTGBot() *tgbotapi.BotAPI { @@ -45,7 +45,7 @@ func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error { return nil } -func (s *NotificationService) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) { +func (s *server) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) { var err error tgbotapi := getTGBot() if NotificationRequest.ChannelId == 0 { @@ -59,57 +59,23 @@ func (s *NotificationService) Notify(ctx context.Context, NotificationRequest *p return &pb.NotificationResponse{Error: "", IsOK: true}, nil } -func healthHandler(w http.ResponseWriter, r *http.Request) { - var telebotError string - IsTelebotOk := true - - w.Header().Add("Content-Type", "application/json") - if r.Method != "GET" { - http.Error(w, "Method is not supported.", http.StatusNotFound) +func startServer(port uint16) { + listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port)) + if err != nil { + log.Fatal().Err(err) } - w.WriteHeader(http.StatusOK) - - json.NewEncoder(w).Encode(struct { - IsHivedOk bool `json:"isTelebotOK"` - TelebotError string `json:"telebotError"` - }{IsHivedOk: IsTelebotOk, TelebotError: telebotError}) -} - -func msgHandler(w http.ResponseWriter, r *http.Request) { + var opts []grpc.ServerOption -} - -func startServer(gracefulWait time.Duration) { - r := mux.NewRouter() - srv := &http.Server{ - Addr: "0.0.0.0:" + *flagPort, - WriteTimeout: time.Second * 15, - ReadTimeout: time.Second * 15, - Handler: r, + grpcServer := grpc.NewServer(opts...) + pb.RegisterNotificationServiceServer(grpcServer, &server{}) + if err := grpcServer.Serve(listener); err != nil { + log.Fatal().Err(err) } - r.HandleFunc("/health", healthHandler) - r.HandleFunc("/msg", msgHandler) - - go func() { - if err := srv.ListenAndServe(); err != nil { - log.Fatal().Err(err) - } - }() - - c := make(chan os.Signal, 1) - - signal.Notify(c, os.Interrupt) - <-c - ctx, cancel := context.WithTimeout(context.Background(), gracefulWait) - defer cancel() - srv.Shutdown(ctx) - log.Info().Msg("gracefully shut down the server") } func main() { - var gracefulWait time.Duration - flag.DurationVar(&gracefulWait, "gracefulwait", time.Second*15, "the duration to wait during the graceful shutdown") flag.Parse() - startServer(gracefulWait) + port, _ := strconv.Atoi(*flagPort) + startServer(uint16(port)) } -- cgit v1.2.3