From b2cd7c773af16f0ce9cc1e2b33ca10f2c6be834e Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 19 Jun 2024 13:58:01 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20FillMess?= =?UTF-8?q?ageTelegramFromMessage()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 15 +++ micro/microfunctions_test.go | 32 +++++++ telegram_client/telegram_client.go | 120 ++++++++++++++++++++++++ telegram_client/telegram_client_test.go | 20 +++- 4 files changed, 186 insertions(+), 1 deletion(-) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 4a9a37dd..62bccd6c 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -931,3 +931,18 @@ func SortMapStringInt_Desc(values map[string]int) []string { } return ranked } + +// IsNilInterface - проверка интерфейса на nil +func IsNilInterface(i any) bool { + iv := reflect.ValueOf(i) + if !iv.IsValid() { + return true + } + + switch iv.Kind() { + case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Func, reflect.Interface: + return iv.IsNil() + default: + return false + } +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 6ec993e6..6f675a19 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -797,3 +797,35 @@ func TestSortkMapStringInt(t *testing.T) { }) } } + +func TestIsNilInterface(t *testing.T) { + // Testing for a nil pointer interface + var ptr *int + if !IsNilInterface(ptr) { + t.Error("Expected true for nil pointer interface") + } + + // Testing for a nil slice interface + var slice []int + if !IsNilInterface(slice) { + t.Error("Expected true for nil slice interface") + } + + // Testing for a non-nil map interface + m := make(map[string]int) + if IsNilInterface(m) { + t.Error("Expected false for non-nil map interface") + } + + // Testing for a nil function interface + var fn func() + if !IsNilInterface(fn) { + t.Error("Expected true for nil function interface") + } + + // Testing for a nil interface + var i interface{} + if !IsNilInterface(i) { + t.Error("Expected true for nil interface") + } +} diff --git a/telegram_client/telegram_client.go b/telegram_client/telegram_client.go index 446baeaf..52d1cb9a 100644 --- a/telegram_client/telegram_client.go +++ b/telegram_client/telegram_client.go @@ -36,6 +36,9 @@ var filenameSession string // Client - клиент соединения мессенджера Телеграм var Client *telegram.Client +// UserSelf - собственный юзер в Телеграм +var UserSelf *tg.User + // lastSendTime - время последней отправки сообщения и мьютекс var lastSendTime = lastSendTimeMutex{} @@ -73,6 +76,34 @@ type SettingsINI struct { TELEGRAM_PHONE_SEND_TEST string } +// MessageTelegram - сообщение из Telegram сокращённо +type MessageTelegram struct { + Text string + FromID int64 + ChatID int64 + IsFromMe bool + MediaType string + //NameTo string + IsGroup bool + ID int + TimeSent time.Time +} + +// String - возвращает строку из структуры +func (m MessageTelegram) String() string { + Otvet := "" + + Otvet = Otvet + fmt.Sprint("Text: ", m.Text, "\n") + Otvet = Otvet + fmt.Sprint("MediaType: ", m.MediaType, "\n") + Otvet = Otvet + fmt.Sprint("FromID: ", m.FromID, "\n") + Otvet = Otvet + fmt.Sprint("IsFromMe: ", m.IsFromMe, "\n") + Otvet = Otvet + fmt.Sprint("IsGroup: ", m.IsGroup, "\n") + Otvet = Otvet + fmt.Sprint("ID: ", m.ID, "\n") + Otvet = Otvet + fmt.Sprint("TimeSent: ", m.TimeSent, "\n") + + return Otvet +} + // SendMessage - отправка сообщения в мессенджер Телеграм // возвращает: // id = id отправленного сообщения в telegram @@ -440,6 +471,13 @@ func ConnectTelegram() error { return err } + //заполним UserSelf + UserSelf, err = Client.Self(ctx) + if err != nil { + return err + } + + // log.Info("Telegram connected: ", Settings.TELEGRAM_PHONE_FROM) return nil @@ -636,3 +674,85 @@ func FillSettings() { } } + +// FillMessageTelegramFromMessage - заполнение струткру MessageTelegram из сообщения от Telegram +func FillMessageTelegramFromMessage(m *tg.Message) MessageTelegram { + Otvet := MessageTelegram{} + + //не подключен + if Client == nil { + return Otvet + } + + ////не подключен + //if stopTelegramFunc == nil { + // return Otvet + //} + + //не подключен + if UserSelf == nil { + return Otvet + } + + // + //ctxMain := contextmain.GetContext() + //ctx, cancel_func := context.WithTimeout(ctxMain, 60*time.Second) //60 + //defer cancel_func() + IsGroup := false + + Otvet.Text = m.Message + Otvet.ID = m.ID + Otvet.MediaType = m.TypeName() + TimeInt := m.GetDate() + Otvet.TimeSent = time.UnixMilli(int64(TimeInt * 1000)) + var ChatID int64 + + if m.PeerID != nil && micro.IsNilInterface(m.PeerID) == false { + switch v := m.PeerID.(type) { + case *tg.PeerUser: + ChatID = v.UserID + case *tg.PeerChat: + { + ChatID = v.ChatID + IsGroup = true + } + case *tg.PeerChannel: + { + ChatID = v.ChannelID + IsGroup = true + } + default: + { + IsGroup = true + } + } + } + Otvet.ChatID = ChatID + + MyID := UserSelf.ID + var SenderID int64 + + IsFromMe := false + if m.FromID != nil && micro.IsNilInterface(m.FromID) == false { + switch v := m.FromID.(type) { + case *tg.PeerUser: + { + SenderID = v.UserID + } + //case *tg.PeerChat: // peerChat#36c6019a + //case *tg.PeerChannel: // peerChannel#a2a5371e + default: + } + } else { + IsFromMe = true + } + Otvet.IsGroup = IsGroup //m.GroupedID != 0 + + if MyID == SenderID { + IsFromMe = true + } + Otvet.IsFromMe = IsFromMe + Otvet.FromID = SenderID + + return Otvet +} diff --git a/telegram_client/telegram_client_test.go b/telegram_client/telegram_client_test.go index 88f4f42a..b41dbaa2 100644 --- a/telegram_client/telegram_client_test.go +++ b/telegram_client/telegram_client_test.go @@ -53,7 +53,7 @@ func TestSendMessage(t *testing.T) { text := "Test www.ya.ru " + time.Now().String() id, err := SendMessage(Settings.TELEGRAM_PHONE_SEND_TEST, text) - log.Info("Message id: ", id) + t.Log("Message id: ", id) if err != nil { t.Error("telegramclient_test.TestSendMessage() SendMessage() error: ", err) } @@ -219,3 +219,21 @@ func TestStartTelegram(t *testing.T) { contextmain.CancelContext() contextmain.GetNewContext() } + +func TestFillMessageTelegramFromMessage(t *testing.T) { + config_main.LoadEnvTest() + CreateTelegramClient(nil) + + mess := &tg.Message{ + Message: "Test Message", + ID: 123, + } + + FillMessageTelegramFromMessage(mess) + + // Check if Otvet.Text is correctly assigned from m.Message + //if result.Text != "Test Message" { + // t.Errorf("Expected Text to be 'Test Message', but got %s", result.Text) + //} + +}