From 38ffdb9e4791652b09d59643c8e0b200c2566e1c Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Wed, 29 Jun 2022 09:56:27 -0400 Subject: [PATCH] Retrieve Bio / Description for vCard desc / note --- telegram/utils.go | 41 +++++++++++++++++++++++++++++++++++ xmpp/extensions/extensions.go | 7 ++++++ xmpp/handlers.go | 19 +++++++++++++--- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/telegram/utils.go b/telegram/utils.go index a4f3899..4c365a3 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -1023,6 +1023,47 @@ func (c *Client) OpenPhotoFile(photoFile *client.File, priority int32) (*os.File return nil, path, err } +// GetChatDescription obtains bio or description according to the chat type +func (c *Client) GetChatDescription(chat *client.Chat) string { + chatType := chat.Type.ChatTypeType() + if chatType == client.TypeChatTypePrivate { + privateType, _ := chat.Type.(*client.ChatTypePrivate) + fullInfo, err := c.client.GetUserFullInfo(&client.GetUserFullInfoRequest{ + UserId: privateType.UserId, + }) + if err == nil { + if fullInfo.Bio != "" { + return fullInfo.Bio + } else if fullInfo.Description != "" { + return fullInfo.Description + } + } else { + log.Warnf("Coudln't retrieve private chat info: %v", err.Error()) + } + } else if chatType == client.TypeChatTypeBasicGroup { + basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup) + fullInfo, err := c.client.GetBasicGroupFullInfo(&client.GetBasicGroupFullInfoRequest{ + BasicGroupId: basicGroupType.BasicGroupId, + }) + if err == nil { + return fullInfo.Description + } else { + log.Warnf("Coudln't retrieve basic group info: %v", err.Error()) + } + } else if chatType == client.TypeChatTypeSupergroup { + supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup) + fullInfo, err := c.client.GetSupergroupFullInfo(&client.GetSupergroupFullInfoRequest{ + SupergroupId: supergroupType.SupergroupId, + }) + if err == nil { + return fullInfo.Description + } else { + log.Warnf("Coudln't retrieve supergroup info: %v", err.Error()) + } + } + return "" +} + // subscribe to a Telegram ID func (c *Client) subscribeToID(id int64, chat *client.Chat) { var args []args.V diff --git a/xmpp/extensions/extensions.go b/xmpp/extensions/extensions.go index 878770a..fac5e7b 100644 --- a/xmpp/extensions/extensions.go +++ b/xmpp/extensions/extensions.go @@ -32,6 +32,7 @@ type IqVcardTemp struct { N IqVcardN Tel IqVcardTel Photo IqVcardPhoto + Desc IqVcardDesc ResultSet *stanza.ResultSet `xml:"set,omitempty"` } @@ -104,6 +105,12 @@ type IqVcardPhotoBinval struct { Text string `xml:",chardata"` } +// IqVcardDesc is vCard/DESC +type IqVcardDesc struct { + XMLName xml.Name `xml:"DESC"` + Text string `xml:",chardata"` +} + // Namespace is a namespace! func (c PresenceNickExtension) Namespace() string { return c.XMLName.Space diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 06bc02b..1222a93 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -260,7 +260,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) { return } - var fn, photo, nickname, given, family, tel string + var fn, photo, nickname, given, family, tel, info string if chat != nil { fn = chat.Title @@ -284,6 +284,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) { log.Errorf("PHOTO: %#v", err.Error()) } } + info = session.GetChatDescription(chat) } if user != nil { nickname = user.Username @@ -299,7 +300,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) { Id: iq.Id, Type: "result", }, - Payload: makeVCardPayload(typ, iq.To, fn, photo, nickname, given, family, tel), + Payload: makeVCardPayload(typ, iq.To, fn, photo, nickname, given, family, tel, info), } log.Debugf("%#v", answer) @@ -371,7 +372,7 @@ func toToID(to string) (int64, bool) { return toID, true } -func makeVCardPayload(typ byte, id string, fn string, photo string, nickname string, given string, family string, tel string) stanza.IQPayload { +func makeVCardPayload(typ byte, id, fn, photo, nickname, given, family, tel, info string) stanza.IQPayload { if typ == TypeVCardTemp { vcard := &extensions.IqVcardTemp{} @@ -384,6 +385,7 @@ func makeVCardPayload(typ byte, id string, fn string, photo string, nickname str vcard.N.Given.Text = given vcard.N.Family.Text = family vcard.Tel.Number.Text = tel + vcard.Desc.Text = info return vcard } else if typ == TypeVCard4 { @@ -447,6 +449,17 @@ func makeVCardPayload(typ byte, id string, fn string, photo string, nickname str }, }) } + if info != "" { + nodes = append(nodes, stanza.Node{ + XMLName: xml.Name{Local: "note"}, + Nodes: []stanza.Node{ + stanza.Node{ + XMLName: xml.Name{Local: "text"}, + Content: info, + }, + }, + }) + } pubsub := &stanza.PubSubGeneric{ Items: &stanza.Items{