mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-26 23:10:42 +02:00
сделал FillMessageTelegramFromMessage()
This commit is contained in:
@@ -931,3 +931,18 @@ func SortMapStringInt_Desc(values map[string]int) []string {
|
|||||||
}
|
}
|
||||||
return ranked
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ var filenameSession string
|
|||||||
// Client - клиент соединения мессенджера Телеграм
|
// Client - клиент соединения мессенджера Телеграм
|
||||||
var Client *telegram.Client
|
var Client *telegram.Client
|
||||||
|
|
||||||
|
// UserSelf - собственный юзер в Телеграм
|
||||||
|
var UserSelf *tg.User
|
||||||
|
|
||||||
// lastSendTime - время последней отправки сообщения и мьютекс
|
// lastSendTime - время последней отправки сообщения и мьютекс
|
||||||
var lastSendTime = lastSendTimeMutex{}
|
var lastSendTime = lastSendTimeMutex{}
|
||||||
|
|
||||||
@@ -73,6 +76,34 @@ type SettingsINI struct {
|
|||||||
TELEGRAM_PHONE_SEND_TEST string
|
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 - отправка сообщения в мессенджер Телеграм
|
// SendMessage - отправка сообщения в мессенджер Телеграм
|
||||||
// возвращает:
|
// возвращает:
|
||||||
// id = id отправленного сообщения в telegram
|
// id = id отправленного сообщения в telegram
|
||||||
@@ -440,6 +471,13 @@ func ConnectTelegram() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//заполним UserSelf
|
||||||
|
UserSelf, err = Client.Self(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
log.Info("Telegram connected: ", Settings.TELEGRAM_PHONE_FROM)
|
log.Info("Telegram connected: ", Settings.TELEGRAM_PHONE_FROM)
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func TestSendMessage(t *testing.T) {
|
|||||||
|
|
||||||
text := "Test www.ya.ru " + time.Now().String()
|
text := "Test www.ya.ru " + time.Now().String()
|
||||||
id, err := SendMessage(Settings.TELEGRAM_PHONE_SEND_TEST, text)
|
id, err := SendMessage(Settings.TELEGRAM_PHONE_SEND_TEST, text)
|
||||||
log.Info("Message id: ", id)
|
t.Log("Message id: ", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("telegramclient_test.TestSendMessage() SendMessage() error: ", err)
|
t.Error("telegramclient_test.TestSendMessage() SendMessage() error: ", err)
|
||||||
}
|
}
|
||||||
@@ -219,3 +219,21 @@ func TestStartTelegram(t *testing.T) {
|
|||||||
contextmain.CancelContext()
|
contextmain.CancelContext()
|
||||||
contextmain.GetNewContext()
|
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)
|
||||||
|
//}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user