diff options
author | terminaldweller <thabogre@gmail.com> | 2021-11-12 15:08:26 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2021-11-12 15:08:26 +0000 |
commit | 0c8c603662d84974bfc681509694c27df404e7bf (patch) | |
tree | b70d9d2860ddf5a2474f3ee10f2f8033deeb0e47 /telebot/telebot.go | |
parent | removed travis (diff) | |
download | hived-0c8c603662d84974bfc681509694c27df404e7bf.tar.gz hived-0c8c603662d84974bfc681509694c27df404e7bf.zip |
restructuring WIP
Diffstat (limited to 'telebot/telebot.go')
-rw-r--r-- | telebot/telebot.go | 35 |
1 files changed, 29 insertions, 6 deletions
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 |