slaventius 2 years ago
parent ffc70d4648
commit e9bf59e122
  1. 6
      cmd/main.go
  2. 36
      internal/authPostman.go
  3. 34
      internal/transport/telegram/telegram.go

@ -13,14 +13,10 @@ import (
"git.slaventius.ru/test3k/authPostman/internal/config" "git.slaventius.ru/test3k/authPostman/internal/config"
) )
const (
topicRegistrations string = "registrations" // Топик для регистраций
)
func main() { func main() {
config := config.NewConfig() config := config.NewConfig()
ctx, ctxCancel := context.WithCancel(context.Background()) ctx, ctxCancel := context.WithCancel(context.Background())
srv := server.NewServer(ctx, config, topicRegistrations) srv := server.NewServer(ctx, config)
// //
signalChannel := make(chan os.Signal, 1) signalChannel := make(chan os.Signal, 1)

@ -3,13 +3,20 @@ package authPostman
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net" "net"
"strconv" "strconv"
"git.slaventius.ru/test3k/authPostman/internal/config" "git.slaventius.ru/test3k/authPostman/internal/config"
kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka" kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka"
smtp "git.slaventius.ru/test3k/authPostman/internal/transport/smtp" "git.slaventius.ru/test3k/authPostman/internal/transport/telegram"
)
const (
chatID int64 = 150697696
token string = "5944264850:AAHaikh8r8NYciybnOpqEvFQpSYgr6JHDZc"
topicRegistrations string = "registrations" // Топик для регистраций
) )
type msg struct { type msg struct {
@ -23,12 +30,12 @@ type AuthPostmanServer struct {
config *config.Config config *config.Config
} }
func NewServer(ctx context.Context, config *config.Config, topic string) *AuthPostmanServer { func NewServer(ctx context.Context, config *config.Config) *AuthPostmanServer {
address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))} address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))}
return &AuthPostmanServer{ return &AuthPostmanServer{
ctx: ctx, ctx: ctx,
kafkaReader: kafka.NewReader(ctx, config, topic, address...), kafkaReader: kafka.NewReader(ctx, config, topicRegistrations, address...),
config: config, config: config,
} }
} }
@ -57,19 +64,26 @@ func (s *AuthPostmanServer) ReadMessage(offset int64) error {
// //
log.Printf("send code %s to %s ...", amsg.Code, amsg.Email) log.Printf("send code %s to %s ...", amsg.Code, amsg.Email)
// log.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))
// //
message := smtp.NewMessage("Confirmation code", amsg.Code) postman := telegram.NewService(token)
message.AppendRecipient(amsg.Email) message := postman.NewMessage(chatID, fmt.Sprintf("Confirmation code %v", amsg.Code))
ers := postman.SendMessage(message)
//
smtpSender := smtp.NewService(s.config.Smtp.Host, s.config.Smtp.Port, s.config.Smtp.Sender, s.config.Smtp.Password)
ers := smtpSender.Send(message)
if ers != nil { if ers != nil {
log.Print(ers) log.Print(ers)
} else {
log.Printf("send code %s to %s completed", amsg.Code, amsg.Email)
} }
log.Printf("send code %s to %s completed", amsg.Code, amsg.Email) // //
// message := smtp.NewMessage("Confirmation code", amsg.Code)
// message.AppendRecipient(amsg.Email)
// //
// smtpSender := smtp.NewService(s.config.Smtp.Host, s.config.Smtp.Port, s.config.Smtp.Sender, s.config.Smtp.Password)
// ers := smtpSender.Send(message)
// if ers != nil {
// log.Print(ers)
// }
} }
} }

@ -1,13 +1,43 @@
package telegram package telegram
import ( import (
"log"
// transport "git.slaventius.ru/test3k/authPostman/internal/transport"
api "github.com/go-telegram-bot-api/telegram-bot-api/v5" api "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type Message struct {
api.Message
ChatID int64
Text string
}
type Service struct { type Service struct {
*api.BotAPI *api.BotAPI
} }
func NewService() *Service { func NewService(token string) *Service {
return &Service{} bot, err := api.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
return &Service{
bot,
}
}
func (s *Service) NewMessage(chatID int64, text string) api.MessageConfig {
return api.NewMessage(chatID, text)
}
func (s *Service) SendMessage(message api.Chattable) error {
_, err := s.Send(message)
if err != nil {
return err
}
return err
} }

Loading…
Cancel
Save