diff --git a/cmd/butler/main.go b/cmd/butler/main.go index 9ba3da9..9bf488c 100644 --- a/cmd/butler/main.go +++ b/cmd/butler/main.go @@ -2,13 +2,18 @@ package main import ( "context" + "os" "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger/telegram" "git.3crabs.ru/VLADIMIR/butler/internal/services/listener" ) func main() { - messengerTelegram := telegram.NewMessengerTelegram() + args := os.Args[1:] // tg token + messengerTelegram, err := telegram.NewMessengerTelegram(args[0]) + if err != nil { + panic(err) + } listenerService := listener.NewListener(messengerTelegram) ctx := context.Background() listenerService.Run(ctx) diff --git a/go.mod b/go.mod index 2713b94..3ff7a91 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.3crabs.ru/VLADIMIR/butler go 1.20 + +require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..db8e45c --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/internal/modules/messenger/telegram/telegram.go b/internal/modules/messenger/telegram/telegram.go index d539b66..ca39e7b 100644 --- a/internal/modules/messenger/telegram/telegram.go +++ b/internal/modules/messenger/telegram/telegram.go @@ -3,24 +3,55 @@ package telegram import ( "context" "fmt" + "log" + "strconv" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger" ) -type messengerTelegram struct{} - -func NewMessengerTelegram() messenger.IMessenger { - return &messengerTelegram{} +type messengerTelegram struct { + bot *tgbotapi.BotAPI + updates tgbotapi.UpdatesChannel } -func (m *messengerTelegram) GetMessage(ctx context.Context) (*messenger.Message, error) { - return &messenger.Message{ - ChatID: "123", - Text: "hello", +func NewMessengerTelegram(token string) (messenger.IMessenger, error) { + bot, err := tgbotapi.NewBotAPI(token) + if err != nil { + return nil, err + } + log.Printf("Authorized on account %s", bot.Self.UserName) + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates := bot.GetUpdatesChan(u) + return &messengerTelegram{ + bot: bot, + updates: updates, }, nil } +func (m *messengerTelegram) GetMessage(ctx context.Context) (*messenger.Message, error) { + tgMsg := <-m.updates + msg := &messenger.Message{ + ChatID: fmt.Sprintf("%d", tgMsg.Message.Chat.ID), + UserID: tgMsg.Message.From.UserName, + Text: tgMsg.Message.Text, + } + log.Println("get:", msg) + return msg, nil +} + func (m *messengerTelegram) SendMessage(ctx context.Context, msg *messenger.Message) error { - fmt.Println(msg.ChatID, msg.Text) + chatID, err := strconv.ParseInt(msg.ChatID, 10, 64) + if err != nil { + return err + } + tgMsg := tgbotapi.NewMessage(chatID, msg.Text) + if _, err = m.bot.Send(tgMsg); err != nil { + return err + } + log.Println("send:", msg) return nil }