add listener service and message module

This commit is contained in:
Владимир Фёдоров 2023-08-13 15:14:10 +07:00
parent 1871fc7c92
commit 0ccd6f8d73
5 changed files with 98 additions and 0 deletions

15
cmd/butler/main.go Normal file
View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1,7 @@
package services
import "context"
type IService interface {
Run(ctx context.Context) (error)
}

View File

@ -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)
}
}
}