From 0ccd6f8d73b41adfd658ae95642953d1873f0ac7 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sun, 13 Aug 2023 15:14:10 +0700 Subject: [PATCH] add listener service and message module --- cmd/butler/main.go | 15 ++++++++ internal/modules/messenger/interface.go | 14 ++++++++ .../modules/messenger/telegram/telegram.go | 26 ++++++++++++++ internal/services/interface.go | 7 ++++ internal/services/listener/listener.go | 36 +++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 cmd/butler/main.go create mode 100644 internal/modules/messenger/interface.go create mode 100644 internal/modules/messenger/telegram/telegram.go create mode 100644 internal/services/interface.go create mode 100644 internal/services/listener/listener.go diff --git a/cmd/butler/main.go b/cmd/butler/main.go new file mode 100644 index 0000000..9ba3da9 --- /dev/null +++ b/cmd/butler/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "context" + + "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger/telegram" + "git.3crabs.ru/VLADIMIR/butler/internal/services/listener" +) + +func main() { + messengerTelegram := telegram.NewMessengerTelegram() + listenerService := listener.NewListener(messengerTelegram) + ctx := context.Background() + listenerService.Run(ctx) +} diff --git a/internal/modules/messenger/interface.go b/internal/modules/messenger/interface.go new file mode 100644 index 0000000..99b3dc1 --- /dev/null +++ b/internal/modules/messenger/interface.go @@ -0,0 +1,14 @@ +package messenger + +import "context" + +type Message struct { + ChatID string + UserID string + Text string +} + +type IMessenger interface { + GetMessage(ctx context.Context) (*Message, error) + SendMessage(ctx context.Context, msg *Message) error +} diff --git a/internal/modules/messenger/telegram/telegram.go b/internal/modules/messenger/telegram/telegram.go new file mode 100644 index 0000000..d539b66 --- /dev/null +++ b/internal/modules/messenger/telegram/telegram.go @@ -0,0 +1,26 @@ +package telegram + +import ( + "context" + "fmt" + + "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger" +) + +type messengerTelegram struct{} + +func NewMessengerTelegram() messenger.IMessenger { + return &messengerTelegram{} +} + +func (m *messengerTelegram) GetMessage(ctx context.Context) (*messenger.Message, error) { + return &messenger.Message{ + ChatID: "123", + Text: "hello", + }, nil +} + +func (m *messengerTelegram) SendMessage(ctx context.Context, msg *messenger.Message) error { + fmt.Println(msg.ChatID, msg.Text) + return nil +} diff --git a/internal/services/interface.go b/internal/services/interface.go new file mode 100644 index 0000000..c0b39b7 --- /dev/null +++ b/internal/services/interface.go @@ -0,0 +1,7 @@ +package services + +import "context" + +type IService interface { + Run(ctx context.Context) (error) +} diff --git a/internal/services/listener/listener.go b/internal/services/listener/listener.go new file mode 100644 index 0000000..5c92694 --- /dev/null +++ b/internal/services/listener/listener.go @@ -0,0 +1,36 @@ +package listener + +import ( + "context" + "fmt" + + "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger" + "git.3crabs.ru/VLADIMIR/butler/internal/services" +) + +type listenerService struct { + messenger messenger.IMessenger +} + +func NewListener( + messenger messenger.IMessenger, +) services.IService { + return &listenerService{ + messenger: messenger, + } +} + +func (s *listenerService) Run(ctx context.Context) error { + for { + msg, err := s.messenger.GetMessage(ctx) + if err != nil { + if err == context.Canceled { + return nil + } + return err + } + if err := s.messenger.SendMessage(ctx, msg); err != nil { + fmt.Println(err) + } + } +}