diff --git a/telegram/handlers.go b/telegram/handlers.go index 5e01520..1313871 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -193,7 +193,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { "chat_id": update.Message.ChatId, }).Warn("New message from chat") - text := c.messageToText(update.Message) + text := c.messageToText(update.Message, false) file, filename := c.contentToFilename(update.Message.Content) // download file(s) diff --git a/telegram/utils.go b/telegram/utils.go index dbb65d7..66f217a 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -264,6 +264,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess } var str strings.Builder + // add messageid and sender var senderId int64 if message.SenderId != nil { switch message.SenderId.MessageSenderType() { @@ -276,6 +277,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess } } str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(senderId))) + // add date if !preview { str.WriteString( time.Unix(int64(message.Date), 0). @@ -284,21 +286,17 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess ) } + // text message var text string if message.Content != nil { - switch message.Content.MessageContentType() { - case client.TypeMessageText: - messageText, _ := message.Content.(*client.MessageText) - text = messageText.Text.Text - // TODO: handle other message types with labels (not supported in Zhabogram!) - } + text = c.messageToText(message, preview) } if text != "" { if !preview { str.WriteString(text) } else { newlinePos := strings.Index(text, newlineChar) - if !preview || newlinePos == -1 { + if newlinePos == -1 { str.WriteString(text) } else { str.WriteString(text[0:newlinePos]) @@ -374,7 +372,7 @@ func (c *Client) formatLocation(location *client.Location) string { ) } -func (c *Client) messageToText(message *client.Message) string { +func (c *Client) messageToText(message *client.Message, preview bool) string { if message.Content == nil { log.Warnf("Unknown message: %#v", message) return "" @@ -406,7 +404,7 @@ func (c *Client) messageToText(message *client.Message) string { return "kicked " + c.formatContact(deleteMember.UserId) case client.TypeMessagePinMessage: pinMessage, _ := message.Content.(*client.MessagePinMessage) - return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, false, nil) + return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil) case client.TypeMessageChatChangeTitle: changeTitle, _ := message.Content.(*client.MessageChatChangeTitle) return "chat title set to: " + changeTitle.Title @@ -415,102 +413,142 @@ func (c *Client) messageToText(message *client.Message) string { return c.formatLocation(location.Location) case client.TypeMessageVenue: venue, _ := message.Content.(*client.MessageVenue) - return fmt.Sprintf( - "*%s*\n%s\n%s", - venue.Venue.Title, - venue.Venue.Address, - c.formatLocation(venue.Venue.Location), - ) + if preview { + return venue.Venue.Title + } else { + return fmt.Sprintf( + "*%s*\n%s\n%s", + venue.Venue.Title, + venue.Venue.Address, + c.formatLocation(venue.Venue.Location), + ) + } case client.TypeMessagePhoto: photo, _ := message.Content.(*client.MessagePhoto) - return formatter.Format( - photo.Caption.Text, - formatter.SortEntities(photo.Caption.Entities), - markupFunction, - ) + if preview { + return photo.Caption.Text + } else { + return formatter.Format( + photo.Caption.Text, + formatter.SortEntities(photo.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageAudio: audio, _ := message.Content.(*client.MessageAudio) - return formatter.Format( - audio.Caption.Text, - formatter.SortEntities(audio.Caption.Entities), - markupFunction, - ) + if preview { + return audio.Caption.Text + } else { + return formatter.Format( + audio.Caption.Text, + formatter.SortEntities(audio.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageVideo: video, _ := message.Content.(*client.MessageVideo) - return formatter.Format( - video.Caption.Text, - formatter.SortEntities(video.Caption.Entities), - markupFunction, - ) + if preview { + return video.Caption.Text + } else { + return formatter.Format( + video.Caption.Text, + formatter.SortEntities(video.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageDocument: document, _ := message.Content.(*client.MessageDocument) - return formatter.Format( - document.Caption.Text, - formatter.SortEntities(document.Caption.Entities), - markupFunction, - ) + if preview { + return document.Caption.Text + } else { + return formatter.Format( + document.Caption.Text, + formatter.SortEntities(document.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageText: text, _ := message.Content.(*client.MessageText) - return formatter.Format( - text.Text.Text, - formatter.SortEntities(text.Text.Entities), - markupFunction, - ) + if preview { + return text.Text.Text + } else { + return formatter.Format( + text.Text.Text, + formatter.SortEntities(text.Text.Entities), + markupFunction, + ) + } case client.TypeMessageVoiceNote: voice, _ := message.Content.(*client.MessageVoiceNote) - return formatter.Format( - voice.Caption.Text, - formatter.SortEntities(voice.Caption.Entities), - markupFunction, - ) + if preview { + return voice.Caption.Text + } else { + return formatter.Format( + voice.Caption.Text, + formatter.SortEntities(voice.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageVideoNote: return "" case client.TypeMessageAnimation: animation, _ := message.Content.(*client.MessageAnimation) - return formatter.Format( - animation.Caption.Text, - formatter.SortEntities(animation.Caption.Entities), - markupFunction, - ) + if preview { + return animation.Caption.Text + } else { + return formatter.Format( + animation.Caption.Text, + formatter.SortEntities(animation.Caption.Entities), + markupFunction, + ) + } case client.TypeMessageContact: contact, _ := message.Content.(*client.MessageContact) - var jid string - if contact.Contact.UserId != 0 { - jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare()) + if preview { + return contact.Contact.FirstName + " " + contact.Contact.LastName + } else { + var jid string + if contact.Contact.UserId != 0 { + jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare()) + } + return fmt.Sprintf( + "*%s %s*\n%s\n%s\n%s", + contact.Contact.FirstName, + contact.Contact.LastName, + contact.Contact.PhoneNumber, + contact.Contact.Vcard, + jid, + ) } - return fmt.Sprintf( - "*%s %s*\n%s\n%s\n%s", - contact.Contact.FirstName, - contact.Contact.LastName, - contact.Contact.PhoneNumber, - contact.Contact.Vcard, - jid, - ) case client.TypeMessageDice: dice, _ := message.Content.(*client.MessageDice) return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value) case client.TypeMessagePoll: poll, _ := message.Content.(*client.MessagePoll) - rows := []string{} - rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question)) - for _, option := range poll.Poll.Options { - var tick string - if option.IsChosen { - tick = "x" - } else { - tick = " " + if preview { + return poll.Poll.Question + } else { + rows := []string{} + rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question)) + for _, option := range poll.Poll.Options { + var tick string + if option.IsChosen { + tick = "x" + } else { + tick = " " + } + rows = append(rows, fmt.Sprintf( + "[%s] %s | %v%% | %v vote", + tick, + option.Text, + option.VotePercentage, + option.VoterCount, + )) } - rows = append(rows, fmt.Sprintf( - "[%s] %s | %v%% | %v vote", - tick, - option.Text, - option.VotePercentage, - option.VoterCount, - )) - } - return strings.Join(rows, "\n") + return strings.Join(rows, "\n") + } } return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType())