This commit is contained in:
Владимир Фёдоров 2023-08-13 15:36:27 +07:00
parent 0ccd6f8d73
commit 116f4041fa
4 changed files with 50 additions and 10 deletions

View File

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

2
go.mod
View File

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

2
go.sum Normal file
View File

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

View File

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